def fastio():
	import sys
	from io import StringIO 
	from atexit import register
	global input
	sys.stdin = StringIO(sys.stdin.read())
	input = lambda : sys.stdin.readline().rstrip('\r\n')
	sys.stdout = StringIO()
	register(lambda : sys.__stdout__.write(sys.stdout.getvalue()))
fastio()

MOD = 10**9 + 7
INF = 10**20
I = lambda:list(map(int,input().split()))
from collections import defaultdict as dd
from math import gcd

t, = I()
while t:
	t -= 1
	n, = I()
	l = I()
	ans = INF
	d = [[] for i in range(n+1)]
	for i in range(n):
		d[l[i]].append(i)

	for i in range(n+1):
		if len(d[i]) <= 1: 
			continue
		for j in range(1, len(d[i])):
			ans = min(ans, d[i][j] - d[i][j-1] + 1)
	if ans == INF:
		ans = -1
	print(ans)

