t = int(input())
for I in range(t):
	n = int(input())
	a = [int(i) for i in input().split(" ")]
	last = [-1] * (n+1)
	ans = 10**6
	for i in range(n):
		if last[a[i]] == -1:
			last[a[i]] = i
		elif last[a[i]] >= 0:
			ans = min(ans, i - last[a[i]]+1)
			last[a[i]] = i
	if ans == 10**6:
		ans = -1
	print(ans)