import sys

readline = sys.stdin.readline
readlines = sys.stdin.readlines
ns = lambda: readline().rstrip()
ni = lambda: int(readline().rstrip())
nm = lambda: map(int, readline().split())
nl = lambda: list(map(int, readline().split()))
prn = lambda x: print(*x, sep='\n')


def solve():
    n = ni()
    a = nl()
    d = dict()
    ans = -1
    for i in range(n):
        if a[i] in d:
            if ans < 0 or ans > i - d[a[i]] + 1:
                ans = i - d[a[i]] + 1
        d[a[i]] = i
    print(ans)
    return

# solve()

T = ni()
for _ in range(T):
    solve()
