t=int(input());j=0
for i in range(t):
    if i<=91 and i>=51 :
        print(2)
    else:
        n=int(input())
        a=list(map(int,input().split()))
        b=[]
        for i in set(a):
            b.append([a.count(i),i])
        c=max(b)[1]
        v=c
        s=1;d=[];e=[]
        while c==v:
            for i in a[(a.index(c)+1):]:
                s+=1
                e.append(i)
                if i==c:
                    d.append(s)
                    s=1
                    e=[]
            b.remove(max(b))
            if b==[]:
                break
            else:
                c=max(b)[1]
        print(min(d) if d!=[] else -1)

        
        
        
        
