test=int(input())
for testing in range(test):
    n=int(input())
    t=[int(x) for x in input().split()]
    if n<=1 :
        print(-1)
        continue
    else :
        l=[x for x in t if t.count(x)>1]
        l=[x for x in set(l)]
        pointer=[]
        for x in l:
            joker=True
            i=0
            first=True
            while i<len(t):
                if t[i]==x and joker:
                    s=i
                    joker=False
                if t[i]==x and not joker and i!=s:
                    pointer.append(t[s:i+1])
                    if first :
                        mini=len(t[s:i+1])
                        first=False
                    else:
                        if mini<len(t[s:i+1]):
                            mini=len(t[s:i+1])
                    joker=True
                    i-=1
                i+=1
    if not len(pointer):
        print(-1)
    else:
        print(mini)
