from collections import deque

import sys

import random

input = sys.stdin.readline





n, m, l = list(map(int, input().split()))

info = [list(map(int, input().split())) for i in range(m)]

graph = [[] for i in range(n)]



for i in range(m):

    a, b, cost = info[i]

    if cost > l:

        continue

    a -= 1

    b -= 1

    graph[a].append((b, cost))

    graph[b].append((a, cost))



for i in range(n):

    graph[i] = random.sample(graph[i], len(graph[i]))

    

def dfs(start):

    INF = float("inf")

    visited_t = [INF]*n # 回数, 残り燃料

    visited_l = [0]*n

    visited_t[start] = 0

    q = deque([start])

    while q:

        pos = q.popleft()

        for next_pos, cost in graph[pos]:

            tmp = cost + visited_l[pos]

            times = visited_t[pos]

            next_times = visited_t[next_pos]

            if tmp <= l:

                if next_times > times:

                    q.append(next_pos)

                    visited_t[next_pos] = times

                    visited_l[next_pos] = tmp

                elif next_times == times:

                    if visited_l[next_pos] > tmp:

                        q.append(next_pos)

                        visited_t[next_pos] = times

                        visited_l[next_pos] = tmp

                else:

                    continue

            else:

                tmp = cost

                if next_times > times + 1:

                    q.append(next_pos)

                    visited_t[next_pos] = times + 1

                    visited_l[next_pos] = tmp

                elif next_times == times + 1:

                    if visited_l[next_pos] > tmp:

                        q.append(next_pos)

                        visited_t[next_pos] = times + 1

                        visited_l[next_pos] = tmp

                else:

                    continue

    return visited_t



ans = [None]*n

for i in range(n):

    ans[i] = dfs(i)



INF = float("inf")

q = int(eval(input()))

query = [list(map(int, input().split())) for i in range(q)]

for i in range(q):

    a, b = query[i]

    a -= 1

    b -= 1

    tmp = ans[a][b]

    if tmp == INF:

        print((-1))

    else:

        print(tmp)

from collections import deque

import sys

input = sys.stdin.readline





n, m, l = list(map(int, input().split()))

info = [list(map(int, input().split())) for i in range(m)]

graph = [[] for i in range(n)]

 

for i in range(m):

    a, b, cost = info[i]

    if cost > l:

        continue

    a -= 1

    b -= 1

    graph[a].append((b, cost))

    graph[b].append((a, cost))





def warshall_floid(graph):

    n = len(graph)

    INF = 10**18

    dp = [[INF]*n for i in range(n)]

    for i in range(n):

        dp[i][i] = 0



    for i in range(n):

        for j, cost in graph[i]:

            dp[i][j] = cost

            dp[j][i] = cost



    for k in range(n):

        for i in range(n):

            for j in range(n):

                dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j])



    return dp



def warshall_floid2(matrix):

    n = len(matrix)

    dp = matrix

    for k in range(n):

        for i in range(n):

            for j in range(n):

                dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j])

    return dp



dp1 = warshall_floid(graph)

INF = 10**18

dp2 = [[INF]*n for i in range(n)]

for i in range(n):

    for j in range(n):

        if dp1[i][j] <= l:

            dp2[i][j] = 1

ans = warshall_floid2(dp2)         



q = int(eval(input()))

query = [list(map(int, input().split())) for i in range(q)]

for i in range(q):

    a, b = query[i]

    a -= 1

    b -= 1

    if ans[a][b] == 10**18:

        print((-1))

    else:

        print((ans[a][b] - 1))
from collections import deque
import sys
import random

input = sys.stdin.readline
n, m, l = list(map(int, input().split()))
info = [list(map(int, input().split())) for i in range(m)]
graph = [[] for i in range(n)]
for i in range(m):
    a, b, cost = info[i]
    if cost > l:
        continue
    a -= 1
    b -= 1
    graph[a].append((b, cost))
    graph[b].append((a, cost))
for i in range(n):
    graph[i] = random.sample(graph[i], len(graph[i]))


def dfs(start):
    INF = float("inf")
    visited_t = [INF] * n  # 回数, 残り燃料
    visited_l = [0] * n
    visited_t[start] = 0
    q = deque([start])
    while q:
        pos = q.popleft()
        for next_pos, cost in graph[pos]:
            tmp = cost + visited_l[pos]
            times = visited_t[pos]
            next_times = visited_t[next_pos]
            if tmp <= l:
                if next_times > times:
                    q.append(next_pos)
                    visited_t[next_pos] = times
                    visited_l[next_pos] = tmp
                elif next_times == times:
                    if visited_l[next_pos] > tmp:
                        q.append(next_pos)
                        visited_t[next_pos] = times
                        visited_l[next_pos] = tmp
                else:
                    continue
            else:
                tmp = cost
                if next_times > times + 1:
                    q.append(next_pos)
                    visited_t[next_pos] = times + 1
                    visited_l[next_pos] = tmp
                elif next_times == times + 1:
                    if visited_l[next_pos] > tmp:
                        q.append(next_pos)
                        visited_t[next_pos] = times + 1
                        visited_l[next_pos] = tmp
                else:
                    continue
    return visited_t


