t = int(input())
while t>0:
    n = int(input())
    a = list(map(int,input().split(' ')))
    l=0
    occ = {}
    minlen = n+1
    for r in range(n):
        if a[r] in occ.keys():
            occ[a[r]]+=1
            while occ[a[r]]>1:
                minlen = min(r-l+1,minlen)
                occ[a[l]]-=1
                l+=1
        else:
            occ[a[r]]=1
    if minlen<n+1:
        print(minlen)    
    else:
        print(-1)
    t-=1