Buggy python submission (verdict: Time Limit Exceeded):
import sys

sys.setrecursionlimit(10 ** 6)
input = sys.stdin.readline

def main():
    n, m, l = map(int, input().split())
    inf = 1001001001
    dist = [[inf] * n for _ in range(n)]
    for _ in range(m):
        a, b, c = map(int, input().split())
        a, b = a - 1, b - 1
        dist[a][b] = dist[b][a] = c
    for w in range(n):
        dw = dist[w]
        for u in range(n):
            du = dist[u]
            duw = du[w]
            for v in range(n):
                tmp = duw + dw[v]
                if tmp < du[v]: du[v] = tmp
    dist2 = [[inf] * n for _ in range(n)]
    for u in range(n):
        du = dist[u]
        d2u = dist2[u]
        for v in range(n):
            if du[v] <= l: d2u[v] = 1
    for w in range(n):
        dw = dist2[w]
        for u in range(n):
            du = dist2[u]
            duw = du[w]
            for v in range(n):
                tmp = duw + dw[v]
                if tmp < du[v]: du[v] = tmp

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

main()


Fixed submission:
import sys

sys.setrecursionlimit(10 ** 6)
input = sys.stdin.readline

def main():
    n, m, l = map(int, input().split())
    inf = 1001001001
    dist = [[inf] * n for _ in range(n)]
    for _ in range(m):
        a, b, c = map(int, input().split())
        a, b = a - 1, b - 1
        dist[a][b] = dist[b][a] = c
    for w in range(n):
        for u in range(n):
            for v in range(n):
                dist[u][v] = min(dist[u][v], dist[u][w] + dist[w][v])
    dist2 = [[inf] * n for _ in range(n)]
    for u in range(n):
        for v in range(n):
            if dist[u][v] <= l: dist2[u][v] = 1
    for w in range(n):
        for u in range(n):
            for v in range(n):
                dist2[u][v] = min(dist2[u][v], dist2[u][w] + dist2[w][v])

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

main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys

sys.setrecursionlimit(10 ** 6)
input = sys.stdin.readline

def main():
    n, m, l = map(int, input().split())
    inf = 1001001001
    dist = [[inf] * n for _ in range(n)]
    for _ in range(m):
        a, b, c = map(int, input().split())
        a, b = a - 1, b - 1
        dist[a][b] = dist[b][a] = c
    for w in range(n):
        for u in range(n):
            for v in range(n):
                dist[u][v] = min(dist[u][v], dist[u][w] + dist[w][v])
    dist2 = [[inf] * n for _ in range(n)]
    for u in range(n):
        for v in range(n):
            if dist[u][v] <= l: dist2[u][v] = 1
    for w in range(n):
        for u in range(n):
            for v in range(n):
                dist2[u][v] = min(dist2[u][v], dist2[u][w] + dist2[w][v])

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

main()


Fixed submission:
import sys

sys.setrecursionlimit(10 ** 6)
input = sys.stdin.readline

def main():
    n, m, l = map(int, input().split())
    inf = 1001001001
    dist = [[inf] * n for _ in range(n)]
    for _ in range(m):
        a, b, c = map(int, input().split())
        a, b = a - 1, b - 1
        dist[a][b] = dist[b][a] = c
    for w in range(n):
        for u in range(n):
            for v in range(n):
                dist[u][v] = min(dist[u][v], dist[u][w] + dist[w][v])
    dist2 = [[inf] * n for _ in range(n)]
    for u in range(n):
        for v in range(n):
            if dist[u][v] <= l: dist2[u][v] = 1
    for w in range(n):
        for u in range(n):
            for v in range(n):
                dist2[u][v] = min(dist2[u][v], dist2[u][w] + dist2[w][v])

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

main()

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

sys.setrecursionlimit(10 ** 6)
input = sys.stdin.readline

