Buggy python submission (verdict: Time Limit Exceeded):
def main():
    import heapq
    import sys
    input = sys.stdin.buffer.readline
    N, M, L = map(int, input().split())

    G = [set() for i in range(N)]
    for i in range(M):
        A, B, C = map(int, input().split())
        A -= 1
        B -= 1
        G[A].add((B, C))
        G[B].add((A, C))

    # ある地点からそこまでにL以下の道があるか、０でいける地点からそのときの残量以下で行ける地点が0で行ける地点
    # ある地点からL以下ではいけるが、その地点に行くまでの残量ではいけない場合には、+1

    D = [[[-1, -1]for j in range(N)] for i in range(N)]
    for i in range(N):
        D[i][i] = [0, L]
    #q = collections.deque()
    hq = []
    count = 0
    for i in range(N):
        r = set([i])
        # q.append(i)
        hq = [(0, i)]
        DD = D[i]
        while(hq):
            count += 1
            #x = q.popleft()
            d, x = heapq.heappop(hq)
            for y, c in G[x]:
                if c > L:
                    continue
                elif c > DD[x][1]:
                    if DD[y][0] == -1 or DD[y][0] > DD[x][0]+1:
                        DD[y][0] = DD[x][0] + 1
                        DD[y][1] = L - c
                        heapq.heappush(hq, (DD[y][0], y))
                    elif DD[y][0] == DD[x][0]+1 and DD[y][1] < L - c:
                        DD[y][1] = L - c
                        heapq.heappush(hq, (DD[y][0], y))
                else:
                    if DD[y][0] == -1 or DD[y][0] > DD[x][0]:
                        DD[y][0] = DD[x][0]
                        DD[y][1] = DD[x][1] - c
                        heapq.heappush(hq, (DD[y][0], y))
                    elif DD[y][0] == DD[x][0] and DD[y][1] < DD[x][1] - c:
                        DD[y][1] = DD[x][1] - c
                        heapq.heappush(hq, (DD[y][0], y))
    # print(D)

    Q = int(input())
    for _ in range(Q):
        s, t = map(int, input().split())
        s -= 1
        t -= 1
        print(D[s][t][0])


main()


Fixed submission:
from scipy.sparse.csgraph import csgraph_from_dense, dijkstra


def main():
    import heapq
    import sys
    input = sys.stdin.buffer.readline
    N, M, L = map(int, input().split())

    G = [[-1]*N for i in range(N)]

    for i in range(M):
        A, B, C = map(int, input().split())
        A -= 1
        B -= 1
        G[A][B] = C
        G[B][A] = C
    g = csgraph_from_dense(G, null_value=-1)
    D = dijkstra(g)
    G2 = [[-1]*N for i in range(N)]
    for i in range(N):
        for j in range(N):
            if D[i][j] >= 0 and D[i][j] <= L:
                G2[i][j] = 1

    g2 = csgraph_from_dense(G2, null_value=-1)
    D = dijkstra(g2)


    Q = int(input())
    for _ in range(Q):
        s, t = map(int, input().split())
        s -= 1
        t -= 1
        if D[s][t] <= N**2:
            print(int(D[s][t])-1)
        else:
            print(-1)


main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    import heapq
    import sys
    input = sys.stdin.buffer.readline
    N, M, L = map(int, input().split())

    G = [[-1]*N for i in range(N)]
    for i in range(M):
        A, B, C = map(int, input().split())
        A -= 1
        B -= 1
        G[A][B] = C
        G[B][A] = C

    # ある地点からそこまでにL以下の道があるか、０でいける地点からそのときの残量以下で行ける地点が0で行ける地点
    # ある地点からL以下ではいけるが、その地点に行くまでの残量ではいけない場合には、+1

    D = [[[-1, -1]for j in range(N)] for i in range(N)]
    for i in range(N):
        D[i][i] = [0, L]
    #q = collections.deque()
    hq = []
    count = 0
    for i in range(N):
        r = set([i])
        # q.append(i)
        hq = [(0, i)]
        while(hq):
            count += 1
            #x = q.popleft()
            d, x = heapq.heappop(hq)
            for y in range(N):
                if y not in r and G[x][y] >= 0:
                    if G[x][y] > L:
                        continue
                    elif G[x][y] > D[i][x][1]:
                        if D[i][y][0] == -1 or D[i][y][0] > D[i][x][0]+1:
                            D[i][y][0] = D[i][x][0] + 1
                            D[i][y][1] = L - G[x][y]
                            heapq.heappush(hq, (D[i][y][0], y))
                        elif D[i][y][0] == D[i][x][0]+1 and D[i][y][1] < L - G[x][y]:
                            D[i][y][1] = L - G[x][y]
                            heapq.heappush(hq, (D[i][y][0], y))
                    else:
                        if D[i][y][0] == -1 or D[i][y][0] > D[i][x][0]:
                            D[i][y][0] = D[i][x][0]
                            D[i][y][1] = D[i][x][1] - G[x][y]
                            heapq.heappush(hq, (D[i][y][0], y))
                        elif D[i][y][0] == D[i][x][0] and D[i][y][1] < D[i][x][1] - G[x][y]:
                            D[i][y][1] = D[i][x][1] - G[x][y]
                            heapq.heappush(hq, (D[i][y][0], y))
    # print(D)

    Q = int(input())
    for _ in range(Q):
        s, t = map(int, input().split())
        s -= 1
        t -= 1
        print(D[s][t][0])


