Buggy python submission (verdict: Time Limit Exceeded):
def main():
    import sys
    input = sys.stdin.readline
    n,m,l = map(int,input().split())
    E = [[] for i in range(n)]
    for i in range(m):
        a,b,c = map(int,input().split())
        E[a-1].append((b-1,c))
        E[b-1].append((a-1,c))

    from collections import deque

    def bfs(s):
        dist = [[float("INF")]*2 for _ in range(n)]
        
        dist[s] = [0,l]
        q = deque([])
        q.append((0,s,l))
        
        while q:
            count,cur,fuel = q.popleft()
            for nex, nex_len in E[cur]:
                if fuel >= nex_len:
                    nex_fuel = fuel - nex_len
                    if dist[nex][0] > count or (dist[nex][0]==count and nex_fuel > dist[nex][1]):
                        q.append((count,nex,nex_fuel))
                        dist[nex] = [count,nex_fuel]
                else:
                    if l >= nex_len:
                        nex_fuel = l-nex_len
                        nex_count = count+1
                        if dist[nex][0] > nex_count or (dist[nex][0]==nex_count and nex_fuel > dist[nex][1]):
                            q.append((nex_count,nex,nex_fuel))
                            dist[nex] = [nex_count,nex_fuel]

        return dist


    ans = []
    for i in range(n):
        ans.append(bfs(i))
    
    x = int(input())
    for i in range(x):
        s,g = map(int,input().split())
        if ans[s-1][g-1][0] == float("INF"):
            print(-1)
        else:
            print(ans[s-1][g-1][0]) 
if __name__ == "__main__":
    main()

Fixed submission:
def main():
    import sys
    input = sys.stdin.readline
    n,m,l = map(int,input().split())
    E = [[float("INF")]*n for i in range(n)]
    for i in range(m):
        a,b,c = map(int,input().split())
        E[a-1][b-1] = c
        E[b-1][a-1] = c

    for k in range(n):
        for i in range(n):
            for j in range(n):
                E[i][j] = min(E[i][j],E[i][k]+E[k][j])
    wa = [[float("INF")]*n for i in range(n)]
    for i in range(n):
        for j in range(n):
            if E[i][j] <= l:
                wa[i][j] = 1
    for k in range(n):
        for i in range(n):
            for j in range(n):
                wa[i][j] = min(wa[i][j],wa[i][k]+wa[k][j])
    
    x = int(input())
    for i in range(x):
        s,g = map(int,input().split())
        if wa[s-1][g-1] == float("INF"):
            print(-1)
        else:
            print(wa[s-1][g-1]-1)

    
if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    import sys
    input = sys.stdin.readline
    n,m,l = map(int,input().split())
    E = [[] for i in range(n)]
    for i in range(m):
        a,b,c = map(int,input().split())
        E[a-1].append((b-1,c))
        E[b-1].append((a-1,c))

    import heapq

    def dijkstra(s):
        dist = [[float("INF")]*2 for _ in range(n)]
        
        dist[s] = [0,l]
        q = []
        heapq.heappush(q, (0,s,l))
        
        while q:
            count,cur,fuel = heapq.heappop(q)
            for nex, nex_len in E[cur]:
                if fuel >= nex_len:
                    nex_fuel = fuel - nex_len
                    if dist[nex][0] > count or (dist[nex][0]==count and nex_fuel > dist[nex][1]):
                        heapq.heappush(q, (count,nex,nex_fuel))
                        dist[nex] = [count,nex_fuel]
                else:
                    if l >= nex_len:
                        nex_fuel = l-nex_len
                        nex_count = count+1
                        if dist[nex][0] > nex_count or (dist[nex][0]==nex_count and nex_fuel > dist[nex][1]):
                            heapq.heappush(q, (nex_count,nex,nex_fuel))
                            dist[nex] = [nex_count,nex_fuel]

        return dist


    ans = []
    for i in range(n):
        ans.append(dijkstra(i))
    
    x = int(input())
    for i in range(x):
        s,g = map(int,input().split())
        if ans[s-1][g-1][0] == float("INF"):
            print(-1)
        else:
            print(ans[s-1][g-1][0]) 
