t = int(input())

for _ in range(t):

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

	x,y = {},[]

	for i in range(a):
		if arr[i] in x:
			y.append(i-x[arr[i]])
			x[arr[i]] = i

		else:
			x[arr[i]] = i

	if len(y) == 0:
		print(-1)
	else:
		print(min(y)+1)


