for _ in range(int(input())):
    n = int(input())
    A = list(map(int,input().split()))
    D = set(A)
    if(len(D)==n or n==1):
        print("-1")
        continue
    ans = 1000000
    pos = [0 for i in range(max(A)+1)]
    for i in range(n):
        if(pos[A[i]]>0):
            ans = min(ans,i-pos[A[i]]+2)
        pos[A[i]]=i+1
    print(ans)
