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: //package Codeforces.Round750Div2;
  
  import java.io.*;
  import java.util.*;
  
  public class C {
      public static void main(String[] args) throws IOException {
          Scanner sc = new Scanner(System.in);
  
          int t = sc.nextInt();
          StringBuilder sb = new StringBuilder();
          while (t-->0){
              int n = sc.nextInt();
              String s = sc.next();
  
              int min = Integer.MAX_VALUE;
              for(int i=0;i<26;i++){
                  int l = 0;
                  int r = n-1;
                  int dels = 0;
                  boolean flag = true;
                  char pchar = (char) (i+97);
                  while(l<=r){
                      char chl = s.charAt(l);
                      char chr = s.charAt(r);
  
                      int isp = 0;
                      if(chl==pchar){
                          isp++;
                      }
                      if(chr==pchar){
                          isp++;
                      }
  
                      if(isp==1){
                          if(chl==pchar){
                              dels++;
                              l++;
                          }
                          else {
                              dels++;
                              r--;
                          }
                      }
                      else if(isp==2){
                          l++;
                          r--;
                      }
                      else{
                          if(chl==chr){
                              l++;
                              r--;
                          }
                          else{
                              flag = false;
                              break;
                          }
                      }
                  }
  
                  if(flag){
                      min = Math.min(min, dels);
                  }
              }
  
              if(min==Integer.MAX_VALUE){
                  sb.append("-1\n");
              }
              else{
                  sb.append(min).append("\n");
              }
          }
  
          System.out.println(sb);
  
          sc.close();
      }
  
      static class Soumit {
          final private int BUFFER_SIZE = 1 << 18;
          final private DataInputStream din;
          final private byte[] buffer;
          private PrintWriter pw;
          private int bufferPointer, bytesRead;
          StringTokenizer st;
  
          public Soumit() {
              din = new DataInputStream(System.in);
              buffer = new byte[BUFFER_SIZE];
              bufferPointer = bytesRead = 0;
          }
  
          public Soumit(String file_name) throws IOException {
              din = new DataInputStream(new FileInputStream(file_name));
              buffer = new byte[BUFFER_SIZE];
              bufferPointer = bytesRead = 0;
          }
  
          public void streamOutput(String file) throws IOException {
              FileWriter fw = new FileWriter(file);
              BufferedWriter bw = new BufferedWriter(fw);
              pw = new PrintWriter(bw);
          }
  
          public void println(String a) {
              pw.println(a);
          }
  
          public void print(String a) {
              pw.print(a);
          }
  
          public String readLine() throws IOException {
              byte[] buf = new byte[3000064]; // line length
              int cnt = 0, c;
              while ((c = read()) != -1) {
                  if (c == '\n')
                      break;
                  buf[cnt++] = (byte) c;
              }
              return new String(buf, 0, cnt);
          }
  
          String next() {
              while (st == null || !st.hasMoreElements()) {
                  try {
                      st = new StringTokenizer(readLine());
                  } catch (IOException e) {
                      e.printStackTrace();
                  }
              }
              return st.nextToken();
          }
  
          public void sort(int[] arr) {
              ArrayList<Integer> arlist = new ArrayList<>();
              for (int i : arr)
                  arlist.add(i);
  
              Collections.sort(arlist);
              for (int i = 0; i < arr.length; i++)
                  arr[i] = arlist.get(i);
          }
  
          public void sort(long[] arr) {
              ArrayList<Long> arlist = new ArrayList<>();
              for (long i : arr)
                  arlist.add(i);
  
              Collections.sort(arlist);
              for (int i = 0; i < arr.length; i++)
                  arr[i] = arlist.get(i);
          }
  
          public int[] nextIntArray(int n) throws IOException {
              int[] arr = new int[n];
              for (int i = 0; i < n; i++) {
                  arr[i] = nextInt();
              }
  
              return arr;
          }
  
          public long[] nextLongArray(int n) throws IOException {
              long[] arr = new long[n];
              for (int i = 0; i < n; i++) {
                  arr[i] = nextLong();
              }
  
              return arr;
          }
  
          public double[] nextDoubleArray(int n) throws IOException {
              double[] arr = new double[n];
              for (int i = 0; i < n; i++) {
                  arr[i] = nextDouble();
              }
  
              return arr;
          }
  
          public int nextInt() throws IOException {
              int ret = 0;
              byte c = read();
              while (c <= ' ')
                  c = read();
              boolean neg = (c == '-');
              if (neg)
                  c = read();
              do {
                  ret = ret * 10 + c - '0';
              } while ((c = read()) >= '0' && c <= '9');
  
              if (neg)
                  return -ret;
              return ret;
          }
  
          public long nextLong() throws IOException {
              long ret = 0;
              byte c = read();
              while (c <= ' ')
                  c = read();
              boolean neg = (c == '-');
              if (neg)
                  c = read();
              do {
                  ret = ret * 10 + c - '0';
              }
              while ((c = read()) >= '0' && c <= '9');
              if (neg)
                  return -ret;
              return ret;
          }
  
          public double nextDouble() throws IOException {
              double ret = 0, div = 1;
              byte c = read();
              while (c <= ' ')
                  c = read();
              boolean neg = (c == '-');
              if (neg)
                  c = read();
  
              do {
                  ret = ret * 10 + c - '0';
              }
              while ((c = read()) >= '0' && c <= '9');
  
              if (c == '.') {
                  while ((c = read()) >= '0' && c <= '9') {
                      ret += (c - '0') / (div *= 10);
                  }
              }
  
              if (neg)
                  return -ret;
              return ret;
          }
  
          private void fillBuffer() throws IOException {
              bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
              if (bytesRead == -1)
                  buffer[0] = -1;
          }
  
          private byte read() throws IOException {
              if (bufferPointer == bytesRead)
                  fillBuffer();
              return buffer[bufferPointer++];
          }
  
          public void close() throws IOException {
              /*if (din == null)
                  return;*/
              if (din != null) din.close();
              if (pw != null) pw.close();
          }
      }
  }
  