T=int(input())
for jsb in range(T):
    n=int(input())
    a=[int(i) for i in input().split()]
    d,count=dict(),n 
    if(n<2):
        answer=-1
    else:
        for i in range(n):
            if a[i] not in d:
                d[a[i]]=i 
            elif a[i] in d:
                count=min(i-d[a[i]],count)
                d[a[i]]=i 
        if(len(d)==n):
            answer=-1 
        else:
            answer=count+1 
    print(answer)        