Buggy python submission (verdict: Time Limit Exceeded):
from heapq import heappush, heappop

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

N = int(input())

edge = [[] for _ in range(N)]  # edge[i]: i から出る道の[重み、行き先]の配列

for _ in range(N-1):
    x, y, z = map(int, input().split())
    edge[x-1].append([z, y-1])
    edge[y-1].append([z, x-1])

Q, K = map(int, input().split())
z = dijkstra_heap(K-1)

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


Fixed submission:
import heapq
def dijkstra_heap(s):
    #始点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

N = int(input())

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

Q, K = map(int, input().split())

zk = dijkstra_heap(K-1)
#print(zk)

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

Buggy python submission (verdict: Time Limit Exceeded):
def dijkstra(s,n,w,cost):
    #始点sから各頂点への最短距離
    #n:頂点数,　w:辺の数, cost[u][v] : 辺uvのコスト(存在しないときはinf)
    d = [float("inf")] * n
    used = [False] * n
    d[s] = 0

    while True:
        v = -1
        #まだ使われてない頂点の中から最小の距離のものを探す
        for i in range(n):
            if (not used[i]) and (v == -1):
                v = i
            elif (not used[i]) and d[i] < d[v]:
                v = i
        if v == -1:
            break
        used[v] = True

        for j in range(n):
            d[j] = min(d[j],d[v]+cost[v][j])
    return d

################################
N = int(input())

cost = [[float("inf") for _ in range(N)] for _ in range(N)]
for _ in range(N-1):
    a, b, c = map(int, input().split())
    cost[a-1][b-1] = c
    cost[b-1][a-1] = c

Q, K = map(int, input().split())

zk = dijkstra(K-1, N, N-1, cost)
#print(zk)

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


Fixed submission:
import heapq
def dijkstra_heap(s):
    #始点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

N = int(input())

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

Q, K = map(int, input().split())

zk = dijkstra_heap(K-1)
#print(zk)

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