#include<bits/stdc++.h>
#define INF 100000000
using namespace std;

struct sg {
	int go[26];
	int par,val,u;
};
sg g[110000];
char s[21000];
int v[110000];
vector<int> a[110000];
int last,tot,root;

void add(int w,int u) {
	int p=last,np=++tot;
	g[np].val=g[p].val+1;
	g[np].u=u;
	while (p && g[p].go[w]==0) g[p].go[w]=np,p=g[p].par;
	if (p==0) g[np].par=root;
	else {
		int q=g[p].go[w];
		if (g[p].val+1==g[q].val) g[np].par=q;
		else {
			int nq=++tot;
			g[nq]=g[q];
			g[nq].val=g[p].val+1;
			g[nq].par=g[q].par;
			g[q].par=nq;
			g[np].par=nq;
			while (p && g[p].go[w]==q) g[p].go[w]=nq,p=g[p].par;
		}
	}
	last=np;
}

void get() {
	for (int i=1;i<=tot;i++) a[g[i].val].push_back(i);
	for (int i=tot;i>=0;i--)
		for (int j=0;j<a[i].size();j++) {
			int x=a[i][j];
			g[g[x].par].u=min(g[g[x].par].u,g[x].u);
		}
	for (int i=1;i<=tot;i++) a[i].clear();
}


int main() {
	int tt,n;
	scanf("%d",&tt);
	while (tt--) {
		scanf("%d",&n);
		scanf("%s",s);
		last=root=tot=1;
		g[root].u=INF;
		for (int i=0;i<n-1;i++) add(s[i]-'a',INF);
		add(s[n-1]-'a',1);
		for (int i=0;i<n-1;i++) add(s[i]-'a',i+2);
		get();
		int x=root,t=n;
		string s1="";
		while (t--) {
			for (int i=25;i>=0;i--) if (g[x].go[i]) {
				s1+='a'+i;
				x=g[x].go[i];
				break;
			}
		} 
		int a1=g[x].u;
		memset(g,0,sizeof(sg)*(tot+10));
		
		last=root=tot=1;
		for (int i=n-1;i>0;i--) add(s[i]-'a',INF);
		add(s[0]-'a',n);
		for (int i=n-1;i>0;i--) add(s[i]-'a',i);
	
		get();
		int y=root;
		t=n;
		string s2="";
		while (t--) {
			for (int i=25;i>=0;i--) if (g[y].go[i]) {
				s2+='a'+i;
				y=g[y].go[i];
				break;
			}
		} 
		int a2=g[y].u;
		if (s1>s2 || s1==s2 && a1<=a2) printf("%d 0\n",a1);
		else printf("%d 1\n",a2);
		memset(g,0,sizeof(sg)*(tot+10));
	}
}

