t = int(input())

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

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

		else:
			j += 1

	if min_len == 100010:
		print(-1)

	else:
		print(min_len)