#include <cstdio>#include <cstring>#include <cstdlib>#include <vector>#include <cmath>#include <iostream>#include <algorithm>#include <functional>#include <map>#include <cctype>using namespace std;char str[200010];char str2[100010];int main(){ int T,L; scanf("%d",&T); while(T--) { scanf("%d %s",&L,str); int len=strlen(str); strcpy(str2,str); strcat(str,str2); int i,j,k; i=0;j=1;k=0; while(k<len&&i<len&&j<len) { if(str[i+k]==str[j+k]) { k++; } else { if(str[i+k]>str[j+k]) { i=i+k+1; if(i==j) i++; } else { j=j+k+1; if(i==j) j++; } k=0; } } printf("%dn",i<j?i:j); } return 0 ;}