#include<bits/stdc++.h>
using namespace std;
const long long M=2e5+10,MOD=998244353;
typedef long long ll;
#define debug(x) cout<<x<<endl
vector<string>all;
vector<int>prime;
vector<int>pp(3000,0);
precal(){
string a="abc";
all.push_back(a);
while(next_permutation(a.begin(),a.end())){
all.push_back(a);
}
prime.push_back(0);
pp[0]=1;
for(int i=2;i<=2000;i++){
int x=1;
for(int j=2;j*j<=i;j++){
if(i%j==0){
x=0;
}
}
if(x)prime.push_back(i);
if(x)pp[i]=1;
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int t=1;
cin>>t;
precal();
while(t--){
int n;
cin>>n;
string s;
cin>>s;
vector<int>fre(3,0);
for(auto i:s){
fre[i-'a']++;
}
int mx=MOD;
for(auto it:all){
string p=it;
for(auto start:prime){
if(start>n)break;
for(auto end:prime){
if(start+end>n)break;
int baki=n-(start+end);
if(!pp[baki])continue;
int a=fre[p[0]-'a'];
int b=fre[p[1]-'a'];
int c=fre[p[2]-'a'];
int pos=0,neg=0;
if(a>start)pos+=a-start;
if(a<start)neg+=start-a;
if(b>end)pos+=b-end;
if(b<end)neg+=end-b;
if(c>baki)pos+=c-baki;
if(c<baki)neg+=baki-c;
mx=min(mx,min(pos,neg));
}
}
}
if(mx==MOD)mx=-1;
cout<<mx<<"\n";
}
return 0;
}