t=int(input())
for x in range(t):
	n=int(input())
	ar=list(map(int,input().split()))
	mx=n+1000
	kol=[-1]*(n+3)
	for i in range(n):
		if kol[ar[i]]!=-1:
			mx=min(mx,abs(i-kol[ar[i]]+1))
		kol[ar[i]]=i
	if mx!=n+1000:
		print(mx)
	else:
		print(-1)
