t=int(input())
for _ in range(t):
    n=int(input())
    l=list(map(int,input().split()))
    d={}
    ans=1000000000
    for i in range(n):
        if l[i] not in d:
            d[l[i]]=i
        else:
            if ans>i-d[l[i]]+1:
                ans=i-d[l[i]]+1
            d[l[i]]=i
    if ans==1000000000:
        print(-1)
    else:
        print(ans)