# class occ(self, nex):
# 	self.count=0
# 	self.nex = nex
# 	self.prev=-1

# 	def add(i):
# 		self.count+=1
# 		self.prev=self.nex
# 		self.nex=i

t = int(input())


while t>0:
	n = int(input())
	rep = None
	rep_count = 0
	l = list(map(int, input().split()))
	if n == 1:
		print(-1)
	else:
		m = {}
		for i in range(n):
			if m.get(l[i]) is None:
				m[l[i]] = [1,i,-1]
			else:
				a = m[l[i]]
				a[0]+=1
				a[2]=a[1]
				a[1]=i
				m[l[i]] = a
				
				if not rep:
					rep = l[i]
					rep_count = m[l[i]][0]
				else:
					if m[rep][0] is None and rep_count!=m[l[i]][0]:
						rep = l[i]
						rep_count = m[l[i]][0]
					elif m[rep][0] < m[l[i]][0]:
						rep = l[i]
						rep_count = m[l[i]][0]
					elif m[rep][0] == m[l[i]][0] and rep!=l[i]:
						rep = None
		if rep:
			j = m[rep]
			print(j[1]-j[2]+1)
		else:
			print(-1)
	t-=1