def read():
    n = int(input())
    a = list(map(int, input().split()))
    return n, a


def solve(n, a):
    last: list = [-1] * (n + 1)
    m = n + 1
    for i in range(n):
        if last[a[i]] != -1:
            d = i - last[a[i]] + 1
            m = min(m, d)
        last[a[i]] = i
    return -1 if m == n + 1 else m


for t in range(int(input())):
    result = solve(*read())
    print(result)