#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int MAXN = 200010;
int next[MAXN];
void Get_Next(char* str, int n)
{
int i = 0, j = -1;
next[0] = -1;
while(i < n)
{
if(j == -1 || str[i] == str[j])
{
++i;
++j;
next[i] = j;
}
else
j = next[j];
}
}
int main()
{
int T, i;
cin>>T;
char str[MAXN];
while(T--)
{
int ans = 0;
int n;
cin>>n>>str;
Get_Next(str, n);
ans = next[n] + n;
for(i = 0; i < n; ++i)
if(next[i] > 0 && next[i] + 1 != next[i+1])
ans += next[i];
cout<<ans%10007<<endl;
}
return 0;
}