Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline

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()) # 頂点数
W = N - 1 # 辺の数

cost = [[float("inf") for _ in range(N)] for _ in range(N)] 
#cost[u][v] : 辺uvのコスト(存在しないときはinf この場合は10**10)
for i in range(W):
    x, y, z = [int(x) for x in input().split()]
    cost[x - 1][y - 1] = z
    cost[y - 1][x - 1] = z
Q, K = [int(x) for x in input().split()]

K_costs = dijkstra(K - 1, N, W, cost) # Kから各頂点へのコスト
ans = [] # クエリごとの答え

for _ in range(Q):
    x, y = [int(x) for x in input().split()]
    ans.append(K_costs[x - 1] + K_costs[y - 1])

for i in ans:
    print(i)

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

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()) # 頂点数
W = N - 1 # 辺の数

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

K_costs = dijkstra_heap(K - 1) # Kから各頂点へのコスト

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

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline

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()) # 頂点数
W = N - 1 # 辺の数

cost = [[float("inf") for _ in range(N)] for _ in range(N)] 
#cost[u][v] : 辺uvのコスト(存在しないときはinf この場合は10**10)
for i in range(W):
    x, y, z = [int(x) for x in input().split()]
    cost[x - 1][y - 1] = z
    cost[y - 1][x - 1] = z
Q, K = [int(x) for x in input().split()]

K_costs = dijkstra(K - 1, N, W, cost) # Kから各頂点へのコスト
ans = [] # クエリごとの答え

for _ in range(Q):
    x, y = [int(x) for x in input().split()]
    ans.append(K_costs[x - 1] + K_costs[y - 1])

for i in ans:
    print(i)

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

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()) # 頂点数
W = N - 1 # 辺の数

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

K_costs = dijkstra_heap(K - 1) # Kから各頂点へのコスト

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