t= int(input())
for i in range(t):
    n= int(input())
    l= list(map(int,input().split()))
    d={}
    res=n+1
    a=False
    for i in range(n):
        d[l[i]]=[]
    for i in range(n):
        d[l[i]].append(i)
        if len(d[l[i]])>=2:
            res=min(res,d[l[i]][-1]-d[l[i]][-2]+1)
            a=True
    if a:
        print(res)
    else:
        print(-1)
 
        