t = int(input())
for i in range(t):
    n = int(input())
    l = list(map(int, input().split()))
    last = [0] * (n + 1)
    ans = n + 1
    for j in range(n):
        ans = min(ans, j - last[l[j]] + 2) if last[l[j]] else ans
        last[l[j]] = j + 1
    if ans == n + 1:
        print(-1)
    else:
        print(ans)
