t = int(raw_input())

for k in xrange(t):
	n = int(raw_input())
	ar = map(int, raw_input().split())
	
	mini = 56488964895
	
	freq = {}
	
	if n == 1:
		print -1
		continue
	i = 0
	j = 1
	at = 2
	freq[ar[i]] = 1
	
	
	while j < n and j - i >= 1:
		if not ar[j] in freq:
			freq[ar[j]] = 0
		freq[ar[j]] += 1
		#print mini
		#print freq
		
		
		if freq[ar[j]] == 2:
			mini = min(mini, j - i + 1)
			i += 1
			freq[ar[i-1]] -= 1
			freq[ar[j]] -= 1
		else:
			j += 1
			
			
	if mini == 56488964895:
		print -1
	else:
		print mini
