t=int(input())
while t>0:
    t-=1
    m=9999999999
    n=int(input())
    a=[int(x) for x in input().split()]
    d=[[m,-1]for i in range(n+1)]
    for i in range(n):
        if d[a[i]][0]==m and d[a[i]][1]==-1:
            d[a[i]][1]=i
        else:
            d[a[i]][0]=min(d[a[i]][0],i-d[a[i]][1])
            d[a[i]][1]=i
    mini=m
    for i in range(n):
        mini=min(mini,d[a[i]][0])
    print(mini+1 if mini!=m else -1)    