ans = [None] * n
for i in range(n):
    ans[i] = dfs(i)
INF = float("inf")
q = int(eval(input()))
query = [list(map(int, input().split())) for i in range(q)]
for i in range(q):
    a, b = query[i]
    a -= 1
    b -= 1
    tmp = ans[a][b]
    if tmp == INF:
        print((-1))
    else:
        print(tmp)

from collections import deque
import sys

input = sys.stdin.readline
n, m, l = list(map(int, input().split()))
info = [list(map(int, input().split())) for i in range(m)]
graph = [[] for i in range(n)]
for i in range(m):
    a, b, cost = info[i]
    if cost > l:
        continue
    a -= 1
    b -= 1
    graph[a].append((b, cost))
    graph[b].append((a, cost))


def warshall_floid(graph):
    n = len(graph)
    INF = 10**18
    dp = [[INF] * n for i in range(n)]
    for i in range(n):
        dp[i][i] = 0
    for i in range(n):
        for j, cost in graph[i]:
            dp[i][j] = cost
            dp[j][i] = cost
    for k in range(n):
        for i in range(n):
            for j in range(n):
                dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j])
    return dp


def warshall_floid2(matrix):
    n = len(matrix)
    dp = matrix
    for k in range(n):
        for i in range(n):
            for j in range(n):
                dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j])
    return dp


dp1 = warshall_floid(graph)
INF = 10**18
dp2 = [[INF] * n for i in range(n)]
for i in range(n):
    for j in range(n):
        if dp1[i][j] <= l:
            dp2[i][j] = 1
ans = warshall_floid2(dp2)
q = int(eval(input()))
query = [list(map(int, input().split())) for i in range(q)]
for i in range(q):
    a, b = query[i]
    a -= 1
    b -= 1
    if ans[a][b] == 10**18:
        print((-1))
    else:
        print((ans[a][b] - 1))

-import random -for i in range(n): -    graph[i] = random.sample(graph[i], len(graph[i])) -def dfs(start): -    INF = float("inf") -    visited_t = [INF] * n  # 回数, 残り燃料 -    visited_l = [0] * n -    visited_t[start] = 0 -    q = deque([start]) -    while q: -        pos = q.popleft() -        for next_pos, cost in graph[pos]: -            tmp = cost + visited_l[pos] -            times = visited_t[pos] -            next_times = visited_t[next_pos] -            if tmp <= l: -                if next_times > times: -                    q.append(next_pos) -                    visited_t[next_pos] = times -                    visited_l[next_pos] = tmp -                elif next_times == times: -                    if visited_l[next_pos] > tmp: -                        q.append(next_pos) -                        visited_t[next_pos] = times -                        visited_l[next_pos] = tmp -                else: -                    continue -            else: -                tmp = cost -                if next_times > times + 1: -                    q.append(next_pos) -                    visited_t[next_pos] = times + 1 -                    visited_l[next_pos] = tmp -                elif next_times == times + 1: -                    if visited_l[next_pos] > tmp: -                        q.append(next_pos) -                        visited_t[next_pos] = times + 1 -                        visited_l[next_pos] = tmp -                else: -                    continue -    return visited_t +def warshall_floid(graph): +    n = len(graph) +    INF = 10**18 +    dp = [[INF] * n for i in range(n)] +    for i in range(n): +        dp[i][i] = 0 +    for i in range(n): +        for j, cost in graph[i]: +            dp[i][j] = cost +            dp[j][i] = cost +    for k in range(n): +        for i in range(n): +            for j in range(n): +                dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j]) +    return dp -ans = [None] * n +def warshall_floid2(matrix): +    n = len(matrix) +    dp = matrix +    for k in range(n): +        for i in range(n): +            for j in range(n): +                dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j]) +    return dp + + +dp1 = warshall_floid(graph) +INF = 10**18 +dp2 = [[INF] * n for i in range(n)] -    ans[i] = dfs(i) -INF = float("inf") +    for j in range(n): +        if dp1[i][j] <= l: +            dp2[i][j] = 1 +ans = warshall_floid2(dp2) -    tmp = ans[a][b] -    if tmp == INF: +    if ans[a][b] == 10**18: -        print(tmp) +        print((ans[a][b] - 1))