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-----
3 2 5
1 2 3
2 3 3
2
3 2
1 3

-----Sample Output-----
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.

Difficulty: MEDIUM_HARD

Solutions:
(n, m, l) = map(int, input().split())
g = [[1 << 30] * n for _ in range(n)]
for _ in range(m):
	(a, b, c) = map(int, input().split())
	g[a - 1][b - 1] = c
	g[b - 1][a - 1] = c
for i in range(n):
	g[i][i] = 0

def bell(g, n):
	for k in range(n):
		for i in range(n - 1):
			for j in range(i + 1, n):
				if g[i][j] > g[i][k] + g[k][j]:
					g[i][j] = g[i][k] + g[k][j]
					g[j][i] = g[i][j]
	return g
g = bell(g, n)
for i in range(n):
	for j in range(n):
		if i == j:
			g[i][j] = 0
		elif g[i][j] <= l:
			g[i][j] = 1
		else:
			g[i][j] = 1 << 30
g = bell(g, n)
q = int(input())
for _ in range(q):
	(s, t) = map(int, input().split())
	if g[s - 1][t - 1] >> 30:
		print(-1)
	else:
		print(g[s - 1][t - 1] - 1)


(N, M, L) = map(int, input().split())
INF = 10 ** 12
dp = [[INF] * N for i in range(N)]
fp = [[INF] * N for i in range(N)]
for i in range(N):
	dp[i][i] = 0
	fp[i][i] = 0
k = [[0] * N for i in range(N)]
for i in range(M):
	(a, b, c) = map(int, input().split())
	dp[a - 1][b - 1] = c
	dp[b - 1][a - 1] = c
for i in range(N):
	for j in range(N):
		for h in range(N):
			dp[j][h] = min(dp[j][h], dp[j][i] + dp[i][h])
for j in range(N):
	for h in range(N):
		if dp[j][h] <= L:
			fp[j][h] = 1
for i in range(N):
	for j in range(N):
		for h in range(N):
			fp[j][h] = min(fp[j][h], fp[j][i] + fp[i][h])
Q = int(input())
for i in range(Q):
	(s, t) = map(int, input().split())
	s = s - 1
	t = t - 1
	if fp[s][t] == INF:
		print(-1)
	else:
		print(fp[s][t] - 1)


def warshall_floid(d):
	for k in range(1, n + 1):
		for i in range(1, n + 1):
			for j in range(1, n + 1):
				d[i][j] = min(d[i][j], d[i][k] + d[k][j])
	return d
(n, m, l) = map(int, input().split())
d = [[10 ** 12] * (n + 1) for _ in range(n + 1)]
for i in range(m):
	(a, b, c) = map(int, input().split())
	d[a][b] = c
	d[b][a] = c
d = warshall_floid(d)
for i in range(1, n + 1):
	for j in range(1, n + 1):
		if d[i][j] <= l:
			d[i][j] = 1
		else:
			d[i][j] = 10 ** 12
d = warshall_floid(d)
q = int(input())
for i in range(q):
	(s, t) = map(int, input().split())
	if d[s][t] >= 10 ** 12:
		print(-1)
	else:
		print(d[s][t] - 1)


(n, m, l) = map(int, input().split())
inf = 10000000000
g = [[inf for i in range(n)] for j in range(n)]
for i in range(m):
	(a, b, c) = map(int, input().split())
	g[a - 1][b - 1] = c
	g[b - 1][a - 1] = c
for k in range(n):
	for i in range(n):
		for j in range(n):
			g[i][j] = min(g[i][j], g[i][k] + g[k][j])
r = [[inf for i in range(n)] for j in range(n)]
for i in range(n):
	for j in range(n):
		if g[i][j] <= l:
			r[i][j] = 1
		elif g[i][j] >= inf:
			r[i][j] = inf
for k in range(n):
	for i in range(n):
		for j in range(n):
			r[i][j] = min(r[i][j], r[i][k] + r[k][j])
q = int(input())
for i in range(q):
	(s, e) = map(int, input().split())
	if r[s - 1][e - 1] >= inf:
		print(-1)
	else:
		print(r[s - 1][e - 1] - 1)


