from sys import stdin
t=int(input())


while t>0:
	n=int(input())
	a=[int(i)for i in input().split()]
	if n==1:
		print(-1)

	else:	
		d={}

		for i in range(n):
			if a[i] in d:
				d[a[i]]+=1
			else:
				d[a[i]]=1

		v = list(d.values()) 
		k = list(d.keys()) 

		# print(k,v,d)
		maxi=k[v.index(max(v))]
		v.sort(reverse=True)
		f=0
		# if len(v)>1 and v[0]==v[1]: 
		# 	f=1
			# print("yo")
		# maxi= max(d, key=d.get) 
		# print(k,v,d,maxi)
		i1=0
		len_max=999999
		count=0

		for i in range(n):
			if a[i]==maxi:
				count+=1
				# print(count,a[i],i)
				if count==1:   
					i1=i
					# j1=i+1
					# count+=1
				elif count==2:
					count=1
					# print("entered")
					if len_max>i-i1+1:
						len_max=i-i1+1
						# print('len_max=',len_max)
					i1=i

		# print("lennn",len_max)			
		if len_max==0:
			print(-1)
		else:
			print(len_max)









	t-=1