t=int(input())
for i in range(t):
    n=int(input())
    a=list(map(int,input().strip().split(" ")))
    x=[-1 for __ in  range(n+1)]
    ans=n+90
    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]]!=-1):
                ans=min(ans,j-x[a[j]]+1)
            x[a[j]]=j
        
        
            
                
            
        print(ans)
