for _ in range(int(input())):
	n = int(input())
	a = [*map(int, input().split())]
	ans = float('inf')
	ind = {}

	for i, j in enumerate(a):
		if j not in ind:
			ind[j] = [i]
		else:
			ind[j].append(i)

	for i in ind.values():
		if len(i) >= 2:
			ans = min(ans, min([y - x + 1 for y, x in zip(i[1:], i[:-1])]))

	print(-1 if ans == float('inf') else ans)