if __name__ == "__main__":
    main()

Fixed submission:
def main():
    import sys
    input = sys.stdin.readline
    n,m,l = map(int,input().split())
    E = [[float("INF")]*n for i in range(n)]
    for i in range(m):
        a,b,c = map(int,input().split())
        E[a-1][b-1] = c
        E[b-1][a-1] = c

    for k in range(n):
        for i in range(n):
            for j in range(n):
                E[i][j] = min(E[i][j],E[i][k]+E[k][j])
    wa = [[float("INF")]*n for i in range(n)]
    for i in range(n):
        for j in range(n):
            if E[i][j] <= l:
                wa[i][j] = 1
    for k in range(n):
        for i in range(n):
            for j in range(n):
                wa[i][j] = min(wa[i][j],wa[i][k]+wa[k][j])
    
    x = int(input())
    for i in range(x):
        s,g = map(int,input().split())
        if wa[s-1][g-1] == float("INF"):
            print(-1)
        else:
            print(wa[s-1][g-1]-1)

    
if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    
    n,m,l = map(int,input().split())
    E = [[] for i in range(n)]
    for i in range(m):
        a,b,c = map(int,input().split())
        E[a-1].append((b-1,c))
        E[b-1].append((a-1,c))

    import heapq

    def dijkstra(graph, node, start):
        dist = [[float("INF")]*2 for _ in range(node)]
        
        dist[start] = [0,l]
        q = []
        heapq.heappush(q, (0,start,l))
        
        while q:
            count,cur,fuel = heapq.heappop(q)
            for nex, nex_len in graph[cur]:
                if fuel >= nex_len:
                    nex_fuel = fuel - nex_len
                    if dist[nex][0] > count or (dist[nex][0]==count and nex_fuel > dist[nex][1]):
                        heapq.heappush(q, (count,nex,nex_fuel))
                        dist[nex] = [count,nex_fuel]
                else:
                    if l >= nex_len:
                        nex_fuel = l-nex_len
                        nex_count = count+1
                        if dist[nex][0] > nex_count or (dist[nex][0]==nex_count and nex_fuel > dist[nex][1]):
                            heapq.heappush(q, (nex_count,nex,nex_fuel))
                            dist[nex] = [nex_count,nex_fuel]

        return dist


    ans = []
    for i in range(n):
        ans.append(dijkstra(E, n, i))
    
    x = int(input())
    for i in range(x):
        s,g = map(int,input().split())
        if ans[s-1][g-1][0] == float("INF"):
            print(-1)
        else:
            print(ans[s-1][g-1][0]) 
if __name__ == "__main__":
    main()

Fixed submission:
def main():
    import sys
    input = sys.stdin.readline
    n,m,l = map(int,input().split())
    E = [[float("INF")]*n for i in range(n)]
    for i in range(m):
        a,b,c = map(int,input().split())
        E[a-1][b-1] = c
        E[b-1][a-1] = c

    for k in range(n):
        for i in range(n):
            for j in range(n):
                E[i][j] = min(E[i][j],E[i][k]+E[k][j])
    wa = [[float("INF")]*n for i in range(n)]
    for i in range(n):
        for j in range(n):
            if E[i][j] <= l:
                wa[i][j] = 1
    for k in range(n):
        for i in range(n):
            for j in range(n):
                wa[i][j] = min(wa[i][j],wa[i][k]+wa[k][j])
    
    x = int(input())
    for i in range(x):
        s,g = map(int,input().split())
        if wa[s-1][g-1] == float("INF"):
            print(-1)
        else:
            print(wa[s-1][g-1]-1)

    
if __name__ == "__main__":
    main()