t=int(input())
for i in range(t):
    n=int(input())
    a=list(map(int,input().strip().split(" ")))
    x=[[0,-1] for _ in range(max(a)+1)]
    ans=10000000
    f=len(list(set(a)))
    if f==n:
        print(-1)
    elif f==1 and f!=n:
        print(2)
    else:
        for j in range(n):
            if x[a[j]][0]==0:
                x[a[j]][0]+=1
                x[a[j]][1]=j
            
            
            elif x[a[j]][0]>0:
                k=j-x[a[j]][1]
                x[a[j]][1]=j
                if k<ans:
                    ans=k
            
                
            
        print(ans+1)
