T=int(input())
for t in range(T):
    n=int(input())
    arr=list(map(int,input().split()))
    d={i:[] for i in range(1,n+1)}
    for i in range(n):
        d[arr[i]].append(i)
    rec=[]
    for i in d:
        if(len(d[i])>1):
            rec.append(d[i])
    ans=10**6
    for i in range(len(rec)):
        for j in range(len(rec[i])-1):
            diff=rec[i][j+1]-rec[i][j]
            ans=min(ans,diff+1)
            
    
    if(ans==10**6):
        print(-1)
        continue
    print(ans)
            
        
        