Buggy python submission (verdict: Time Limit Exceeded):
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    from itertools import combinations, permutations, accumulate, groupby, product
    from bisect import bisect_left,bisect_right
    from heapq import heapify, heappop, heappush
    import math
    #from math import gcd

    inf = 10**17
    #mod = 10**9 + 7

    n,m,l = map(int, input().split())
    edge = [[] for i in range(n)]
    #edge[i] : iから出る道の[重み,行先]の配列
    for _ in range(m):
        x,y,z = map(int,input().split())
        edge[x-1].append((z,y-1))
        edge[y-1].append((z,x-1))

    def bfs(start):
        d = [inf]*n
        d[start] = 0
        f = [-1]*n
        f[start] = l
        dq = deque([[start, l, 0]])
        while dq:
            v, nl, dist = dq.popleft()
            if d[v] != dist or f[v] != nl:
                continue
            for c, nv in edge[v]:
                if l - c < 0:
                    continue
                if nl - c >= 0:
                    if dist < d[nv] or (dist==d[nv]and f[nv] < nl - c):
                        f[nv] = nl - c
                        d[nv] = dist
                        dq.append((nv, nl - c, dist))
                else:
                    if dist + 1 < d[nv] or (dist+1==d[nv] and f[nv] < l-c):
                        f[nv] = l - c
                        d[nv] = dist + 1
                        dq.append((nv, l - c, dist + 1))
        return d

    res = []
    for i in range(n):
        res.append(bfs(i))

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

if __name__ == '__main__':
    main()


Fixed submission:
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    from itertools import combinations, permutations, accumulate, groupby, product
    from bisect import bisect_left,bisect_right
    from heapq import heapify, heappop, heappush
    import math
    #from math import gcd

    inf = 10**17
    #mod = 10**9 + 7

    N,M,L = map(int, input().split())
    dp = [[inf]*N for _ in range(N)]
    for i in range(N):
        dp[i][i] = 0
    for _ in range(M):
        a,b,c = map(int, input().split())
        dp[a-1][b-1] = c
        dp[b-1][a-1] = c

    #kを経由してiからjに行く行き方を考える
    #これで上手く行くのは与えられた辺の和しか考えていないため
    for k in range(N):
        for i in range(N):
            for j in range(N):
                if dp[i][j] > dp[i][k] + dp[k][j]:
                    dp[i][j] = dp[j][i] = dp[i][k]+dp[k][j]

    dp2 =  [[inf]*N for _ in range(N)]
    for i in range(N):
        for j in range(N):
            if dp[i][j] <= L:
                dp2[i][j] = dp2[j][i] = 1

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

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

if __name__ == '__main__':
    main()