T=int(input())
for i in range(T):
    n=int(input())
    A=[int(j) for j in input().split()]
    maxx=max(A)
    B=[-1]*(maxx+1)
    minn=1000000
    tinn=1000000
    for i in range(len(A)):
        if B[A[i]]>-1:
            if minn>(i-B[A[i]]):
                minn=i-B[A[i]]
        B[A[i]]=i
    minn=minn+1
    if minn>tinn:
        print(-1)
    else:
        print(minn)
