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: /*input
  5
  8
  abcaacab
  6
  xyzxyz
  4
  abba
  8
  rprarlap
  10
  khyyhhyhky
  
  */
  import java.util.*;
  import java.lang.*;
  import java.io.*;
  
  public class Main
  {
  	static PrintWriter out;
  	static int MOD = 1000000007;
  	static FastReader scan;
  
      /*-------- I/O usaing short named function ---------*/
  	public static String ns(){return scan.next();}
  	public static int ni(){return scan.nextInt();}
  	public static long nl(){return scan.nextLong();}
  	public static double nd(){return scan.nextDouble();}
  	public static String nln(){return scan.nextLine();}
  	public static void p(Object o){out.print(o);}
  	public static void ps(Object o){out.print(o + " ");}
  	public static void pn(Object o){out.println(o);}
      /*-------- for output of an array ---------------------*/
       static void iPA(int arr []){
       	 StringBuilder output = new StringBuilder();
  	     for(int i=0; i<arr.length; i++)output.append(arr[i] + " ");out.println(output);
  	}
  	static void lPA(long arr []){
  	     StringBuilder output = new StringBuilder();
  	     for(int i=0; i<arr.length; i++)output.append(arr[i] + " ");out.println(output);
  	}
  	static void sPA(String arr []){
  	     StringBuilder output = new StringBuilder();
  	     for(int i=0; i<arr.length; i++)output.append(arr[i] + " ");out.println(output);
  	}
  	static void dPA(double arr []){
  	     StringBuilder output = new StringBuilder();
  	     for(int i=0; i<arr.length; i++)output.append(arr[i] + " ");out.println(output);
  	}
      /*-------------- for input in an array ---------------------*/
  	static void iIA(int arr[]){
  	     for(int i=0; i<arr.length; i++)arr[i] = ni();
  	}
  	static void lIA(long arr[]){
  	     for(int i=0; i<arr.length; i++)arr[i] = nl(); 
  	}
  	static void sIA(String arr[]){
  	     for(int i=0; i<arr.length; i++)arr[i] = ns(); 
  	}
  	static void dIA(double arr[]){
  	     for(int i=0; i<arr.length; i++)arr[i] = nd();
  	}
  	/*------------ for taking input faster ----------------*/
       static class FastReader
  	{
  		BufferedReader br;
  		StringTokenizer st;
  		public FastReader(){br = new BufferedReader(new InputStreamReader(System.in));}
  		String next(){
  			while (st == null || !st.hasMoreElements()){
  				try{st = new StringTokenizer(br.readLine());}
  				catch (IOException e){e.printStackTrace();}
  			}
  			return st.nextToken();
  		}
  		int nextInt(){return Integer.parseInt(next());}
  		long nextLong(){return Long.parseLong(next());}
  		double nextDouble(){return Double.parseDouble(next());}
  		String nextLine(){
  			String str = "";
  			try{str = br.readLine();}
  			catch (IOException e){e.printStackTrace();}
  			return str;
  		}
  	}
  
  	// Method to check if x is power of 2
      static boolean isPowerOfTwo (int x) { return x!=0 && ((x&(x-1)) == 0);}
      //Method to return gcd of two numbers
      static int gcd(int a, int b){return a==0?b:gcd(b % a, a); }
      //Method to count digit of a number
      static int countDigit(long n){return (int)Math.floor(Math.log10(n) + 1);}
      //Method for sorting
  	static void sort(int[] a) {
  		ArrayList<Integer> l=new ArrayList<>();
  		for (int i:a) l.add(i);
  		Collections.sort(l);
  		for (int i=0; i<a.length; i++) a[i]=l.get(i);
  	}
  	//Method for sorting
  	static void sort(long[] a) {
  		ArrayList<Long> l=new ArrayList<>();
  		for (long i:a) l.add(i);
  		Collections.sort(l);
  		for (int i=0; i<a.length; i++) a[i]=l.get(i);
  	}
  	//Method foAr checking if a number is prime or not
  	static boolean isPrime(int n) { 
          if (n <= 1) return false; 
          if (n <= 3) return true; 
          if (n % 2 == 0 || n % 3 == 0) return false; 
          for (int i = 5; i * i <= n; i = i + 6) 
              if (n % i == 0 || n % (i + 2) == 0) 
              	return false; 
          return true; 
      } 
  
  	public static void main (String[] args) throws java.lang.Exception
  	{
          out =new PrintWriter(System.out);
          scan =new FastReader();
  		//for fast output sometimes
  		StringBuilder sb = new StringBuilder();
  		int t = ni();
  		while(t-->0){
  			int n = ni();
  			String s = ns();
  			char ch[] = s.toCharArray();
  
  			long ans = Integer.MAX_VALUE;
  
  			for(char c = 'a'; c<='z'; c++){
  				int l = 0, r = n-1;
  				long count = 0;
  				boolean palindrome = true;
  
  				while(l<r && l<n && r>=0){
  					if(ch[l] == ch[r]){
  						l++;
  						r--;
  					}
  					else{
  						if(ch[l] == c){
  							l++;
  							count++;
  						}else if(ch[r] == c){
  							r--;
  							count++;
  						}else{
  							palindrome = false;
  							break;
  
  						}
  					}
  				}
  				if(palindrome)
  					ans = Math.min(ans, count);
  
  			}
  			if(ans != Integer.MAX_VALUE)
  			pn(ans);else pn(-1);
          }
          out.flush();
  		out.close();
  	}
  	
  }