def solve(n, a):
    m = n + 1
    d = {}
    for i in range(n):
        v = a[i]
        if v in d:
            m = min(m, i - d[v] + 1)
            if m == 2:
                break
        d[v] = i
    return [m, -1][m > n]

r = []
for _ in range(int(input())):
    n = int(input())
    a = list(map(int, input().split()))
    r.append(solve(n, a))
print(*r, sep='\n')