t = int(input())

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

	i = 0
	min_len = 100010
	sub = set()
	while i < n:
		sub_len = len(sub)
		sub.add(nums[i])
		if len(sub) == sub_len:
			min_len = min(min_len, sub_len + 1)
			sub = set()
			sub.add(nums[i])

		i += 1

	if min_len == 100010:
		print(-1)

	else:
		print(min_len)