import sys 
input=sys.stdin.readline
for _ in range(int(input())):
    n=int(input())
    cnt=[0]*(n+1)
    l=[int(i) for i in input().split()]
    st=0 
    mini=n+ 4 
    end=0 
    while end<n:
        cnt[l[end]]+=1 
        while cnt[l[end]]>1:
            cnt[l[st]]-=1 
            mini=min(mini,end-st+1)
            st+=1 
        end+=1 
    print(mini if mini<=n else -1)