t = int(input())
result = []
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	if n > 1:
		#creating dict
		d = dict()
		for el in a:
			if el in d:
				d[el] += 1
			else:
				d[el] = 1

		#finding v
		max_val = 0
		max_key = -1
		flag = False

		for i in d.keys():
			if d[i] >= max_val:
				if d[i] == max_val:
					flag = True
				else:
					flag = False
					max_val = d[i]
					max_key = i

		if flag == False:
			min_len = n
			first = a.index(max_key)
			second = first + 1

			while second < n:
				if a[second] == max_key:
					if second - first < min_len:
						min_len = second - first
					first = second
				

				second += 1
			result.append(min_len + 1)


		else:
			result.append(-1)
	else:
		result.append(-1)


for i in range(t):
	print(result[i])