def solve(n, arr):
    ans = n + 5
    lst = dict()

    for i, x in enumerate(arr):
        if lst.get(x) is not None:
            ans = min(ans, i - lst[x] + 1)
        lst[x] = i

    if ans > n:
        print(-1)
    else:
        print(ans)


t = int(input())

for _ in range(t):
    n = int(input())
    arr = list(map(int, input().rstrip().split()))
    solve(n, arr)
