Buggy python submission (verdict: Time Limit Exceeded):
import sys
import math
from collections import defaultdict

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def II(): return map(int, input().split())
def III(): return list(map(int, input().split()))
def Line(N,num):
    if N<=0:
        return [[]]*num
    elif num==1:
        return [I() for _ in range(N)]
    else:
        read_all = [tuple(II()) for _ in range(N)]
        return map(list, zip(*read_all))

#################

from collections import deque
import copy

N,M,L = II()
A,B,C = Line(M,3)
Q = I()
s,t = Line(Q,2)

from heapq import heappop, heappush
def dijkstra(E,s,n):
    inf=float('inf')
    d=[inf for _ in range(n)]
    d[s]=0
    prev = [None]*n
    q=[]
    heappush(q,(0,s))
    while q:
        du, u1 = heappop(q)
        if d[u1]<du: continue
        for v1,weight in E[u1].items():
            alt=du+weight
            if d[v1]>alt:
                d[v1]=alt
                prev[v1] = u1
                heappush(q, (alt,v1))
    return d,prev

E = [dict() for _ in range(N)]
for i in range(M):
    E[A[i]-1][B[i]-1] = C[i]
    E[B[i]-1][A[i]-1] = C[i]


pa = [[] for _ in range(N)]
for i in range(N):
    d,prev = dijkstra(E,i,N)
    pa[i] = prev

path = [[[] for _ in range(N)] for _ in range(N)]
for i in range(N):
    for j in range(N):
        k = j
        while pa[i][k]!=None:
            k = pa[i][k]
            path[i][j].append(k)
        path[i][j] = list(reversed(path[i][j]))

E2 = [[float('inf')]*N for _ in range(N)]
for i in range(N):
    E2[i][i] = 0
for i in range(M):
    E2[A[i]-1][B[i]-1] = C[i]
    E2[B[i]-1][A[i]-1] = C[i]

ans = [[0]*N for _ in range(N)]
for i in range(N):
    for j in range(N):
        l = L
        now = i
        p = 0
        if path[i][j]:
            ne = path[i][j][p]
        else:
            ne = j
        while now != j:
            if E2[now][ne]>L:
                ans[i][j] = -1
                break
            elif l < E2[now][ne]:
                ans[i][j] += 1
                l = L-E2[now][ne]
                now = ne
                if path[ne][j]:
                    ne = path[ne][j][0]
                else:
                    ne = j
            else:
                l -= E2[now][ne]
                now = ne
                if path[ne][j]:
                    ne = path[ne][j][0]
                else:
                    ne = j                 

for i in range(Q):
    print(ans[s[i]-1][t[i]-1])

Fixed submission:
import sys
import math
from collections import defaultdict

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def II(): return map(int, input().split())
def III(): return list(map(int, input().split()))
def Line(N,num):
    if N<=0:
        return [[]]*num
    elif num==1:
        return [I() for _ in range(N)]
    else:
        read_all = [tuple(II()) for _ in range(N)]
        return map(list, zip(*read_all))

#################

import copy

N,M,L = II()
A,B,C = Line(M,3)
Q = I()
s,t = Line(Q,2)

E = [[float('inf')]*N for _ in range(N)]
for i in range(N):
    E[i][i] = 0
for i in range(M):
    E[A[i]-1][B[i]-1] = C[i]
    E[B[i]-1][A[i]-1] = C[i]

for k in range(N):
    for i in range(N):
        for j in range(N):
            if E[i][j] > E[i][k]+E[k][j]:
                E[i][j] = E[i][k]+E[k][j]

E2 = [[float('inf')]*N for _ in range(N)]

for i in range(N):
    E2[i][i] = 0
for i in range(N):
    for j in range(N):
        if E[i][j]<=L:
            E2[i][j] = 1
            E2[j][i] = 1

for k in range(N):
    for i in range(N):
        for j in range(N):
            if E2[i][j] > E2[i][k]+E2[k][j]:
                E2[i][j] = E2[i][k]+E2[k][j]

for i in range(Q):
    if E2[s[i]-1][t[i]-1] == float('inf'):
        print(-1)
    else:
        print(E2[s[i]-1][t[i]-1]-1)