'''input
3
5 3 3 2
100 33 100 1
6 0 2 3

'''

from collections import defaultdict

for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	
	idxs = defaultdict(list)

	ans = 10 ** 9
	for i, ele in enumerate(arr):
		idxs[ele].append(i)
		if len(idxs[ele]) > 1:
			ans = min(ans, idxs[ele][-1] - idxs[ele][-2] + 1)
	print(ans if ans != 10 ** 9 else -1)