import sys
from collections import Counter
def get_ints(): return list(map(int, sys.stdin.readline().strip().split()))
sys.setrecursionlimit(20000)
T = int(input())
for _ in range(T):
n = get_ints()[0]
s = sys.stdin.readline()
count = Counter()
for ss in s:
count[ss]+=1
ans = -1
for x in count:
l = 0
r = (n - 1)
removals = 0
while l < r:
if s[l] == s[r]:
l+= 1
r-= 1
elif s[l] == x:
l+= 1
removals+= 1
elif s[r] == x:
r-= 1
removals+= 1
else:
removals = -1
break
if removals != -1:
if removals <= ans or ans == -1:
ans = removals
print(ans)
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mod 1000000007
#define fr(n) for(ll i=0;i<n;i++)
#define frj(m) for(ll j=0;j<m;j++)
#define fro(m) for(ll i=1; i<=m; i++)
#define frr(n) for(ll i=n;i>=0;i--)
#define nl cout<<"\n"
#define nll '\n'
#define pr(n) cout<<n<<'\n';
#define yes cout<<"YES\n"
#define no cout<<"NO\n"
#define ff first
#define ss second
#define pb push_back
#define pf push_front
#define inf 1ll<<62
#define inff 1<<30
#define debug(arr,n) for(int i=0; i<n; i++) cout<<arr[i]<<" "; cout<<"\n"
#define debugr(arr,s,e) for(int i=s; i<=e; i++) cout<<arr[i]<<" "; cout<<"\n"
#define inp freopen("input.txt", "r", stdin)
#define outp freopen("output.txt", "w", stdout)
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
int main(){
fastio;
ll t;
cin>>t;
while(t--){
ll n;
cin>>n;
string s;
cin>>s;
ll ans=-1;
for(char ch='a';ch<='z';ch++){
ll l=0,r=n-1,cnt=0,f=0;
while(l<r){
if(s[l]==s[r]){
l++,r--;
}
else{
if(s[l]==ch){
l++;cnt++;
}
else if(s[r]==ch){
r--;cnt++;
}
else{
f=1;break;
}
}
}
if(!f){
if(ans==-1) ans=cnt;
else ans=min(ans,cnt);
}
}
cout<<ans<<nll;
}
return 0;
}
1302. Deepest Leaves Sum | 1209. Remove All Adjacent Duplicates in String II |
994. Rotting Oranges | 983. Minimum Cost For Tickets |
973. K Closest Points to Origin | 969. Pancake Sorting |
967. Numbers With Same Consecutive Differences | 957. Prison Cells After N Days |
946. Validate Stack Sequences | 921. Minimum Add to Make Parentheses Valid |
881. Boats to Save People | 497. Random Point in Non-overlapping Rectangles |
528. Random Pick with Weight | 470. Implement Rand10() Using Rand7() |
866. Prime Palindrome | 1516A - Tit for Tat |
622. Design Circular Queue | 814. Binary Tree Pruning |
791. Custom Sort String | 787. Cheapest Flights Within K Stops |
779. K-th Symbol in Grammar | 701. Insert into a Binary Search Tree |
429. N-ary Tree Level Order Traversal | 739. Daily Temperatures |
647. Palindromic Substrings | 583. Delete Operation for Two Strings |
518. Coin Change 2 | 516. Longest Palindromic Subsequence |
468. Validate IP Address | 450. Delete Node in a BST |