Buggy python submission (verdict: Runtime Error):
def dfs(v, d):
	global D
	D[v] = d
	for u,dd in G[v]:
		if D[u] is not None:
			continue
		dfs(u, d+dd)

N = int(input())
G = [[] for _ in range(N+1)]
for _ in range(N-1):
	a, b, c = map(int, input().split())
	G[a].append([b, c])
	G[b].append([a, c])

Q, K = map(int, input().split())
D = [None]*(N+1)
dfs(K, 0)
for _ in range(Q):
	x, y = map(int, input().split())
	print(D[x] + D[y])


Fixed submission:
import heapq
import sys
input = sys.stdin.readline


def dijkstra_heap(s, n, edge):
    #始点sから各頂点への最短距離
    d = [float("inf")] * n
    used = [True] * n #True:未確定
    d[s] = 0
    used[s] = False
    edgelist = []
    for e in edge[s]:
        heapq.heappush(edgelist,e)
    while len(edgelist):
        minedge = heapq.heappop(edgelist)
        #まだ使われてない頂点の中から最小の距離のものを探す
        if not used[minedge[1]]:
            continue
        v = minedge[1]
        d[v] = minedge[0]
        used[v] = False
        for e in edge[v]:
            if used[e[1]]:
                heapq.heappush(edgelist,[e[0]+d[v],e[1]])
    return d


def main():
    N = int(input())

    edge = [[] for i in range(N)]
    for _ in range(N-1):
        a, b, c = map(int, input().split())
        a -= 1
        b -= 1
        edge[a].append([c,b])
        edge[b].append([c,a])
    
    Q, K = map(int, input().split())
    K -= 1
    D = dijkstra_heap(K, N, edge)

    for _ in range(Q):
        x, y = map(int, input().split())
        x -= 1
        y -= 1
        print(D[x]+D[y])


if __name__ == '__main__':
    main()

Buggy python submission (verdict: Runtime Error):
def dfs(v, p, d):
	global D
	global S
	D[v-1] = d
	for g in G[v-1]:
		if S[g[0]-1]==0:
			continue
		S[g[0]-1] = 0
		dfs(g[0], v, d+g[1])

N = int(input())
G = [[] for _ in range(N)]
for _ in range(N-1):
	a, b, c = map(int, input().split())
	G[a-1].append([b, c])
	G[b-1].append([a, c])

S = [-1]*N
Q, K = map(int, input().split())
S[K-1] = 0
D = [0]*N
dfs(K, -1, 0)
for _ in range(Q):
	x, y = map(int, input().split())
	print(D[x-1] + D[y-1])


Fixed submission:
import heapq
import sys
input = sys.stdin.readline


def dijkstra_heap(s, n, edge):
    #始点sから各頂点への最短距離
    d = [float("inf")] * n
    used = [True] * n #True:未確定
    d[s] = 0
    used[s] = False
    edgelist = []
    for e in edge[s]:
        heapq.heappush(edgelist,e)
    while len(edgelist):
        minedge = heapq.heappop(edgelist)
        #まだ使われてない頂点の中から最小の距離のものを探す
        if not used[minedge[1]]:
            continue
        v = minedge[1]
        d[v] = minedge[0]
        used[v] = False
        for e in edge[v]:
            if used[e[1]]:
                heapq.heappush(edgelist,[e[0]+d[v],e[1]])
    return d


def main():
    N = int(input())

    edge = [[] for i in range(N)]
    for _ in range(N-1):
        a, b, c = map(int, input().split())
        a -= 1
        b -= 1
        edge[a].append([c,b])
        edge[b].append([c,a])
    
    Q, K = map(int, input().split())
    K -= 1
    D = dijkstra_heap(K, N, edge)

    for _ in range(Q):
        x, y = map(int, input().split())
        x -= 1
        y -= 1
        print(D[x]+D[y])


if __name__ == '__main__':
    main()

Buggy python submission (verdict: Runtime Error):
def dfs(v, p, d):
	global D
	global G
	D[v-1] = d
	while G[v-1]!=[]:
		g = G[v-1].pop()
		if g[0]==p:
			continue
		dfs(g[0], v, d+g[1])

N = int(input())
G = [[] for _ in range(N)]
for _ in range(N-1):
	a, b, c = map(int, input().split())
	G[a-1].append([b, c])
	G[b-1].append([a, c])

Q, K = map(int, input().split())
D = [0]*N
dfs(K, -1, 0)
for _ in range(Q):
	x, y = map(int, input().split())
	print(D[x-1] + D[y-1])


Fixed submission:
import heapq
import sys
input = sys.stdin.readline


def dijkstra_heap(s, n, edge):
    #始点sから各頂点への最短距離
    d = [float("inf")] * n
    used = [True] * n #True:未確定
    d[s] = 0
    used[s] = False
    edgelist = []
    for e in edge[s]:
        heapq.heappush(edgelist,e)
    while len(edgelist):
        minedge = heapq.heappop(edgelist)
        #まだ使われてない頂点の中から最小の距離のものを探す
        if not used[minedge[1]]:
            continue
        v = minedge[1]
        d[v] = minedge[0]
        used[v] = False
        for e in edge[v]:
            if used[e[1]]:
                heapq.heappush(edgelist,[e[0]+d[v],e[1]])
    return d


def main():
    N = int(input())

    edge = [[] for i in range(N)]
    for _ in range(N-1):
        a, b, c = map(int, input().split())
        a -= 1
        b -= 1
        edge[a].append([c,b])
        edge[b].append([c,a])
    
    Q, K = map(int, input().split())
    K -= 1
    D = dijkstra_heap(K, N, edge)

    for _ in range(Q):
        x, y = map(int, input().split())
        x -= 1
        y -= 1
        print(D[x]+D[y])


if __name__ == '__main__':
    main()