t=int(input())
for i in range(t):
    n=int(input())
    a=list(map(int,input().split()))
    min_r=10**9;f=[]
    for j in range(n+1):f.append([])
    for j in range(len(a)):
        f[a[j]].append(j)
    for j in range(len(f)):
        if len(f[j])>1:
            k=f[j];r=10**9
            for u in range(len(k)-1):
                r=min(r,k[u+1]-k[u]+1)
            min_r=min(min_r,r)
    if min_r==10**9:print(-1)
    else:
        print(min_r)