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)))

def io_tmp():

    global input

    import io, sys

    f = io.StringIO(open(0).read())

    input = f.readline



io_tmp()





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.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)))

def io_tmp():
    global input
    import io, sys

    f = io.StringIO(open(0).read())
    input = f.readline


io_tmp()
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)))

+def io_tmp(): +    global input +    import io, sys + +    f = io.StringIO(open(0).read()) +    input = f.readline + + +io_tmp()