main()


Fixed submission:
from scipy.sparse.csgraph import csgraph_from_dense, dijkstra


def main():
    import heapq
    import sys
    input = sys.stdin.buffer.readline
    N, M, L = map(int, input().split())

    G = [[-1]*N for i in range(N)]

    for i in range(M):
        A, B, C = map(int, input().split())
        A -= 1
        B -= 1
        G[A][B] = C
        G[B][A] = C
    g = csgraph_from_dense(G, null_value=-1)
    D = dijkstra(g)
    G2 = [[-1]*N for i in range(N)]
    for i in range(N):
        for j in range(N):
            if D[i][j] >= 0 and D[i][j] <= L:
                G2[i][j] = 1

    g2 = csgraph_from_dense(G2, null_value=-1)
    D = dijkstra(g2)


    Q = int(input())
    for _ in range(Q):
        s, t = map(int, input().split())
        s -= 1
        t -= 1
        if D[s][t] <= N**2:
            print(int(D[s][t])-1)
        else:
            print(-1)


main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    import heapq
    import sys
    input = sys.stdin.buffer.readline
    N, M, L = map(int, input().split())

    G = [[-1]*N for i in range(N)]
    for i in range(M):
        A, B, C = map(int, input().split())
        A -= 1
        B -= 1
        G[A][B] = C
        G[B][A] = C

    # ある地点からそこまでにL以下の道があるか、０でいける地点からそのときの残量以下で行ける地点が0で行ける地点
    # ある地点からL以下ではいけるが、その地点に行くまでの残量ではいけない場合には、+1

    D = [[[-1, -1]for j in range(N)] for i in range(N)]
    for i in range(N):
        D[i][i] = [0, L]
    #q = collections.deque()
    hq = []
    count = 0
    for i in range(N):
        r = set([i])
        # q.append(i)
        hq = [(0, i)]
        while(hq):
            count += 1
            #x = q.popleft()
            d, x = heapq.heappop(hq)
            for y in range(N):
                if y not in r and G[x][y] >= 0:
                    if G[x][y] > L:
                        continue
                    elif G[x][y] > D[i][x][1]:
                        if D[i][y][0] == -1 or D[i][y][0] > D[i][x][0]+1:
                            D[i][y][0] = D[i][x][0] + 1
                            D[i][y][1] = L - G[x][y]
                            heapq.heappush(hq, (D[i][y][0], y))
                        elif D[i][y][0] == D[i][x][0]+1 and D[i][y][1] < L - G[x][y]:
                            D[i][y][1] = L - G[x][y]
                            heapq.heappush(hq, (D[i][y][0], y))
                    else:
                        if D[i][y][0] == -1 or D[i][y][0] > D[i][x][0]:
                            D[i][y][0] = D[i][x][0]
                            D[i][y][1] = D[i][x][1] - G[x][y]
                            heapq.heappush(hq, (D[i][y][0], y))
                        elif D[i][y][0] == D[i][x][0] and D[i][y][1] < D[i][x][1] - G[x][y]:
                            D[i][y][1] = D[i][x][1] - G[x][y]
                            heapq.heappush(hq, (D[i][y][0], y))
    # print(D)

    Q = int(input())
    for _ in range(Q):
        s, t = map(int, input().split())
        s -= 1
        t -= 1
        print(D[s][t][0])


main()


Fixed submission:
from scipy.sparse.csgraph import csgraph_from_dense, dijkstra


def main():
    import heapq
    import sys
    input = sys.stdin.buffer.readline
    N, M, L = map(int, input().split())

    G = [[-1]*N for i in range(N)]

    for i in range(M):
        A, B, C = map(int, input().split())
        A -= 1
        B -= 1
        G[A][B] = C
        G[B][A] = C
    g = csgraph_from_dense(G, null_value=-1)
    D = dijkstra(g)
    G2 = [[-1]*N for i in range(N)]
    for i in range(N):
        for j in range(N):
            if D[i][j] >= 0 and D[i][j] <= L:
                G2[i][j] = 1

    g2 = csgraph_from_dense(G2, null_value=-1)
    D = dijkstra(g2)


    Q = int(input())
    for _ in range(Q):
        s, t = map(int, input().split())
        s -= 1
        t -= 1
        if D[s][t] <= N**2:
            print(int(D[s][t])-1)
        else:
            print(-1)


main()