(n, m, l) = map(int, input().split())
g = [[float('inf')] * n for _ in range(n)]
for _ in range(m):
	(a, b, c) = map(int, input().split())
	a -= 1
	b -= 1
	g[a][b] = c
	g[b][a] = c
for k in range(n):
	for i in range(n):
		for j in range(n):
			g[i][j] = min(g[i][j], g[i][k] + g[k][j])
h = [[float('inf')] * n for _ in range(n)]
for i in range(n):
	for j in range(n):
		if g[i][j] <= l:
			h[i][j] = 1
for k in range(n):
	for i in range(n):
		for j in range(n):
			h[i][j] = min(h[i][j], h[i][k] + h[k][j])
Q = int(input())
for _ in range(Q):
	(s, t) = map(int, input().split())
	s -= 1
	t -= 1
	if h[s][t] == float('inf'):
		print(-1)
	else:
		print(h[s][t] - 1)


(n, m, l) = map(int, input().split())
d = [[10 ** 18] * n for i in range(n)]
for _ in range(m):
	(a, b, c) = map(int, input().split())
	if c <= l:
		d[a - 1][b - 1] = c
		d[b - 1][a - 1] = c
for i in range(n):
	d[i][i] = 0
for k in range(n):
	for i in range(n):
		for j in range(n):
			d[i][j] = min(d[i][j], d[i][k] + d[k][j])
for i in range(n):
	for j in range(n):
		if d[i][j] <= l:
			d[i][j] = 1
for k in range(n):
	for i in range(n):
		for j in range(n):
			d[i][j] = min(d[i][j], d[i][k] + d[k][j])
q = int(input())
for i in range(q):
	(s, t) = map(int, input().split())
	s -= 1
	t -= 1
	print(d[s][t] - 1 if d[s][t] - 1 < 999999999999999999 else -1)


def warshall_floyd(d):
	for k in range(n):
		for i in range(n):
			for j in range(n):
				d[i][j] = min(d[i][j], d[i][k] + d[k][j])
	return d
(n, m, l) = map(int, input().split())
d = [[10 ** 10 for _ in range(n)] for _ in range(n)]
for i in range(n):
	d[i][i] = 0
for i in range(m):
	(a, b, c) = map(int, input().split())
	d[a - 1][b - 1] = c
	d[b - 1][a - 1] = c
p = [[10 ** 5 for _ in range(n)] for _ in range(n)]
d = warshall_floyd(d)
for i in range(n):
	for j in range(n):
		if d[i][j] <= l:
			p[i][j] = 1
p = warshall_floyd(p)
q = int(input())
for i in range(q):
	(s, t) = map(int, input().split())
	if p[s - 1][t - 1] == 10 ** 5:
		print(-1)
		continue
	print(p[s - 1][t - 1] - 1)


import sys
input = sys.stdin.readline
(N, M, L) = map(int, input().split())
ABC = [list(map(int, input().split())) for _ in [0] * M]
Q = int(input())
ST = [list(map(int, input().split())) for _ in [0] * Q]
INF = (L + 1) * N
d = [[INF] * N for _ in [0] * N]
for i in range(N):
	d[i][i] = 0
for (a, b, c) in ABC:
	a -= 1
	b -= 1
	d[a][b] = c
	d[b][a] = c

def Warshall_Floyd(d, N=None):
	if N == None:
		N = len(d)
	for k in range(N):
		for i in range(N):
			for j in range(N):
				d[i][j] = min(d[i][j], d[i][k] + d[k][j])
Warshall_Floyd(d, N)
d2 = [None] * N
for (i, row) in enumerate(d):
	d2[i] = [1 if e <= L else INF for e in row]
	d2[i][i] = 0
Warshall_Floyd(d2, N)
for (s, t) in ST:
	s -= 1
	t -= 1
	ans = d2[s][t] % INF - 1
	print(ans)


def warshall_floyd(edge):
	e = edge
	for k in range(len(e)):
		for i in range(len(e)):
			for j in range(len(e)):
				e[i][j] = min(e[i][j], e[i][k] + e[k][j])
	return e
