for t in range(int(input())):
	n = int(input())
	l = list(map(int,input().split()))
	if n==1:
		print(-1)
	else:
		m = n+100
		a=[[] for i in range(n+1)]
		for i in range(n):
			a[l[i]].append(i)
		m = 10**18
		for i in a:
			if len(i)>=2:
				for j in  range(1,len(i)):
					m = min(m,i[j]-i[j-1]+1)
		if m>n:
			print(-1)
		else:
			print(m)
