q=int(input())
for i in range(q):
    n=int(input())
    l=input()
    l=[int(x) for x in l.split(' ')]
    pred={x:-1 for x in l}
    ans=n+1
    for i in range(n):
        if pred[l[i]]!=-1:
            ans=min(ans,i-pred[l[i]]+1)
        pred[l[i]]=i
    if ans!=n+1:
        print(ans)
    else:
        print(-1)