description: Grandma Capa has decided to knit a scarf and asked Grandpa Sher to make a pattern for it, a pattern is a string consisting of lowercase English letters. Grandpa Sher wrote a string s of length n.
  
  Grandma Capa wants to knit a beautiful scarf, and in her opinion, a beautiful scarf can only be knit from a string that is a palindrome. She wants to change the pattern written by Grandpa Sher, but to avoid offending him, she will choose one lowercase English letter and erase some (at her choice, possibly none or all) occurrences of that letter in string s.
  
  She also wants to minimize the number of erased symbols from the pattern. Please help her and find the minimum number of symbols she can erase to make string s a palindrome, or tell her that it's impossible. Notice that she can only erase symbols equal to the one letter she chose.
  
  A string is a palindrome if it is the same from the left to the right and from the right to the left. For example, the strings 'kek', 'abacaba', 'r' and 'papicipap' are palindromes, while the strings 'abb' and 'iq' are not.
  
  Input
  
  The first line contains a single integer t (1 ≤ t ≤ 100) — the number of test cases. The next 2 ⋅ t lines contain the description of test cases. The description of each test case consists of two lines.
  
  The first line of each test case contains a single integer n (1 ≤ n ≤ 10^5) — the length of the string.
  
  The second line of each test case contains the string s consisting of n lowercase English letters.
  
  It is guaranteed that the sum of n over all test cases does not exceed 2 ⋅ 10^5.
  
  Output
  
  For each test case print the minimum number of erased symbols required to make the string a palindrome, if it is possible, and -1, if it is impossible.
  
  Example
  
  Input
  
  
  5
  8
  abcaacab
  6
  xyzxyz
  4
  abba
  8
  rprarlap
  10
  khyyhhyhky
  
  
  Output
  
  
  2
  -1
  0
  3
  2
  
  Note
  
  In the first test case, you can choose a letter 'a' and erase its first and last occurrences, you will get a string 'bcaacb', which is a palindrome. You can also choose a letter 'b' and erase all its occurrences, you will get a string 'acaaca', which is a palindrome as well.
  
  In the second test case, it can be shown that it is impossible to choose a letter and erase some of its occurrences to get a palindrome.
  
  In the third test case, you don't have to erase any symbols because the string is already a palindrome.
solution: // Problem: C. Grandma Capa Knits a Scarf
  // Contest: Codeforces - Codeforces Round #750 (Div. 2)
  // URL: https://codeforces.com/contest/1582/problem/C
  // Memory Limit: 256 MB
  // Time Limit: 1000 ms
  // 
  // Powered by CP Editor (https://cpeditor.org)
  
  import java.io.*;
  import java.util.*;
  
  import static java.lang.Math.max;
  import static java.lang.Math.min;
  import static java.lang.Math.abs;
  
  
  /**
   * zxz 
   * problem link:
   **/
  public class Main {
      //put some data here
  
      /**
       * do some clean action before next testcase
       */
      private void clear() {
  
      }
  
  
      private void solve() throws Exception {
          rf();
          int n = ri();
  		rf();
  		char [] s = rs2c();
  		int ans = -1;
  		for(int i = 0; i < 26; i++){
  			char t = (char)('a' + i);
  			int l = 0, r = n - 1;
  			int cnt = 0;
  			while(l < r){
  				if(s[l] == s[r]){
  					l ++;
  					r --;
  				}else{
  					if(s[l] == t){
  						cnt ++;
  						l++;
  					}else
  					if(s[r] == t){
  						r--;
  						cnt++;
  					}else{
  						cnt = -1;
  						break;
  					}
  				}
  			}
  			if(cnt != -1){
  				if(ans == -1){
  					ans = cnt;
  				}else{
  					ans = min(cnt, ans);
  				}
  			}
  		}
  		addAns(ans);
  
      }
  
      private void run() throws Exception {
  
         rf();
         int T = ri();
          // int T = 1;
  
          while (T-- > 0) {
              solve();
              if (T != 0) {
                  clear();
              }
          }
          printAns();
      }
  
      public static void main(String[] args) throws Exception {
          new Main().run();
      }
  
      StringBuilder sb = new StringBuilder();
      BufferedReader infile = new BufferedReader(new InputStreamReader(System.in));
      StringTokenizer st;
  
  	private void addAns(int a){
  		sb.append(a + "\n");
  	}
  	
  	private void addAns(long a){
  		sb.append(a + "\n");
  	}
  	
  	private void addAns(String s){
  		sb.append(s + "\n");
  	}
  
      private void rf() throws IOException {
          st = new StringTokenizer(infile.readLine());
      }
  
  
      private int ri() {
          return Integer.parseInt(st.nextToken());
      }
  
      private long rl() {
          return Long.parseLong(st.nextToken());
      }
  
  	private char [] rs2c(){
  		return st.nextToken().toCharArray();
  	}
  	
  	private String rs(){
  		return st.nextToken();
  	}
  
      private void printAns() {
          System.out.println(sb);
      }
  
      private int[] readArr(int N) throws Exception {
          int[] arr = new int[N];
          rf();
          for (int i = 0; i < N; i++) {
              arr[i] = Integer.parseInt(st.nextToken());
          }
          return arr;
      }
  
      private void print(int[] arr) {
          //for debugging only
          for (int x : arr) {
              System.out.print(x + " ");
          }
          System.out.println();
      }
      
      private void print(long[] arr) {
          //for debugging only
          for (long x : arr) {
              System.out.print(x + " ");
          }
          System.out.println();
      }
  
      private long[] readArr2(int N) throws Exception {
          long[] arr = new long[N];
          st = new StringTokenizer(infile.readLine());
          for (int i = 0; i < N; i++) {
              arr[i] = Long.parseLong(st.nextToken());
          }
          return arr;
      }
  
      private boolean isPrime(long n) {
          if (n < 2) {
              return false;
          }
          if (n == 2 || n == 3) {
              return true;
          }
          if (n % 2 == 0 || n % 3 == 0) {
              return false;
          }
          long sqrtN = (long) Math.sqrt(n) + 1;
          for (long i = 6L; i <= sqrtN; i += 6) {
              if (n % (i - 1) == 0 || n % (i + 1) == 0) {
                  return false;
              }
          }
          return true;
      }
  
      private long gcd(long a, long b) {
          if (a > b) {
              a = (a + b) - (b = a);
          }
          if (a == 0L) {
              return b;
          }
          return gcd(b % a, a);
      }
      
      private int gcd(int a, int b) {
          if (a > b) {
              a = (a + b) - (b = a);
          }
          if (a == 0) {
              return b;
          }
          return gcd(b % a, a);
      }
      
      private double pow(double x, long N) {
          double ans = 1.0;
          // 贡献的初始值为 x
          double x_contribute = x;
          // 在对 N 进行二进制拆分的同时计算答案
          while (N > 0) {
              if (N % 2 == 1) {
                  // 如果 N 二进制表示的最低位为 1，那么需要计入贡献
                  ans *= x_contribute;
              }
              // 将贡献不断地平方
              x_contribute *= x_contribute;
              // 舍弃 N 二进制表示的最低位，这样我们每次只要判断最低位即可
              N /= 2;
          }
          return ans;
      }
      
      long pow_mod(long x, long i, long mod){
  	    if(i == 0){
  	        return 1;
  	    }
  	    long temp = pow_mod(x, i >> 1, mod);
  	    temp = temp * temp % mod;
  	    if((i & 1 )!= 0){
  	        temp = temp * x % mod;
  	    }
  	    return temp;
  	}
      
  
  }
  