Buggy python submission (verdict: Time Limit Exceeded):
inf = float('inf')
def dijkstra(s):
    d = [(inf, 1)] * N
    used = [False] * N
    d[s] = (0, -L)  
    while True:
        v = -1
        for i in range(N):
            if used[i]:
                continue
            if v==-1:
                v = i
            else:
                v = min(v, i, key = lambda x:d[x])
        if v == -1:
            break
        used[v] = True   
        m, r = d[v]
        r = -r
        for nv in range(N):
            c = cost[v][nv]
            if r>=c:
                d[nv] = min(d[nv],(m, -(r-c)))
            elif c<=L:
                d[nv] = min(d[nv], (m+1, -(L-c)))
    return d
N, M, L = map(int, input().split())
cost = [[inf]*N for _ in range(N)]
for _ in range(M):
    a, b, c = map(int, input().split())
    cost[a-1][b-1] = c
    cost[b-1][a-1] = c
ans = []
for i in range(N):
    ans.append(dijkstra(i))
Q = int(input())
for _ in range(Q):
    a, b = map(int, input().split())
    res = ans[a-1][b-1][0]
    print(res if res<10**10 else -1)

Fixed submission:
inf = 10**10
def warshall_floyd(d):#d[u][v]:辺uvのコスト.存在しなければinf.d[i][i]は0
    for i in range(N):#0~iとa, bのみを使う最短経路を考える
        for a in range(N):
            for b in range(N):
                d[a][b] = min(d[a][b], d[a][i]+ d[i][b])#頂点iを使う場合と使わない場合
    return d
N, M, L = map(int, input().split())
G = [[inf]*N for _ in range(N)]
for i in range(N):
    G[i][i] = 0
for _ in range(M):
    a, b, c = map(int, input().split())
    G[a-1][b-1] = c
    G[b-1][a-1] = c
G = warshall_floyd(G)
G2 = [[0]*N for _ in range(N)]
for i in range(N):
    for j in range(N):
        if i == j:
            G2[i][j] = 0
        elif G[i][j]<=L:
            G2[i][j] = 1
        else:
            G2[i][j] = inf
d = warshall_floyd(G2)
Q = int(input())
for _ in range(Q):
    s, t = map(int, input().split())
    ans = d[s-1][t-1]-1
    print(ans if ans < 10**9 else -1)

Buggy python submission (verdict: Time Limit Exceeded):
import heapq
inf = 10**11
def dijkstra(v0):
    d = [(inf, inf)] * N
    d[v0] = (0, L)
    edgelist = []
    for v, c in G[v0]:
        if c<=L:
            heapq.heappush(edgelist,(0, -(L-c), v))
    while len(edgelist):
        c, r, v = heapq.heappop(edgelist)
        r = -r
        if d[v][0] != inf:
            continue
        d[v] = (c, r)
        for nv, nr in G[v]:
            if d[nv][0] == inf:
                if nr<=r:
                    heapq.heappush(edgelist,(c, -(r-nr), nv))
                elif nr<L:
                    heapq.heappush(edgelist,(c+1,-(L-nr), nv))
    return d
N, M, L = map(int, input().split())
G = [[] for _ in range(N)]
for _ in range(M):
    a, b, c = map(int, input().split())
    G[a-1].append((b-1, c))
    G[b-1].append((a-1, c))
ans = []
for i in range(N):
    ans.append(dijkstra(i))
Q = int(input())
for _ in range(Q):
    a, b = map(int, input().split())
    res = ans[a-1][b-1][0]
    print(res if res<10**10 else -1)

Fixed submission:
inf = 10**10
def warshall_floyd(d):#d[u][v]:辺uvのコスト.存在しなければinf.d[i][i]は0
    for i in range(N):#0~iとa, bのみを使う最短経路を考える
        for a in range(N):
            for b in range(N):
                d[a][b] = min(d[a][b], d[a][i]+ d[i][b])#頂点iを使う場合と使わない場合
    return d
N, M, L = map(int, input().split())
G = [[inf]*N for _ in range(N)]
for i in range(N):
    G[i][i] = 0
for _ in range(M):
    a, b, c = map(int, input().split())
    G[a-1][b-1] = c
    G[b-1][a-1] = c
G = warshall_floyd(G)
G2 = [[0]*N for _ in range(N)]
for i in range(N):
    for j in range(N):
        if i == j:
            G2[i][j] = 0
        elif G[i][j]<=L:
            G2[i][j] = 1
        else:
            G2[i][j] = inf
d = warshall_floyd(G2)
Q = int(input())
for _ in range(Q):
    s, t = map(int, input().split())
    ans = d[s-1][t-1]-1
    print(ans if ans < 10**9 else -1)

Buggy python submission (verdict: Time Limit Exceeded):
import heapq
inf = 10**11
def dijkstra(v0):
    d = [(inf, inf)] * N
    d[v0] = (0, L)
    edgelist = []
    for v, c in G[v0]:
        if c<=L:
            heapq.heappush(edgelist,(0, -(L-c), v))
    while len(edgelist):
        c, r, v = heapq.heappop(edgelist)
        r = -r
        if d[v][0] != inf:
            continue
        d[v] = (c, r)
        for nv, nr in G[v]:
            if d[nv][0] == inf:
                if nr<=r:
                    heapq.heappush(edgelist,(c, -(r-nr), nv))
                elif nr<L:
                    heapq.heappush(edgelist,(c+1,-(L-nr), nv))
    return d
N, M, L = map(int, input().split())
G = [[] for _ in range(N)]
for _ in range(M):
    a, b, c = map(int, input().split())
    G[a-1].append((b-1, c))
    G[b-1].append((a-1, c))
ans = []
for i in range(N):
    ans.append(dijkstra(i))
Q = int(input())
for _ in range(Q):
    a, b = map(int, input().split())
    res = ans[a-1][b-1][0]
    print(res if res<10**10 else -1)

Fixed submission:
inf = 10**10
def warshall_floyd(d):#d[u][v]:辺uvのコスト.存在しなければinf.d[i][i]は0
    for i in range(N):#0~iとa, bのみを使う最短経路を考える
        for a in range(N):
            for b in range(N):
                d[a][b] = min(d[a][b], d[a][i]+ d[i][b])#頂点iを使う場合と使わない場合
    return d
N, M, L = map(int, input().split())
G = [[inf]*N for _ in range(N)]
for i in range(N):
    G[i][i] = 0
for _ in range(M):
    a, b, c = map(int, input().split())
    G[a-1][b-1] = c
    G[b-1][a-1] = c
G = warshall_floyd(G)
G2 = [[0]*N for _ in range(N)]
for i in range(N):
    for j in range(N):
        if i == j:
            G2[i][j] = 0
        elif G[i][j]<=L:
            G2[i][j] = 1
        else:
            G2[i][j] = inf
d = warshall_floyd(G2)
Q = int(input())
for _ in range(Q):
    s, t = map(int, input().split())
    ans = d[s-1][t-1]-1
    print(ans if ans < 10**9 else -1)