t=int(input())
while t:
    n=int(input())
    l=list(map(int,input().split()))
    pos=[-1]*(n+1)
    mini=n+1
    cnt=0
    flag=0
    for i in range(n):
        if pos[l[i]]!=-1:
            mini=min(mini,i-pos[l[i]])
        pos[l[i]]=i    
    if mini==n+1:
        print(-1)
    else:
        print(mini+1)
    t-=1
