def run_testcase():
	n = int(input())

	ns = [int(x) for x in input().split()]

	last_index = [None] * (n + 1)
	# smallest_pair_distance = None
	smallest_pair_distance = float('inf')

	for i, v in enumerate(ns):
		if last_index[v] == None:
			last_index[v] = i
			continue

		last_distance = i - last_index[v]
		if last_distance < smallest_pair_distance:
			smallest_pair_distance = last_distance
		last_index[v] = i


	return smallest_pair_distance + 1 if smallest_pair_distance != float('inf') else -1




testcase_count = int(input())

for i in range(testcase_count):
	print(str(run_testcase()))
	# run_testcase()
# run_testcase()
# print(str(run_testcase()))
