Problem:
Score : 500 points

Problem Statement

There are N towns numbered 1 to N and M roads. The i-th road connects Town A_i and Town B_i bidirectionally and has a length of C_i.

Takahashi will travel between these towns by car, passing through these roads. The fuel tank of his car can contain at most L liters of fuel, and one liter of fuel is consumed for each unit distance traveled. When visiting a town while traveling, he can full the tank (or choose not to do so). Travel that results in the tank becoming empty halfway on the road cannot be done.

Process the following Q queries:

The tank is now full. Find the minimum number of times he needs to full his tank while traveling from Town s_i to Town t_i. If Town t_i is unreachable, print -1.

Constraints

All values in input are integers.

2 \leq N \leq 300

0 \leq M \leq \frac{N(N-1)}{2}

1 \leq L \leq 10^9

1 \leq A_i, B_i \leq N

A_i \neq B_i

\left(A_i, B_i\right) \neq \left(A_j, B_j\right) (if i \neq j)

\left(A_i, B_i\right) \neq \left(B_j, A_j\right) (if i \neq j)

1 \leq C_i \leq 10^9

1 \leq Q \leq N\left(N-1\right)

1 \leq s_i, t_i \leq N

s_i \neq t_i

\left(s_i, t_i\right) \neq \left(s_j, t_j\right) (if i \neq j)

Input

Input is given from Standard Input in the following format:

N M L
A_1 B_1 C_1
:
A_M B_M C_M
Q
s_1 t_1
:
s_Q t_Q

Output

Print Q lines.

The i-th line should contain the minimum number of times the tank needs to be fulled while traveling from Town s_i to Town t_i. If Town t_i is unreachable, the line should contain -1 instead.

Sample Input 1

3 2 5
1 2 3
2 3 3
2
3 2
1 3

Sample Output 1

0
1

To travel from Town 3 to Town 2, we can use the second road to reach Town 2 without fueling the tank on the way.

To travel from Town 1 to Town 3, we can first use the first road to get to Town 2, full the tank, and use the second road to reach Town 3.

Sample Input 2

4 0 1
1
2 1

Sample Output 2

-1

There may be no road at all.

Sample Input 3

5 4 4
1 2 2
2 3 2
3 4 3
4 5 2
20
2 1
3 1
4 1
5 1
1 2
3 2
4 2
5 2
1 3
2 3
4 3
5 3
1 4
2 4
3 4
5 4
1 5
2 5
3 5
4 5

Sample Output 3

0
0
1
2
0
0
1
2
0
0
0
1
1
1
0
0
2
2
1
0

Solution in Julia (0.5.0) (judge verdict: Wrong Answer)

parseInt(x)=parse(Int,x)
parseMap(x::Array{SubString{String},1})=map(parseInt,x)

function hpush(h::Array{Tuple{Int,Int,Int},1},x::Tuple{Int,Int,Int})
	push!(h,x)
	if length(h) > 1
		l = length(h)
		k = div(l,2)
		while pareto(h[k],x)==1&&k>=1
			tmp = h[k]
			h[k] = x
			h[l] = tmp
			k = max(div(k,2),1)
			l = div(l,2)
		end
	end
end

function pareto(a::Tuple{Int,Int,Int},b::Tuple{Int,Int,Int})
	if a[2]>b[2] || (a[2]==b[2]&&a[3]>b[3])
		1
	elseif a[2]==b[2] && a[3]>b[3]
		0
	else
		-1
	end
end

function hpop(h::Array{Tuple{Int,Int},1})
	x = h[1]
	z = pop!(h)
	if !isempty(h)
		h[1] = z
		k = 1
		l = length(h)
		while k <= l
			if 2*k>l
				break
			elseif 2*k+1>l
				if pareto(h[2*k],z) >= 0
					break
				else
					tmp = h[2*k]
					h[2*k] = z
					h[k] = tmp
					k = 2*k
				end
			else
				if pareto(h[2*k],z[2])>=0&&pareto(h[2*k+1],z[2])>=0
					break
				else
					t = pareto(h[2*k],h[2*k+1])<0?2*k:2*k+1
					tmp = h[t]
					h[t] = z
					h[k] = tmp
					k = t
				end
			end
		end
	end
	x
end

function main()
	n,m,l = readline() |> split |> parseMap
	g = [Tuple{Int,Int}[] for i in 1:n]
	for i in 1:m
		a,b,c = readline() |> split |> parseMap
		push!(g[a],(b,c))
		push!(g[b],(a,c))
	end
	k = Array{Tuple{Int,Int},2}(n,n)
	for i in 1:n
		for j in 1:n
			k[i,j] = i==j?(0,0):(-1,-1)
		end
	end
	for r in 1:n
		stack = Tuple{Int,Int,Int}[(r,0,l)]
		while !isempty(stack)
			now = shift!(stack)
			i = now[1]
			if (k[r,i] == (-1,-1)) ||  k[r,i][1]>now[2] || (k[r,i][1]==now[2]&&k[r,i][2]>now[3])
				k[r,i] = (now[2],now[3])
			end

			for jj in 1:length(g[i])
				j = g[i][jj][1]
				x = now[2]
				y = now[3]
				y -= g[i][jj][2]
				if y<0
					x += 1
					y = l-g[i][jj][2]
				end
				if y>=0 && (k[r,j]==(-1,-1) || k[r,j][1]>x || (k[r,j][1]==x && k[r,j][2]>y))
					k[r,j] = (x,y)
					hpush(stack,(j,x,y))
				end
			end
		end
	end
	q = readline() |> parseInt
	for i in 1:q
		s,t = readline() |> split |> parseMap
		println(k[s,t][1])
	end
end

main()