t=int(input())
while t!=0:
	n=int(input())
	a=list(map(int,input().split()))
	fre=[-1]*(n+1)
	ans=1000000
	for i in range(n):
		if fre[a[i]]!=-1:
			ans=min(ans,i-fre[a[i]]+1)
		fre[a[i]]=i
	if(ans==1000000):
		ans=-1
	print(ans)
	t-=1