T = int(input())
for _ in range(T):
	n = int(input())
	infinity = n+1
	A = [int(v) for v in input().split()]
	
	last = {}
	ans = infinity
	for i in range(len(A)):
		v = A[i]
		if v in last:
			ans = min(ans, 1+i-last[v])
		last[v] = i
	
	if ans == infinity:
		ans = -1
	
	print(ans)