t=int(input())
for i in range(t):
    n=int(input())
    a=list(map(int,input().split()))
    o=[]
    d=-1
    for i in range(n):
        o.append(-1)
    for i in range(n):
        x=a[i]-1
        if(o[x]!=-1 ):
            if(d>i-o[x] or d==-1):
                d=i-o[x]+1
        o[x]=i
    print(d)