from scipy.sparse.csgraph import csgraph_from_dense
from scipy.sparse.csgraph import floyd_warshall

n, m, energy = map(int, input().split())
edges = [[float('INF')]*n for _ in range(n)]

for i in range(m):
    a, b, c = map(int, input().split())
    edges[a-1][b-1] = c
    edges[b-1][a-1] = c

G = csgraph_from_dense(edges, null_value=float('INF'))
dist = floyd_warshall(G)

L_edges = [[float('INF')]*n for _ in range(n)]
for i in range(n-1):
    for j in range(i, n):
        if dist[i][j] <= energy:
            L_edges[i][j] = 1
            L_edges[j][i] = 1

G = csgraph_from_dense(L_edges, null_value=float('INF'))
answers = floyd_warshall(G)

Q = int(input())
for i in range(Q):
    s, t = map(int, input().split())
    ans = answers[s-1][t-1] - 1
    if ans == float('INF'):
        ans = -1
    print(int(ans)) from scipy.sparse.csgraph import csgraph_from_dense from scipy.sparse.csgraph import floyd_warshall n, m, energy = map(int, input().split()) n map(int, input().split()) map int input().split() ().split () input split m energy [float('INF')]*n for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) [float('INF')]*n [float('INF')] float('INF') float 'INF' n edges = [[float('INF')]*n for _ in range(n)] edges [[float('INF')]*n for _ in range(n)] for i in range(m):
    a, b, c = map(int, input().split())
    edges[a-1][b-1] = c
    edges[b-1][a-1] = c i range(m) range m a, b, c = map(int, input().split()) a map(int, input().split()) map int input().split() ().split () input split b c edges[a-1][b-1] = c edges[a-1][b-1] [a-1] edges a-1 a 1 b-1 b 1 c edges[b-1][a-1] = c edges[b-1][a-1] [b-1] edges b-1 b 1 a-1 a 1 c G = csgraph_from_dense(edges, null_value=float('INF')) G csgraph_from_dense(edges, null_value=float('INF')) csgraph_from_dense edges null_value=float('INF') null_value float('INF') float 'INF' dist = floyd_warshall(G) dist floyd_warshall(G) floyd_warshall G [float('INF')]*n for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) [float('INF')]*n [float('INF')] float('INF') float 'INF' n L_edges = [[float('INF')]*n for _ in range(n)] L_edges [[float('INF')]*n for _ in range(n)] for i in range(n-1):
    for j in range(i, n):
        if dist[i][j] <= energy:
            L_edges[i][j] = 1
            L_edges[j][i] = 1 i range(n-1) range n-1 n 1 for j in range(i, n):
        if dist[i][j] <= energy:
            L_edges[i][j] = 1
            L_edges[j][i] = 1 j range(i, n) range i n if dist[i][j] <= energy:
            L_edges[i][j] = 1
            L_edges[j][i] = 1 dist[i][j] <= energy dist[i][j] [i] dist i j energy L_edges[i][j] = 1 L_edges[i][j] [i] L_edges i j 1 L_edges[j][i] = 1 L_edges[j][i] [j] L_edges j i 1 G = csgraph_from_dense(L_edges, null_value=float('INF')) G csgraph_from_dense(L_edges, null_value=float('INF')) csgraph_from_dense L_edges null_value=float('INF') null_value float('INF') float 'INF' answers = floyd_warshall(G) answers floyd_warshall(G) floyd_warshall G Q = int(input()) Q int(input()) int input() input for i in range(Q):
    s, t = map(int, input().split())
    ans = answers[s-1][t-1] - 1
    if ans == float('INF'):
        ans = -1
    print(int(ans)) i range(Q) range Q s, t = map(int, input().split()) s map(int, input().split()) map int input().split() ().split () input split t ans = answers[s-1][t-1] - 1 ans answers[s-1][t-1] - 1 answers[s-1][t-1] [s-1] answers s-1 s 1 t-1 t 1 1 if ans == float('INF'):
        ans = -1
     ans == float('INF') ans float('INF') float 'INF' ans = -1 ans -1 print(int(ans)) print int(ans) int ans edges = [[float('INF')]*n for _ in range(n)] [[float('INF')]*n for _ in range(n)] edges G = csgraph_from_dense(L_edges, null_value=float('INF')) csgraph_from_dense(L_edges, null_value=float('INF')) G ans = answers[s-1][t-1] - 1 answers[s-1][t-1] - 1 ans c = map(int, input().split()) map(int, input().split()) c t = map(int, input().split()) map(int, input().split()) t n, m, energy = map(int, input().split()) map(int, input().split()) n G = csgraph_from_dense(edges, null_value=float('INF')) csgraph_from_dense(edges, null_value=float('INF')) G s, t = map(int, input().split()) map(int, input().split()) s dist = floyd_warshall(G) floyd_warshall(G) dist b, c = map(int, input().split()) map(int, input().split()) b Q = int(input()) int(input()) Q m, energy = map(int, input().split()) map(int, input().split()) m L_edges = [[float('INF')]*n for _ in range(n)] [[float('INF')]*n for _ in range(n)] L_edges a, b, c = map(int, input().split()) map(int, input().split()) a answers = floyd_warshall(G) floyd_warshall(G) answers energy = map(int, input().split()) map(int, input().split()) energy ans = -1 -1 ans
from scipy.sparse.csgraph import csgraph_from_dense
from scipy.sparse.csgraph import floyd_warshall

n, m, energy = map(int, input().split())
edges = [[float('INF')]*n for _ in range(n)]

for i in range(m):
    a, b, c = map(int, input().split())
    edges[a-1][b-1] = c
    edges[b-1][a-1] = c

G = csgraph_from_dense(edges, null_value=float('INF'))
dist = floyd_warshall(G)

L_edges = [[float('INF')]*n for _ in range(n)]
for i in range(n-1):
    for j in range(i, n):
        if dist[i][j] <= energy:
            L_edges[i][j] = 1
            L_edges[j][i] = 1

G = csgraph_from_dense(L_edges, null_value=float('INF'))
answers = floyd_warshall(G)

Q = int(input())
for i in range(Q):
    s, t = map(int, input().split())
    ans = answers[s-1][t-1] - 1
    if ans == float('INF'):
        ans = -1
    print(int(ans))