for _ in range(int(input())):
    n = int(input())
    a = list(map(int, input().split()))

    b = dict()
    ans = float('inf')

    for i, x in enumerate(a):
        if x not in b:
            b[x] = i
            continue

        ans = min(ans, i - b[x] + 1)
        b[x] = i

    if ans > n:
        ans = -1

    print(ans)
