t=int(input())
for _ in range(t):
    n=int(input())
    y=list(map(int,input().split()))
    arr=[-1]*(n+1)
    mina=-1
    i=0
    while i<n:
        if arr[y[i]]==-1:
            arr[y[i]]=i
        else:
            if mina==-1:
                mina=i-arr[y[i]]+1
            else:
                if mina>i-arr[y[i]]:
                    mina=i-arr[y[i]]+1
            arr[y[i]]=i
        i+=1
    
    print(mina)