
t = int(input())
for i in range(t):
	n= int(input())
	m = [0]*200000
	o = [0]*200000
	f = 0
	ans = n
	l = [int(x) for x in input().split()]
	for j in range(n):	
		o[l[j]] +=1
	for j in range(n):
		if(o[j]>1):
			f = 1
	if(f == 0):
		print(-1)
		continue
	o = [0]*200000
	for j in range(n):
		if(o[l[j]] == 0):
			o[l[j]] = 1
		else:
			if(j-m[l[j]]<ans):
				ans = j-m[l[j]]+1
		m[l[j]] = j
	print(ans)

