for _ in range(int(input())):
    w={}
    a=int(input())
    d=input().split()
 
    for i in range(a):
        if d[i] in w:
            w[d[i]].append(i)
        else:
            w[d[i]]=[i]
 
    a+=1
    for i in w:
        z=w[i]
        for p in range(len(z)-1):
            a=min(a,z[p+1]-z[p])
 
    if len(d)<a:
        print(-1)
    else:
        print(a+1)