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

input = sys.stdin.readline

cost = []
row = []
col = []

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

for _ in range(m):
    a, b, c = map(int, input().split())
    cost.append(c)
    row.append(a-1)
    col.append(b-1)

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

csr = csr_matrix((cost, (row, col)), shape = (n, n))
path = floyd_warshall(csr, directed=False)

cost = []
row = []
col = []

for i in range(n):
    for j in range(n):
        if path[i][j] <= l:
            cost.append(1)
            row.append(i)
            col.append(j) 

csr = csr_matrix((cost, (row, col)), shape = (n, n))
path = floyd_warshall(csr, directed=False)

INF = float('inf')

for s,t in st:
    ans = path[s-1][t-1]
    if ans == INF:
        print(-1)
    else:
        print(int(ans) - 1)



 import sys sys import numpy as np numpy from scipy.sparse.csgraph import floyd_warshall from scipy.sparse import csr_matrix input = sys.stdin.readline input sys.stdin.readline sys.stdin sys stdin readline cost = [] cost [] row = [] row [] col = [] col [] n, m, l = list(map(int, input().split())) n list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split m l for _ in range(m):
    a, b, c = map(int, input().split())
    cost.append(c)
    row.append(a-1)
    col.append(b-1) _ 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 cost.append(c) cost.append cost append c row.append(a-1) row.append row append a-1 a 1 col.append(b-1) col.append col append b-1 b 1 q = int(input()) q int(input()) int input() input list(map(int, input().split())) for _ in range(q) for _ in range(q) _ range(q) range q for _ in range(q) list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split st = [list(map(int, input().split())) for _ in range(q)] st [list(map(int, input().split())) for _ in range(q)] csr = csr_matrix((cost, (row, col)), shape = (n, n)) csr csr_matrix((cost, (row, col)), shape = (n, n)) csr_matrix (cost, (row, col)) cost (row, col) row col shape = (n, n) shape (n, n) n n path = floyd_warshall(csr, directed=False) path floyd_warshall(csr, directed=False) floyd_warshall csr directed=False directed False cost = [] cost [] row = [] row [] col = [] col [] for i in range(n):
    for j in range(n):
        if path[i][j] <= l:
            cost.append(1)
            row.append(i)
            col.append(j)  i range(n) range n for j in range(n):
        if path[i][j] <= l:
            cost.append(1)
            row.append(i)
            col.append(j)  j range(n) range n if path[i][j] <= l:
            cost.append(1)
            row.append(i)
            col.append(j)  path[i][j] <= l path[i][j] [i] path i j l cost.append(1) cost.append cost append 1 row.append(i) row.append row append i col.append(j) col.append col append j csr = csr_matrix((cost, (row, col)), shape = (n, n)) csr csr_matrix((cost, (row, col)), shape = (n, n)) csr_matrix (cost, (row, col)) cost (row, col) row col shape = (n, n) shape (n, n) n n path = floyd_warshall(csr, directed=False) path floyd_warshall(csr, directed=False) floyd_warshall csr directed=False directed False INF = float('inf') INF float('inf') float 'inf' for s,t in st:
    ans = path[s-1][t-1]
    if ans == INF:
        print(-1)
    else:
        print(int(ans) - 1) s t st ans = path[s-1][t-1] ans path[s-1][t-1] [s-1] path s-1 s 1 t-1 t 1 if ans == INF:
        print(-1)
    else:
        print(int(ans) - 1) ans == INF ans INF print(-1) print -1 print(int(ans) - 1) print int(ans) - 1 int(ans) int ans 1 col = [] [] col row = [] [] row a, b, c = map(int, input().split()) map(int, input().split()) a csr = csr_matrix((cost, (row, col)), shape = (n, n)) csr_matrix((cost, (row, col)), shape = (n, n)) csr col = [] [] col csr = csr_matrix((cost, (row, col)), shape = (n, n)) csr_matrix((cost, (row, col)), shape = (n, n)) csr cost = [] [] cost b, c = map(int, input().split()) map(int, input().split()) b ans = path[s-1][t-1] path[s-1][t-1] ans m, l = list(map(int, input().split())) list(map(int, input().split())) m cost = [] [] cost l = list(map(int, input().split())) list(map(int, input().split())) l n, m, l = list(map(int, input().split())) list(map(int, input().split())) n c = map(int, input().split()) map(int, input().split()) c q = int(input()) int(input()) q path = floyd_warshall(csr, directed=False) floyd_warshall(csr, directed=False) path path = floyd_warshall(csr, directed=False) floyd_warshall(csr, directed=False) path row = [] [] row input = sys.stdin.readline sys.stdin.readline input st = [list(map(int, input().split())) for _ in range(q)] [list(map(int, input().split())) for _ in range(q)] st INF = float('inf') float('inf') INF
import sys
import numpy as np
from scipy.sparse.csgraph import floyd_warshall
from scipy.sparse import csr_matrix

input = sys.stdin.readline

cost = []
row = []
col = []

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

for _ in range(m):
    a, b, c = map(int, input().split())
    cost.append(c)
    row.append(a-1)
    col.append(b-1)

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

csr = csr_matrix((cost, (row, col)), shape = (n, n))
path = floyd_warshall(csr, directed=False)

cost = []
row = []
col = []

for i in range(n):
    for j in range(n):
        if path[i][j] <= l:
            cost.append(1)
            row.append(i)
            col.append(j) 

csr = csr_matrix((cost, (row, col)), shape = (n, n))
path = floyd_warshall(csr, directed=False)

INF = float('inf')

for s,t in st:
    ans = path[s-1][t-1]
    if ans == INF:
        print(-1)
    else:
        print(int(ans) - 1)




