t = int(input())
while t:
    t -= 1
    n, a, res = int(input()), [int(i) for i in input().split()], 300000
    ls = [-1] * (n + 1)
    resi = -1
    for i in range(n):
        if ls[a[i]] != -1 and resi < ls[a[i]]:
            res = min(i - ls[a[i]] + 1, res)
        ls[a[i]] = i
    if res > n: res = -1
    print(res)
