t=int(input())
for you in range(t):
    n=int(input())
    l=input().split()
    li=[int(i) for i in l]
    hashi=dict()
    maxa=[]
    for i in range(n):
        if(li[i] in hashi):
            maxa.append(i-hashi[li[i]]+1)
        hashi[li[i]]=i
    if(maxa==[]):
        print(-1)
    else:
        print(min(maxa))
