from scipy.sparse import csr_matrix

from scipy.sparse.csgraph import floyd_warshall

import numpy as np





def create_graph(N, edge_pairs, is_bi_dir=False):

    in_vs, out_vs, weights = [], [], []

    for s, t, w in edge_pairs:

        in_vs.append(s)

        out_vs.append(t)

        weights.append(w)

        if is_bi_dir:

            in_vs.append(t)

            out_vs.append(s)

            weights.append(w)

    return csr_matrix((weights, (in_vs, out_vs)), shape=(N, N), dtype=np.int64)





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

edge_pairs = []

for _ in range(M):

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

    edge_pairs.append((s-1, t-1, w))

graph = create_graph(N, edge_pairs, True)

dist = floyd_warshall(graph, directed=False)

edge_pairs = []

for i in range(N):

    for j in range(i+1, N):

        if dist[i][j] <= L:

            edge_pairs.append((i, j, 1))

graph = create_graph(N, edge_pairs, True)

dist = floyd_warshall(graph, directed=False)



Q = int(eval(input()))

for _ in range(Q):

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

    ans = dist[s-1][t-1]

    if np.isinf(ans):

        print((-1))

    else:

        print((int(ans - 1)))

from scipy.sparse.csgraph import floyd_warshall





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

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

for _ in range(M):

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

    graph[s-1][t-1] = graph[t-1][s-1] = w

dist = floyd_warshall(graph, directed=False)

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

for i in range(N):

    for j in range(i+1, N):

        if dist[i][j] <= L:

            graph[i][j] = graph[i][j] = 1

dist = floyd_warshall(graph, directed=False)



Q = int(eval(input()))

for _ in range(Q):

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

    ans = dist[s-1][t-1]

    if ans == float("inf"):

        print((-1))

    else:

        print((int(ans - 1)))

from scipy.sparse import csr_matrix
from scipy.sparse.csgraph import floyd_warshall
import numpy as np


def create_graph(N, edge_pairs, is_bi_dir=False):
    in_vs, out_vs, weights = [], [], []
    for s, t, w in edge_pairs:
        in_vs.append(s)
        out_vs.append(t)
        weights.append(w)
        if is_bi_dir:
            in_vs.append(t)
            out_vs.append(s)
            weights.append(w)
    return csr_matrix((weights, (in_vs, out_vs)), shape=(N, N), dtype=np.int64)


N, M, L = list(map(int, input().split()))
edge_pairs = []
for _ in range(M):
    s, t, w = list(map(int, input().split()))
    edge_pairs.append((s - 1, t - 1, w))
graph = create_graph(N, edge_pairs, True)
dist = floyd_warshall(graph, directed=False)
edge_pairs = []
for i in range(N):
    for j in range(i + 1, N):
        if dist[i][j] <= L:
            edge_pairs.append((i, j, 1))
graph = create_graph(N, edge_pairs, True)
dist = floyd_warshall(graph, directed=False)
Q = int(eval(input()))
for _ in range(Q):
    s, t = list(map(int, input().split()))
    ans = dist[s - 1][t - 1]
    if np.isinf(ans):
        print((-1))
    else:
        print((int(ans - 1)))

from scipy.sparse.csgraph import floyd_warshall

N, M, L = list(map(int, input().split()))
graph = [[0] * N for _ in range(N)]
for _ in range(M):
    s, t, w = list(map(int, input().split()))
    graph[s - 1][t - 1] = graph[t - 1][s - 1] = w
dist = floyd_warshall(graph, directed=False)
graph = [[0] * N for _ in range(N)]
for i in range(N):
    for j in range(i + 1, N):
        if dist[i][j] <= L:
            graph[i][j] = graph[i][j] = 1
dist = floyd_warshall(graph, directed=False)
Q = int(eval(input()))
for _ in range(Q):
    s, t = list(map(int, input().split()))
    ans = dist[s - 1][t - 1]
    if ans == float("inf"):
        print((-1))
    else:
        print((int(ans - 1)))

-from scipy.sparse import csr_matrix -import numpy as np - - -def create_graph(N, edge_pairs, is_bi_dir=False): -    in_vs, out_vs, weights = [], [], [] -    for s, t, w in edge_pairs: -        in_vs.append(s) -        out_vs.append(t) -        weights.append(w) -        if is_bi_dir: -            in_vs.append(t) -            out_vs.append(s) -            weights.append(w) -    return csr_matrix((weights, (in_vs, out_vs)), shape=(N, N), dtype=np.int64) - -edge_pairs = [] +graph = [[0] * N for _ in range(N)] -    edge_pairs.append((s - 1, t - 1, w)) -graph = create_graph(N, edge_pairs, True) +    graph[s - 1][t - 1] = graph[t - 1][s - 1] = w -edge_pairs = [] +graph = [[0] * N for _ in range(N)] -            edge_pairs.append((i, j, 1)) -graph = create_graph(N, edge_pairs, True) +            graph[i][j] = graph[i][j] = 1 -    if np.isinf(ans): +    if ans == float("inf"):