for _ in range(int(input())):
	n=int(input())
	l=list(map(int,input().split()))
	d={}
	k=[]
	if len(set(l))==n:
		print(-1)
	else:
		for i in range(n):
			if l[i] in d:
				k.append(i-d[l[i]][-1]+1)
				d[l[i]]+=[i]
			else:
				d[l[i]]=[i]
		print(min(k))