t=int(input())
for ij in range(0,t):
    ans=[]
    d={}
    n=int(input())
    l=list(map(int,input().split()))
    for i in range(0,n):
        if l[i] in d:
            ans.append(i-d[l[i]]+1)
        d[l[i]]=i
    if ans!=[]:
        print(min(ans))
    else:
        print(-1)
