t=int(input())
while t:
    t-=1
    n=int(input())
    a = [int(x) for x in input().split()]
    if(n==1):
        print(-1)
    else:
        mx=1000000
        d={i:0 for i in range(2*n)}
        i=0
        j=0
        while(j<n):
            d[a[j]]+=1
            while(i<j and d[a[j]]==2):
                d[a[i]]-=1
                i+=1
                mx=min(mx,j-i+2)
            j+=1
        print(mx)
