for _ in range(int(input())):
	n = int(input())
	a = [int(__) for __ in input().split()]
	
	if n >= 2:
		d = {}

		ans = float('inf')

		for i in range(n):
			if a[i] in d:
				ans = min(i - d[a[i]], ans)
			d[a[i]] = i
		
		if ans == float('inf'):
			print(-1)
		else:
			print(ans+1)
	else:
		print(-1)