from scipy.sparse.csgraph import floyd_warshall



N, M, L, *I = list(map(int, open(0).read().split()))

ABC, ST = I[:3 * M], I[3 * M + 1:]



E = [[0] * 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 = floyd_warshall(E)



for i, ei in enumerate(E):

    for j, eij in enumerate(ei):

        E[i][j] = (eij <= L)



E = floyd_warshall(E)



INF = float("inf")



A = []

for s, t in zip(*[iter(ST)] * 2):

    a = E[s - 1][t - 1] - 1

    A.append("-1" if a == INF else str(int(a)))



print(("\n".join(A)))
from scipy.sparse.csgraph import floyd_warshall



N, M, L, *I = list(map(int, open(0).read().split()))

ABC, ST = I[:3 * M], I[3 * M + 1:]



E = [[0] * 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 = floyd_warshall(E)

E = [[(eij <= L) for j, eij in enumerate(ei)] for i, ei in enumerate(E)]

E = floyd_warshall(E)



INF = float("inf")



A = []

for s, t in zip(*[iter(ST)] * 2):

    a = E[s - 1][t - 1] - 1

    A.append("-1" if a == INF else str(int(a)))



print(("\n".join(A)))

from scipy.sparse.csgraph import floyd_warshall

N, M, L, *I = list(map(int, open(0).read().split()))
ABC, ST = I[: 3 * M], I[3 * M + 1 :]
E = [[0] * 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 = floyd_warshall(E)
for i, ei in enumerate(E):
    for j, eij in enumerate(ei):
        E[i][j] = eij <= L
E = floyd_warshall(E)
INF = float("inf")
A = []
for s, t in zip(*[iter(ST)] * 2):
    a = E[s - 1][t - 1] - 1
    A.append("-1" if a == INF else str(int(a)))
print(("\n".join(A)))

from scipy.sparse.csgraph import floyd_warshall

N, M, L, *I = list(map(int, open(0).read().split()))
ABC, ST = I[: 3 * M], I[3 * M + 1 :]
E = [[0] * 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 = floyd_warshall(E)
E = [[(eij <= L) for j, eij in enumerate(ei)] for i, ei in enumerate(E)]
E = floyd_warshall(E)
INF = float("inf")
A = []
for s, t in zip(*[iter(ST)] * 2):
    a = E[s - 1][t - 1] - 1
    A.append("-1" if a == INF else str(int(a)))
print(("\n".join(A)))

-for i, ei in enumerate(E): -    for j, eij in enumerate(ei): -        E[i][j] = eij <= L +E = [[(eij <= L) for j, eij in enumerate(ei)] for i, ei in enumerate(E)]