(n, m, l) = map(int, input().split())
edge = [n * [10 ** 18] for _ in range(n)]
for i in range(n):
	edge[i][i] = 0
for i in range(m):
	(a, b, c) = map(int, input().split())
	a -= 1
	b -= 1
	edge[a][b] = edge[b][a] = c
edge = warshall_floyd(edge)
edge2 = [n * [10 ** 18] for _ in range(n)]
for i in range(n):
	edge2[i][i] = 0
	for j in range(n):
		if edge[i][j] <= l:
			edge2[i][j] = 1
edge2 = warshall_floyd(edge2)
q = int(input())
for i in range(q):
	(s, t) = map(int, input().split())
	s -= 1
	t -= 1
	ans = edge2[s][t]
	if ans == 10 ** 18:
		print(-1)
	else:
		print(ans - 1)


(N, M, L) = map(int, input().split())
G = [[10 ** 15 for i in range(N)] for i in range(N)]
for i in range(M):
	(a, b, c) = map(int, input().split())
	G[a - 1][b - 1] = c
	G[b - 1][a - 1] = c
for k in range(N):
	for i in range(N):
		for j in range(N):
			G[i][j] = min(G[i][j], G[i][k] + G[k][j])
H = [[10 ** 15 for i in range(N)] for i in range(N)]
for i in range(N):
	for j in range(N):
		if G[i][j] <= L:
			H[i][j] = 1
for k in range(N):
	for i in range(N):
		for j in range(N):
			H[i][j] = min(H[i][j], H[i][k] + H[k][j])
Q = int(input())
for line in range(Q):
	(s, t) = map(int, input().split())
	s -= 1
	t -= 1
	if H[s][t] == 10 ** 15:
		print(-1)
	else:
		print(H[s][t] - 1)


import sys
input = sys.stdin.readline
(N, M, L) = map(int, input().split())

def war(d):
	for k in range(N):
		for i in range(N):
			for j in range(N):
				d[i][j] = min(d[i][j], d[i][k] + d[k][j])
	return d
INF = (L + 1) * N
d = [[INF] * N for _ in range(N)]
d2 = [[INF] * N for _ in range(N)]
for _ in range(M):
	(A, B, C) = map(int, input().split())
	if C > L:
		continue
	d[A - 1][B - 1] = C
	d[B - 1][A - 1] = C
for i in range(N):
	d[i][i] = 0
d = war(d)
for i in range(N):
	for j in range(N):
		if d[i][j] <= L:
			d2[i][j] = 1
for i in range(N):
	d2[i][i] = 0
d2 = war(d2)
Q = int(input())
for i in range(Q):
	(s, t) = map(int, input().split())
	print(d2[s - 1][t - 1] % INF - 1)


import sys
input = sys.stdin.readline
inf = 10 ** 9 + 7
(n, m, l) = map(int, input().split())
D = [[inf] * (n + 1) for _ in range(n + 1)]
for _ in range(m):
	(a, b, c) = map(int, input().split())
	D[a][b] = c
	D[b][a] = c
for k in range(1, n + 1):
	for i in range(1, n + 1):
		for j in range(1, n + 1):
			D[i][j] = min(D[i][j], D[i][k] + D[k][j])
DD = [[inf] * (n + 1) for _ in range(n + 1)]
for x in range(1, n + 1):
	for y in range(1, n + 1):
		if D[x][y] <= l:
			DD[x][y] = 1
for k in range(1, n + 1):
	for i in range(1, n + 1):
		for j in range(1, n + 1):
			DD[i][j] = min(DD[i][j], DD[i][k] + DD[k][j])
q = int(input())
for _ in range(q):
	(s, t) = map(int, input().split())
	if DD[s][t] == inf:
		print(-1)
	else:
		print(DD[s][t] - 1)


import sys
I = sys.stdin.readlines()
(N, M, L) = map(int, I[0].split())
(a, b, c) = (0, 0, 0)
D = [[L + 1] * N for i in range(N)]
for i in range(M):
	(a, b, c) = map(int, I[i + 1].split())
	(a, b) = (a - 1, b - 1)
	D[a][b] = c
	D[b][a] = c
