from scipy.sparse.csgraph import floyd_warshall



N, M, L = list(map(int, input().split()))

E = [[0] * N for _ in range(N)]

for _ in range(M):

    a, b, c = list(map(int, input().split()))

    E[a - 1][b - 1] = E[b - 1][a - 1] = c



E = floyd_warshall(E)



for i in range(N):

    for j in range(N):

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



E = floyd_warshall(E)



INF = float("inf")



Q = int(eval(input()))

for _ in range(Q):

    s, t = list(map(int, input().split()))

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

    print((-1 if a == INF else int(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 in range(N):

    for j in range(N):

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



E = floyd_warshall(E)



INF = float("inf")



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

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

    print((-1 if a == INF else int(a)))

from scipy.sparse.csgraph import floyd_warshall

N, M, L = list(map(int, input().split()))
E = [[0] * N for _ in range(N)]
for _ in range(M):
    a, b, c = list(map(int, input().split()))
    E[a - 1][b - 1] = E[b - 1][a - 1] = c
E = floyd_warshall(E)
for i in range(N):
    for j in range(N):
        E[i][j] = E[i][j] <= L
E = floyd_warshall(E)
INF = float("inf")
Q = int(eval(input()))
for _ in range(Q):
    s, t = list(map(int, input().split()))
    a = E[s - 1][t - 1] - 1
    print((-1 if a == INF else int(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 in range(N):
    for j in range(N):
        E[i][j] = E[i][j] <= L
E = floyd_warshall(E)
INF = float("inf")
for s, t in zip(*[iter(ST)] * 2):
    a = E[s - 1][t - 1] - 1
    print((-1 if a == INF else int(a)))

-N, M, L = list(map(int, input().split())) +N, M, L, *I = list(map(int, open(0).read().split())) +ABC, ST = I[: 3 * M], I[3 * M + 1 :] -for _ in range(M): -    a, b, c = list(map(int, input().split())) +for a, b, c in zip(*[iter(ABC)] * 3): -Q = int(eval(input())) -for _ in range(Q): -    s, t = list(map(int, input().split())) +for s, t in zip(*[iter(ST)] * 2):