from math import inf
t = int(input())
for tc in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	lasts = [-inf] * (n+1)
	mn = inf
	for i in range(n):
		mn = min(mn, i-lasts[a[i]]+1)
		lasts[a[i]] = i
	print(-1 if mn==inf else mn)