for i in range(N):
	D[i][i] = 0
for k in range(N):
	for i in range(N):
		for j in range(N):
			D[i][j] = min(D[i][j], D[i][k] + D[k][j])
D2 = [[N * N + 2] * N for i in range(N)]
for i in range(N):
	D2[i][i] = 0
	for j in range(N):
		if D[i][j] <= L:
			D2[i][j] = 1
for k in range(N):
	for i in range(N):
		for j in range(N):
			D2[i][j] = min(D2[i][j], D2[i][k] + D2[k][j])
Q = int(I[M + 1])
for i in range(Q):
	(a, b) = map(int, I[i + 2 + M].split())
	if N < D2[a - 1][b - 1]:
		print(-1)
	else:
		print(D2[a - 1][b - 1] - 1)


(N, M, L) = map(int, input().split())
D = [[1000000000000] * N for i in range(N)]
for i in range(N):
	D[i][i] = 0
for i in range(M):
	(A, B, C) = map(int, input().split())
	(A, B) = (A - 1, B - 1)
	D[A][B] = C
	D[B][A] = C
for k in range(N):
	for i in range(N):
		for j in range(N):
			D[i][j] = min(D[i][j], D[i][k] + D[k][j])
E = [[1000000000000] * N for i in range(N)]
for i in range(N):
	E[i][i] = 0
for i in range(N):
	for j in range(N):
		if D[i][j] <= L:
			E[i][j] = 1
for k in range(N):
	for i in range(N):
		for j in range(N):
			E[i][j] = min(E[i][j], E[i][k] + E[k][j])
Q = int(input())
for i in range(Q):
	(s, t) = map(int, input().split())
	(s, t) = (s - 1, t - 1)
	if E[s][t] == 1000000000000:
		r = -1
	else:
		r = E[s][t] - 1
	print(r)


def warshall_floyd(G):
	import copy
	ret = copy.deepcopy(G)
	for k in range(N):
		for i in range(N):
			for j in range(N):
				ret[i][j] = min(ret[i][j], ret[i][k] + ret[k][j])
	return ret
INF = 10 ** 10
(N, M, L) = map(int, input().split())
G = [[INF] * N for _ in range(N)]
for i in range(M):
	(A, B, C) = map(int, input().split())
	(G[A - 1][B - 1], G[B - 1][A - 1]) = (C, C)
MinG = warshall_floyd(G)
GL = [[1 if MinG[i][j] <= L else INF for j in range(N)] for i in range(N)]
MinGL = warshall_floyd(GL)
Q = int(input())
for _ in range(Q):
	(s, t) = map(int, input().split())
	print(-1 if MinGL[s - 1][t - 1] == INF else MinGL[s - 1][t - 1] - 1)


def WarshallFloyd(M):
	N = len(M)
	for k in range(N):
		for j in range(N):
			for i in range(N):
				M[i][j] = min(M[i][j], M[i][k] + M[k][j])
	return M
INF = float('inf')
(N, M, L, *I) = map(int, open(0).read().split())
(ABC, ST) = (I[:3 * M], I[3 * M + 1:])
E = [[INF] * N for _ in range(N)]
for (a, b, c) in zip(*[iter(ABC)] * 3):
	E[a - 1][b - 1] = E[b - 1][a - 1] = c
E = WarshallFloyd(E)
for i in range(N):
	for j in range(N):
		if E[i][j] <= L:
			E[i][j] = 1
		else:
			E[i][j] = INF
E = WarshallFloyd(E)
for (s, t) in zip(*[iter(ST)] * 2):
	a = E[s - 1][t - 1] - 1
	print(-1 if a == INF else a)


import sys
imput = sys.stdin.readline

def func(d):
	for k in range(N):
		for i in range(N):
			for j in range(N):
				d[i][j] = min(d[i][j], d[i][k] + d[k][j])
	return d
(N, M, L) = map(int, input().split())
road = [[10 ** 10] * N for i in range(N)]
for i in range(N):
	road[i][i] = 0
for i in range(M):
	(a, b, c) = map(int, input().split())
	road[a - 1][b - 1] = c
	road[b - 1][a - 1] = c
