t=int(input())
for _ in range(t):
    n=int(input())
    l=list(map(int,input().split()))
    d={}
    ans=float('inf')
    for i,j in enumerate(l):
        if j not in d:
            d[j]=i
        else:
            ans=min(ans,i-d[j]+1)
            d[j]=i
    if(ans==float('inf')):
        print('-1')
    else:
        print(ans)