from collections import defaultdict as dd
for _ in range(int(input())):
    n=int(input())
    arr=list(map(int,input().split()))
    d=dd(list)
    for i in range(len(arr)):
        d[arr[i]]+=[i]
    ans=99999999999999999
    for k in d:
        
        for i in range(1,len(d[k])):
            ans=min(d[k][i]-d[k][i-1]+1,ans)
   
    if(ans==99999999999999999):
        print(-1)
    else:
        print(ans)
    
             