import sys

while True:
    try:
        t = int(input())
        for _ in range(t):
            n = int(input())
            s = input()
            s2 = ''
            l = len(s)
            minl = l
            for c in s:
                if c in s2:
                    a = len(s2)-s2.rfind(c)-1
                    b = l-a-2
                    e = len(s2)-s2.find(c)-1
                    d = l-e-2
                    minl = min(a,b,e,d,minl)
                s2 += c
            if minl == l:
                print(-1)
            else:
                print(minl)
    except:
        break