road = func(road)
ans = [[10 ** 5] * N for i in range(N)]
for i in range(N):
	for j in range(N):
		if road[i][j] <= L:
			ans[i][j] = 1
ans = func(ans)
for i in range(int(input())):
	(a, b) = map(int, input().split())
	o = ans[a - 1][b - 1]
	if o == 10 ** 5:
		print(-1)
	else:
		print(o - 1)


(n, m, l) = map(int, input().split())
abe = [list(map(int, input().split())) for i in range(m)]
dist = [[10 ** 18 for i in range(n)] for i in range(n)]
for (a, b, e) in abe:
	dist[a - 1][b - 1] = e
	dist[b - 1][a - 1] = e
for k in range(n):
	for i in range(n):
		for j in range(n):
			dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j])
for i in range(n):
	for j in range(n):
		if dist[i][j] <= l:
			dist[i][j] = 1
		else:
			dist[i][j] = 10 ** 18
for k in range(n):
	for i in range(n):
		for j in range(n):
			dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j])
q = int(input())
for i in range(q):
	(s, t) = map(int, input().split())
	x = dist[s - 1][t - 1] - 1
	if x == 10 ** 18 - 1:
		print(-1)
	else:
		print(x)


(n, m, l) = map(int, input().split())
INF = 1 << 30
g = [[INF] * n for _ in range(n)]
for _ in range(m):
	(a, b, c) = map(int, input().split())
	a -= 1
	b -= 1
	g[a][b] = c
	g[b][a] = c
for k in range(n):
	for i in range(n):
		for j in range(n):
			g[i][j] = min(g[i][j], g[i][k] + g[k][j])
h = [[INF] * n for _ in range(n)]
for i in range(n):
	for j in range(n):
		if g[i][j] <= l:
			h[i][j] = 1
for k in range(n):
	for i in range(n):
		for j in range(n):
			h[i][j] = min(h[i][j], h[i][k] + h[k][j])
q = int(input())
for _ in range(q):
	(s, t) = map(lambda x: int(x) - 1, input().split())
	if h[s][t] == INF:
		print(-1)
	else:
		print(h[s][t] - 1)


(n, m, l) = map(int, input().split())
inf = 100000000000
wf = [[inf] * n for i in range(n)]
for i in range(m):
	(a, b, c) = map(int, input().split())
	wf[a - 1][b - 1] = c
	wf[b - 1][a - 1] = c
for i in range(n):
	wf[i][i] = 0
for k in range(n):
	for i in range(n):
		for j in range(n):
			wf[i][j] = min(wf[i][j], wf[i][k] + wf[k][j])
for i in range(n):
	for j in range(n):
		if i != j:
			wf[i][j] = 1 if wf[i][j] <= l else inf
for k in range(n):
	for i in range(n):
		for j in range(n):
			wf[i][j] = min(wf[i][j], wf[i][k] + wf[k][j])
q = int(input())
for i in range(q):
	(s, t) = map(int, input().split())
	x = wf[s - 1][t - 1]
	print(-1 if x == inf else wf[s - 1][t - 1] - 1)


import sys
input = sys.stdin.readline
(N, M, L) = map(int, input().split())
d = [[10 ** 16 * (i != j) for j in range(N + 1)] for i in range(N + 1)]
for _ in range(M):
	(x, y, c) = map(int, input().split())
	d[x][y] = c
	d[y][x] = c
for k in range(N + 1):
	for i in range(N + 1):
		for j in range(N + 1):
			d[i][j] = min(d[i][j], d[i][k] + d[k][j])
dd = [[(d[i][j] <= L) + (d[i][j] > L) * 10 ** 16 for j in range(N + 1)] for i in range(N + 1)]
for k in range(N + 1):
	for i in range(N + 1):
		for j in range(N + 1):
			dd[i][j] = min(dd[i][j], dd[i][k] + dd[k][j])
for _ in range(int(input())):
	(s, t) = map(int, input().split())
	if dd[s][t] != 10 ** 16:
		print(dd[s][t] - 1)
	else:
		print(-1)