u = []

def jopa_medvedya(w, z):
	mn = len(w)
	tm = 0
	tf = False
	for c in w:
		# print('debug', c, tf, tm)
		if not tf:
			if c == z:
				tf = True
		else:
			# tf is True
			if c == z:
				mn = min(mn, tm)
				tm = 0
			else:
				tm += 1
	mn = min(mn, tm) if tm != 0 else mn
	return mn


for _ in range(int(input())):
	n = int(input())
	a = [int(x) for x in input().split()]

	d = {i: a.count(i) for i in set(a)}
	s = [(k, d[k]) for k in sorted(d, key=d.get, reverse=True)]
	# print(s)
	if s[0][1] < 2:
		u.append(-1)
		continue
	j = 0
	try:
		while s[j][1] == s[j + 1][1]:
			j += 1
	except IndexError:
		pass
	j += 1
	# print(j)

	r = 999999999
	for y in range(j):
		r = min(r, jopa_medvedya(a, s[y][0]) + 2)
		# print(''.join(str(c) for c in a).split(str(s[y][0])))
		# f = [len(x) for x in ''.join(str(c) for c in a).split(str(s[y][0]))]
		# if 
		# r = min(r, min())
	u.append(r)

print('\n'.join(str(v) for v in u))



