for _ in range(input()):

	n = input()
	l = map(int, raw_input().split())
	dic = {}
	left = 0
	right = 0
	ans = -1
	while right < n:
		#print dic
		if len(dic) == (right - left):
			if l[right] in dic:
				dic[l[right]] += 1
			else:
				dic[l[right]] = 1
			right += 1
		else:
			if ans == -1:
				ans = right - left
			else:
				ans = min(ans, right - left)
			dic[l[left]] -= 1
			if dic[l[left]] == 0:
				del dic[l[left]]
			left += 1
	if len(dic) < right - left:
		if ans == -1:
			ans = right - left
		else:
			ans = min(ans, right - left)
	print ans