#include <iostream>
using namespace std;

int main() {
    int a;cin>>a;
    while (a--) { // 注意 while 处理多个 case
        int n ;cin>>n;
        string str;
        cin>>str;
        int out =-1;
        for(int i=0;i<=n-2;i++)
        {
            for(int j=0;j<n;j++)
            {
                if(j+i+1<n&&str[j]==str[j+i+1]||
                j+i+1>=n&&str[j]==str[j+i+1-n])
                {out =i;i=n-2;
                break;
                }
            }
        }
        cout << out << endl;
    }
}
// 64 位输出请用 printf("%lld")