from collections import defaultdict
x=int(input())
for __ in range(x):
    d=defaultdict(int)
    n=int(input())
    a=list(map(int,input().split()))
    k=1000000000001
    if len(a)>1:
        for i in range(n):
            if d[a[i]]==0:
                d[a[i]]=i+1
            else:
                k=min(k,i+1-d[a[i]])
                d[a[i]]=i+1
        if k!=1000000000001:        
            print(k+1)
        else:
            print(-1)
    else:
        print(-1)