T = int(input())

for t in range(T):
	n = int(input())
	lst = list(map(int, input().split()))

	d_min = n
	value2index = dict()

	for counter, value in enumerate(lst):

		if value in value2index:
			index = value2index[value]
			d = counter - index
			if d < d_min:
				d_min = d

		value2index[value] = counter

	if d_min == n:
		print(-1)
	else:
		print(d_min + 1)