from math import inf

for t in range(int(input())):
    n = int(input())
    a = list(map(int, input().split()))
    lasts = [-inf] * (n + 1)
    res = inf
    for i in range(n):
        value = a[i]
        res = min(res, i - lasts[value] + 1)
        lasts[value] = i
    print(-1 if res == inf else res)
