for _ in range(int(input())):
	n=int(input())
	arr=list(map(int,input().split()))
	if n>1:
		lst=[-1]*(n+1)
		ans=n+10
		for i in range(n):
			if lst[arr[i]]!=-1:
				ans=min(ans,i-lst[arr[i]]+1)
			lst[arr[i]]=i
		if ans==n+10:
			print(-1)
		else:
			print(ans)
	else:
		print(-1)

