T=int(input())
for t in range(T):
    n=int(input())
    arr=list(map(int,input().split()))
    d={}
    for i in arr:
        d[i]=-1
    ans=10**6

    for i in range(n):   
        if(d[arr[i]]==-1):
            d[arr[i]]=i
        else:
            a=i-d[arr[i]]+1
            ans=min(ans,a)
            d[arr[i]]=i
    
    if(ans==10**6):
        print(-1)
        continue
    print(ans)
            
        
        