T = int(input())
while(T>0):
	N = int(input())
	dict = {}
	arr = [int(x) for x in input().split()]
	min = N+1
	i,j=[0,0]
	flag = False
	num = 0
	
	while(j<N):
		if(flag==False):
			if(arr[j] not in dict):
				dict[arr[j]] = 1
			elif(dict[arr[j]]<1):
				dict[arr[j]]+=1
			else:
				dict[arr[j]] += 1
				flag = True
				num = arr[j]
			j+=1
		else:
			if(arr[i]==num):
				flag = False
				num = 0
				if(min>j-i):
					min=j-i
			dict[arr[i]]-=1
			i+=1
	while(i<N):
		dict[arr[i]]-=1
		if(arr[i]==num):
			if(min>j-i):
				min=j-i
			break
		i+=1
	print(min if min<N+1 else -1)
	T-=1