def dominated(n,l):
    p={}
    m=n+1
    for i in range(n):
        if l[i] in p.keys():
            m=min(m,i-p[l[i]]+1)
        p[l[i]]=i
    return m if m!=n+1 else -1
 
 
for _ in range(int(input())):
    n=int(input())
    l=list(map(int,input().split()))
    print(dominated(n,l))