T = int(input())
C = []
for e in range(T):
    n = int(input())
    A = list(map(int,input().split()))

    D = {}
    flag = 0
    x = len(A)
    for i in range(len(A)):
        if A[i] not in D:
            D[A[i]] = i
        else:
            x = min(x,i+1 - D[A[i]])
            D[A[i]] = i
            flag += 1
    if flag == 0:
        C.append(-1)
    else:
        C.append(x)

for i in range(len(C)):
    print(C[i])