for i in range(int(input())):
	input()
	array = list(map(int, input().split()))
	search = {e: -1 for e in array}
	size = 1e9
	for i, e in enumerate(array):
		if e in search:
			if search[e] != -1:
				size = min(size, i - search[e])
			search[e] = i
	print(-1 if size == 1e9 else size + 1)