for _ in range(int(input())):
	n = int(input())
	a = list(map(int,input().split()))
	d = dict()
	dup = False
	mx = 1
	mx_index = -1

	if len(a)>=2:

		for i in range(len(a)):
			num = a[i]
			if num not in d:
				d[num] = [i]
			else:
				d[num].append(i)

		for i in d.keys():
			l = len(d[i])
			if l>mx:
				dup = False
				mx = l
				mx_index = i
			elif l==mx:
				dup = True

		if dup==True or mx==1:
			print(-1)
		else:
			m = d[mx_index][0+1]-d[mx_index][0]+1
			for i in range(1,mx-1):
				m = min(d[mx_index][i+1]-d[mx_index][i]+1, m)
			print(m)
	else:
		print(-1)
