for _ in range(int(input())):
	n = int(input())
	x = list(map(int, input().split()))
	# print(n)
	# print(x)
	i_found,j_found=0,n-1
	for i in range(n):
		for j in range(n):
			if j != i and x[i]==x[j]:
				if abs(j-i)<abs(j_found-i_found):
					j_found = j
					i_found = i
	if (i_found == j_found):
		print(-1)
	else:
		print(abs(i_found-j_found)+1)