T = int(input())
answer = ""
inf = float('inf')

def min_delta(Arr:list):
	
	ret = float('inf')

	for i in range(0, len(Arr)-1):
		delta = Arr[i+1] - Arr[i] + 1
		ret = min(ret, delta)

	return ret

for t in range(T):

	n = int(input())

	arr = list(map(int, input().split()))

	indexes = dict()
	for i in range(n):
		if indexes.get(arr[i]):
			indexes[arr[i]].append(i)
		else:
			indexes[arr[i]] = [i]

	result = inf

	for key in indexes:
		if len(indexes[key]) > 1:
			result = min(min_delta(indexes[key]), result)

	if result == inf:
		result = -1

	answer += str(result) + '\n'


print(answer)