def ind(l,x):
    t=[]
    for i in range(len(l)):
        if l[i]== x:
            t.append(i)
    return t
    
t= int(input())

l=  []
for i in range(t):
    n=int(input())
    l.append([int(i) for i in input().split()])
    


for a in l:
    p=len(a)+1
    t=[[] for i in range(len(a))]
    for i in range(len(a)):
        if t[a[i]-1]==[]:
            t[a[i]-1].append(i)
        else :
            if i-t[a[i]-1][0]+1< p:
                p=i-t[a[i]-1][0]+1
            t[a[i]-1][0]= i
            if p == 2:
                break
    if p<= len(a):
        print(p)
    else:
        print(-1)