def main():
    n, m, l = map(int, input().split())
    to = defaultdict(list)
    for _ in range(m):
        a, b, c = map(int, input().split())
        a, b = a - 1, b - 1
        to[a].append((b, c))
        to[b].append((a, c))
    q = int(input())
    st = []
    idx = [[-1] * n for _ in range(n)]
    qc = [0] * n
    for i in range(q):
        s, t = map(int, input().split())
        s, t = s - 1, t - 1
        idx[s][t] = i
        qc[min(s, t)] += 1

    # for x in idx:
    # print(x)
    # print(qc)

    ans = [-1] * q
    for s in range(n):
        # print(ans)
        cnt = qc[s]
        if cnt == 0: continue
        hp = []
        heappush(hp, (0, -l, s))
        fin = [False] * n
        while cnt > 0 and hp:
            k, g, u = heappop(hp)
            if fin[u]: continue
            fin[u] = True
            g = -g
            i = idx[s][u]
            if i != -1:
                ans[i] = k
                idx[s][u] = -1
                cnt -= 1
            i = idx[u][s]
            if i != -1:
                ans[i] = k
                idx[u][s] = -1
                cnt -= 1
            for ku, c in to[u]:
                if fin[ku]: continue
                if g - c >= 0:
                    heappush(hp, (k, c - g, ku))
                elif l - c>=0:
                    heappush(hp, (k + 1, -l + c, ku))
    print(*ans,sep="\n")

main()


Fixed submission:
import sys

sys.setrecursionlimit(10 ** 6)
input = sys.stdin.readline

def main():
    n, m, l = map(int, input().split())
    inf = 1001001001
    dist = [[inf] * n for _ in range(n)]
    for _ in range(m):
        a, b, c = map(int, input().split())
        a, b = a - 1, b - 1
        dist[a][b] = dist[b][a] = c
    for w in range(n):
        for u in range(n):
            for v in range(n):
                dist[u][v] = min(dist[u][v], dist[u][w] + dist[w][v])
    dist2 = [[inf] * n for _ in range(n)]
    for u in range(n):
        for v in range(n):
            if dist[u][v] <= l: dist2[u][v] = 1
    for w in range(n):
        for u in range(n):
            for v in range(n):
                dist2[u][v] = min(dist2[u][v], dist2[u][w] + dist2[w][v])

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

main()

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

sys.setrecursionlimit(10 ** 6)
input = sys.stdin.readline

def main():
    n, m, l = map(int, input().split())
    to = defaultdict(list)
    for _ in range(m):
        a, b, c = map(int, input().split())
        a, b = a - 1, b - 1
        to[a].append((b, c))
        to[b].append((a, c))
    q = int(input())
    st = []
    idx = [[-1] * n for _ in range(n)]
    qc = [0] * n
    for i in range(q):
        s, t = map(int, input().split())
        s, t = s - 1, t - 1
        idx[s][t] = i
        qc[min(s, t)] += 1

    # for x in idx:
    # print(x)
    # print(qc)

    ans = [-1] * q
    for s in range(n):
        # print(ans)
        cnt = qc[s]
        if cnt == 0: continue
        hp = []
        heappush(hp, (0, -l, s))
        fin = [False] * n
        while cnt > 0 and hp:
            k, g, u = heappop(hp)
            if fin[u]: continue
            fin[u] = True
            g = -g
            i = idx[s][u]
            if i != -1:
                ans[i] = k
                idx[s][u] = -1
                cnt -= 1
            i = idx[u][s]
            if i != -1:
                ans[i] = k
                idx[u][s] = -1
                cnt -= 1
            for ku, c in to[u]:
                if fin[ku]: continue
                if g - c >= 0:
                    heappush(hp, (k, c - g, ku))
                elif l - c>=0:
                    heappush(hp, (k + 1, -l + c, ku))
    print(*ans,sep="\n")

main()


Fixed submission:
import sys

sys.setrecursionlimit(10 ** 6)
input = sys.stdin.readline

def main():
    n, m, l = map(int, input().split())
    inf = 1001001001
    dist = [[inf] * n for _ in range(n)]
    for _ in range(m):
        a, b, c = map(int, input().split())
        a, b = a - 1, b - 1
        dist[a][b] = dist[b][a] = c
    for w in range(n):
        for u in range(n):
            for v in range(n):
                dist[u][v] = min(dist[u][v], dist[u][w] + dist[w][v])
    dist2 = [[inf] * n for _ in range(n)]
    for u in range(n):
        for v in range(n):
            if dist[u][v] <= l: dist2[u][v] = 1
    for w in range(n):
        for u in range(n):
            for v in range(n):
                dist2[u][v] = min(dist2[u][v], dist2[u][w] + dist2[w][v])

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

main()