from sys import stdin
from collections import Counter


def solve():
    ans, cur, most, be = float('inf'), 0, Counter(a).most_common(1)[0][0], [-1] * (n + 1)

    for i in range(n):
        if be[a[i]] != -1:
            ans = min(ans, i - be[a[i]] + 1)
        be[a[i]] = i

    return str(ans)


out = []
for i in range(int(stdin.readline())):
    n, a = int(stdin.readline()), list(map(int, stdin.readline().split()))
    out.append(str(-1) if n == 1 else solve())

print('\n'.join(out))
