for i in range(int(input())):
    N, A, R = int(input()), [int(x) for x in input().strip().split()], False
    S, M = {c: -1 for c in set(A)}, N+1
    for j, v in enumerate(A):
        if S[v] != -1: M = min(M, j - S[v] + 1)
        S[v] = j
    print(M if M <= N else -1)
        