def e_travel_by_car(INF=float('inf')):

    from copy import deepcopy

    from scipy.sparse.csgraph import floyd_warshall

    N, M, L = [int(i) for i in input().split()]

    Roads = [[int(i) for i in input().split()] for j in range(M)]

    Q = int(eval(input()))

    Queries = [[int(i) for i in input().split()] for j in range(Q)]



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

    for k in range(N):

        graph[k][k] = 0

    fuel = deepcopy(graph)



    for a, b, c in Roads:

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

    dist = floyd_warshall(graph)



    for i in range(N):

        for j in range(N):

            fuel[i][j] = int(dist[i][j] <= L)

    fuel = floyd_warshall(fuel)



    ans = [-1 if fuel[s - 1][t - 1] == INF else int(fuel[s - 1][t - 1]) - 1 for s, t in Queries]

    return '\n'.join(map(str, ans))



print((e_travel_by_car()))
def e_travel_by_car(INF=float('inf')):

    from copy import deepcopy

    from scipy.sparse.csgraph import floyd_warshall

    N, M, L = [int(i) for i in input().split()]

    Roads = [[int(i) for i in input().split()] for j in range(M)]

    Q = int(eval(input()))

    Queries = [[int(i) for i in input().split()] for j in range(Q)]



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

    for k in range(N):

        graph[k][k] = 0

    fuel = deepcopy(graph)



    for a, b, c in Roads:

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

    dist = floyd_warshall(graph)



    for i in range(N):

        for j in range(i):

            fuel[i][j] = fuel[j][i] = int(dist[i][j] <= L)

    fuel = floyd_warshall(fuel)



    ans = [-1 if fuel[s - 1][t - 1] == INF else int(fuel[s - 1][t - 1]) - 1 for s, t in Queries]

    return '\n'.join(map(str, ans))



print((e_travel_by_car()))
def e_travel_by_car(INF=float("inf")):
    from copy import deepcopy
    from scipy.sparse.csgraph import floyd_warshall

    N, M, L = [int(i) for i in input().split()]
    Roads = [[int(i) for i in input().split()] for j in range(M)]
    Q = int(eval(input()))
    Queries = [[int(i) for i in input().split()] for j in range(Q)]
    graph = [[INF] * N for _ in range(N)]
    for k in range(N):
        graph[k][k] = 0
    fuel = deepcopy(graph)
    for a, b, c in Roads:
        graph[a - 1][b - 1] = graph[b - 1][a - 1] = c
    dist = floyd_warshall(graph)
    for i in range(N):
        for j in range(N):
            fuel[i][j] = int(dist[i][j] <= L)
    fuel = floyd_warshall(fuel)
    ans = [
        -1 if fuel[s - 1][t - 1] == INF else int(fuel[s - 1][t - 1]) - 1
        for s, t in Queries
    ]
    return "\n".join(map(str, ans))


print((e_travel_by_car()))

def e_travel_by_car(INF=float("inf")):
    from copy import deepcopy
    from scipy.sparse.csgraph import floyd_warshall

    N, M, L = [int(i) for i in input().split()]
    Roads = [[int(i) for i in input().split()] for j in range(M)]
    Q = int(eval(input()))
    Queries = [[int(i) for i in input().split()] for j in range(Q)]
    graph = [[INF] * N for _ in range(N)]
    for k in range(N):
        graph[k][k] = 0
    fuel = deepcopy(graph)
    for a, b, c in Roads:
        graph[a - 1][b - 1] = graph[b - 1][a - 1] = c
    dist = floyd_warshall(graph)
    for i in range(N):
        for j in range(i):
            fuel[i][j] = fuel[j][i] = int(dist[i][j] <= L)
    fuel = floyd_warshall(fuel)
    ans = [
        -1 if fuel[s - 1][t - 1] == INF else int(fuel[s - 1][t - 1]) - 1
        for s, t in Queries
    ]
    return "\n".join(map(str, ans))


print((e_travel_by_car()))

-        for j in range(N): -            fuel[i][j] = int(dist[i][j] <= L) +        for j in range(i): +            fuel[i][j] = fuel[j][i] = int(dist[i][j] <= L)