k = int(input())
for _ in range(k):
	n = int(input())
	a = list(map(int, input().split()))
	m = n
	if n == 1 or len(set(a)) == n:
		print(-1)
	else:
		l = dict()
		for i in range(n):
			if a[i] not in l.keys():
				l[a[i]] = [i]
			else:
				l[a[i]].append(i)
		for tab in l.values():
			for v in range(1, len(tab)):
				m = min(m, tab[v] - tab[v - 1])
		print(m + 1)