from math import inf
t=int(input())
for _ in range(t):
	n=int(input())
	var=inf
	arr=list(map(int,input().split()))
	ls=[-inf]*(n+1)
	for i in range(n):
		var=min(i-ls[arr[i]]+1,var)
		ls[arr[i]] = i
	print(-1 if var==inf else var)