k = int(input())
for _ in range(k):
	n = int(input())
	a = list(map(int, input().split()))
	m = n
	if n == 1 or len(set(a)) == n:
		print(-1)
	else:
		i = 1
		cp = list(a)
		f = 0
		while i < n:
			if a[i] in a[:i]:
				m = min(m, i - f)
				f = i - 1
			i += 1
		print(m + 1)