for _ in range(int(input())):
	n=int(input())
	a=list(map(int,input().split()))
	occ=[-1]*(n+1)
	m=200001
	x,y=0,0
	for i in range(n):
		if occ[a[i]]!=-1:
			m=min(m,i-occ[a[i]])
		occ[a[i]]=i
	if m<200001:
		print(m+1)
	else:
		print(-1)