#include <iostream>
#include <cstdio>
#include <sstream>
#include <string>
using namespace std;
int n;
string str;
int main()
{
int t;
cin>>t;
while(t--)
{
cin>>n;
cin>>str;
int i=0,j=1,k;
while(i<n && j<n)
{
for(k=0;k<n;k++)
{
if(str[(k+i)%n]!=str[(k+j)%n])
break;
}
if(k==n)
break;
if(str[(k+i)%n]>str[(k+j)%n])
i=i+k+1;
else if(str[(k+i)%n]<str[(k+j)%n])
j=j+k+1;
if(i==j)
j+=1;
}
cout<<((i>j)?j:i)<<"\n";
}
return 0;
}