"""in the name of almighty Allah let's proceed"""
f = lambda: map(int, input().split())
t=int(input())
for _ in range(t):
    n=int(input())
    m=n+1
    A=list(f())
    S=set()
    last={}
    leng={}
    for i in range(n):
        if A[i] not in S:
            last[A[i]]=i
            leng[A[i]]=1
            S.add(A[i])
        else:
            if leng[A[i]]==1:
                leng[A[i]]=i-last[A[i]]+1
                last[A[i]]=i
                continue
            leng[A[i]]=min(leng[A[i]],i-last[A[i]]+1)
            last[A[i]]=i
    for i in leng:
        if leng[i]!=1 and leng[i]<m:
            m=leng[i]
    print(-1 if m==n+1 else m)

