from collections import deque

for _ in range(int(input())):
	n = int(input())
	a = [*map(int, input().split())]
	ans = float('inf')
	
	d = deque()
	s = set()
	for i in a:
		if i not in s:
			s.add(i)
			d.append(i)
		else:
			while len(d) and d[0] != i:
				d.popleft()
			d.append(i)
			if len(d) > 1:
				ans = min(len(d), ans)
			# print(d, len(d))
			while len(d) and d[0] == i:
				d.popleft()
	print(-1 if ans == float('inf') else ans)