t = int(input())


for i in range(t) :
	n = int(input())
	arr = list(map(int,input().split()))
	dic = {}

	for i in arr :
		dic[i] = 0

	res = []

	for i in range(len(arr)) :
		j = arr[i]
		if dic[j]!=0 :
			res.append( ((i+1) - dic[j] + 1))
			dic[j] = i+1
		else :
			dic[j] = i + 1
		#print(dic)

	if len(res)!=0 :
		print(min(res))
	else :
		print(-1)
