Buggy python submission (verdict: Runtime Error):
from collections import defaultdict
from scipy.sparse.csgraph import floyd_warshall

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

graph = defaultdict(list)

for _ in range(m):
    a, b, c = map(int, input().split())
    graph[a].append((b, c))
    graph[b].append((a, c))


def dfs(u):
    dist = [-1] * (n + 1)
    stack = [u]
    dist[u] = 0
    while stack:
        u = stack.pop()
        for v, d in graph[u]:
            nxt = dist[u] + d
            if dist[v] == -1 or dist[v] > nxt:
                dist[v] = nxt
                stack.append(v)
    return dist


dist = [[0] * n for _ in range(n)]
for i in range(1, n + 1):
    dist[i - 1] = dfs(i)[1:]

dist = np.array(dist)

graph2 = np.full((n, n), np.inf)
graph2[dist <= l] = 1
dist = floyd_warshall(graph2, directed=False)
dist = dist.astype(int)

q = int(input())

ans = []
for _ in range(q):
    s, t = map(int, input().split())
    ans.append(dist[s - 1, t - 1] - 1)

print(*ans, sep="\n")

Fixed submission:
import numpy as np
from scipy.sparse import csr_matrix
from scipy.sparse.csgraph import floyd_warshall

n, m, l = map(int, input().split())
INF = 1 << 60

dist = [[INF] * n for _ in range(n)]

for i in range(n):
    dist[i][i] = 0

for _ in range(m):
    a, b, c = map(int, input().split())
    a -= 1
    b -= 1
    if c > l:
        continue
    dist[a][b] = c
    dist[b][a] = c


dist = floyd_warshall(csr_matrix(dist))

dist2 = np.full_like(dist, 1)
dist2[np.where(dist > l)] = INF
dist2 = floyd_warshall(dist2, directed=False)
dist2[dist2 == INF] = 0
dist2 = dist2.astype(int)

q = int(input())

ans = []
for _ in range(q):
    s, t = map(int, input().split())
    if dist[s - 1, t - 1] == -1:
        ans.append(-1)
    else:
        ans.append(dist2[s - 1, t - 1] - 1)

print(*ans, sep="\n")

Buggy python submission (verdict: Runtime Error):
from collections import defaultdict
sys.setrecursionlimit(10**7)

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

graph = defaultdict(list)

for _ in range(m):
    a, b, c = map(int, input().split())
    graph[a].append((b, c))
    graph[b].append((a, c))


def dfs(u, l, rest):
    dist = [-1] * (n + 1)
    stack = [u]
    dist[u] = 0
    while stack:
        u = stack.pop()
        tmp_rest = rest
        for v, d in graph[u]:
            rest = tmp_rest
            rest -= d
            if rest < 0:
                nxt = dist[u] + 1
                rest = l
            else:
                nxt = dist[u]
            if dist[v] == -1 or dist[v] > nxt:
                dist[v] = nxt
                stack.append(v)
    return dist


q = int(input())

ans = []
for _ in range(q):
    s, t = map(int, input().split())
    ans.append(dfs(s, l, l)[t])

print(ans, sep="\n")

Fixed submission:
import numpy as np
from scipy.sparse import csr_matrix
from scipy.sparse.csgraph import floyd_warshall

n, m, l = map(int, input().split())
INF = 1 << 60

dist = [[INF] * n for _ in range(n)]

for i in range(n):
    dist[i][i] = 0

for _ in range(m):
    a, b, c = map(int, input().split())
    a -= 1
    b -= 1
    if c > l:
        continue
    dist[a][b] = c
    dist[b][a] = c


dist = floyd_warshall(csr_matrix(dist))

dist2 = np.full_like(dist, 1)
dist2[np.where(dist > l)] = INF
dist2 = floyd_warshall(dist2, directed=False)
dist2[dist2 == INF] = 0
dist2 = dist2.astype(int)

q = int(input())

ans = []
for _ in range(q):
    s, t = map(int, input().split())
    if dist[s - 1, t - 1] == -1:
        ans.append(-1)
    else:
        ans.append(dist2[s - 1, t - 1] - 1)

print(*ans, sep="\n")