
t = int(input())

for i in range(t):
	m = {}
	input()
	arr = list(map(int, input().split()))
	ans = float('inf')
	for i, e in enumerate(arr):
		if e not in m:
			m[e] = i
		else:
			ans = min(i - m[e] + 1, ans)
			m[e] = i
	if ans == float('inf'):
		ans = -1
	print (ans)
	
