t=int(input())
for q in range(t):
	n=int(input())
	li = list(map(int,input().split(" ",n)[:n]))
	hs = [-1]*(max(li)+1)
	mi=1000000000000
	for i in range(n):
		if hs[li[i]]!=-1:
			if i-hs[li[i]]+1>=2 and i-hs[li[i]]+1<mi:
				mi = i-hs[li[i]]+1
		hs[li[i]]=i
	if mi<10000000000:
		print(mi)
	else:
		print(-1)
