def check(n, a):
    lst = [-1] * (n + 1)
    result = n + 5
    for i in range(n):
        if lst[a[i]] != - 1:
            result = min(result, i - lst[a[i]] + 1)
        lst[a[i]] = i
    if result > n:
        result = - 1
    return result


for _ in range(int(input())):
    m = int(input())
    b = [int(j) for j in input().split()]
    print(check(m, b))