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: import decimal
  import heapq
  import math
  import os
  import sys
  from array import array
  from collections import Counter, deque
  from io import BytesIO, IOBase
  import bisect
  
  BUFSIZE = 8192
  
  
  class FastIO(IOBase):
      newlines = 0
  
      def __init__(self, file):
          self._fd = file.fileno()
          self.buffer = BytesIO()
          self.writable = 'x' in file.mode or 'r' not in file.mode
          self.write = self.buffer.write if self.writable else None
  
      def read(self):
          while True:
              b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
              if not b:
                  break
              ptr = self.buffer.tell()
              self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
          self.newlines = 0
          return self.buffer.read()
  
      def readline(self):
          while self.newlines == 0:
              b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
              self.newlines = b.count(b'\n') + (not b)
              ptr = self.buffer.tell()
              self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
          self.newlines -= 1
          return self.buffer.readline()
  
      def flush(self):
          if self.writable:
              os.write(self._fd, self.buffer.getvalue())
              self.buffer.truncate(0), self.buffer.seek(0)
  
  
  class IOWrapper(IOBase):
      def __init__(self, file):
          self.buffer = FastIO(file)
          self.flush = self.buffer.flush
          self.writable = self.buffer.writable
          self.write = lambda s: self.buffer.write(s.encode('ascii'))
          self.read = lambda: self.buffer.read().decode('ascii')
          self.readline = lambda: self.buffer.readline().decode('ascii')
  
  
  sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
  input = lambda: sys.stdin.readline().rstrip('\r\n')
  
  
  def ints_get(): return map(int, input().strip().split())
  
  
  def list_get(): return list(map(int, sys.stdin.readline().strip().split()))
  
  
  def chars_get(): return list(map(str, sys.stdin.readline().strip().split()))
  
  
  def output(to_be_printed, end="\n"): sys.stdout.write(str(to_be_printed) + end)
  
  
  def ipn(): return int(input())
  
  
  # ******************************************************#
  # **************** code starts here ********************#
  # ******************************************************#
  
  
  def main():
      for _ in range(ipn()):
          n = ipn()
          s = [x for x in input()]
          c = Counter(s)
          out = 1e9
          flag = 0
          for j in range(len(s)):
              if s[j] != s[len(s) - j - 1]:
                  flag = 1
                  break
          if flag == 0:
              out = 0
  
          for i in c:
              p, q = 0, len(s) - 1
              flag = 0
              count = 0
              while p < q:
                  if s[p] != s[q]:
                      if s[p] == i:
                          count += 1
                          p += 1
                      elif s[q] == i:
                          count += 1
                          q -= 1
                      else:
                          flag = 1
                          break
                  else:
                      p += 1
                      q -= 1
              if flag == 0:
                  out = min(out, count)
          if out > 1e8:
              print(-1)
          else:
              print(out)
      return
  
  
  if __name__ == "__main__":
      main()
  
  
  