r = ""
for i in range(int(input())):
	n = int(input())
	s = [int(i) for i in input().split()]
	d = {}
	ans = 10**9
	for i in range(n):
		if s[i] in d:
			ans = min(i-d[s[i]]+1,ans)
			d[s[i]] = i
		else:
			d[s[i]] = i
		
	if ans>1000000:
		r+="-1\n"
	else:
		r+=str(ans)+"\n"
print(r)
