inf = 1e7
for i in range(int(input())):
	minimal = inf
	n = int(input())
	a = list(map(int, input().split()))
	d = dict()
	for ind, i in enumerate(a):
		if i in d:
			minimal = min(minimal, ind - d[i] + 1)
		d[i] = ind
	print(minimal if minimal != inf else -1)