
for _ in range(int(input())):
    n=int(input())
    a=list(map(int,input().split()))
    ind=[0]*(n+1)
    m=10**12
    for i in range(1,n+1):
        if ind[a[i-1]]==0:
            ind[a[i-1]]=i
        else:    
            # print(ind[a[i]])
            m=min(m,i- ind[a[i-1]]+1)
            ind[a[i-1]]=i
    if m==10**12:
        print(-1)
    else:    
        print(m)    