t=int(input())
for i in range(t):
    n=int(input())
    a=[int(i) for i in input().split()]
    c={}
    m=-1
    if n==-1:
        print(-1)
        continue
    for i in range(len(a)):
        if a[i] not in c:
            c[a[i]]=i
        else:
            k=i-c[a[i]]+1
            c[a[i]]=i
            if m==-1 or k<m: m=k
    print(m)
