Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
E = []
for i in range(N - 1):
        E.append([int(n) for n in input().split()])

Q, K = [int(n) for n in input().split()]
P = []
for i in range(Q):
        P.append([int(n) for n in input().split()])

def get_path(start, goal, prev):
    path = []
    now = goal
    path.append(now)
    while True:
        path.append(prev[now])
        if prev[now] == start: break
        now = prev[now]
    path.reverse()
    return path

def search(glaph, start, goal):
    MAX_VAL = 1e18
    g_size = N + 1
    visited = [False] * g_size
    cost = [MAX_VAL] * g_size
    prev = [None] * g_size
    cost[start] = 0
    prev[start] = start
    now = start
    while True:
        min = MAX_VAL
        next = -1
        visited[now] = True
        for i in range(g_size):
            if visited[i]: continue
            flag = False
            cc = None
            for [a, b, c] in E:
                    if (a == now and b == i) or (a == i and b == now):
                            flag = True
                            cc = c
                            break
            if flag:
                tmp_cost = cc + cost[now]
                if cost[i] > tmp_cost:
                    cost[i] = tmp_cost
                    prev[i] = now
            if min > cost[i]:
                min = cost[i]
                next = i
        if next == -1: break
        now = next
    return cost[goal]

for i in range(Q):
        [x, y] = P[i]
        xcost = search(E, x, K)
        ycost = search(E, K, y)
        cost = xcost + ycost
        print(cost)

Fixed submission:
#!/usr/bin/env python3
import sys

def dijkstra(graph: 'List[List[int]]', s: int):
    """
    特定の始点から各点への最短距離を求めます。
    重みが非負であることを前提とします。
    ================
    s: 始点
    graph: 隣接リスト
    ----------------
    d: sから各頂点への最短距離
    counter: sから各頂点への最短経路の個数
    """
    import heapq
    INF = 1e18
    d = [INF] * len(graph)
    d[s]  = 0
    counter = [0] * len(graph)
    counter[s] = 1

    q = []
    heapq.heappush(q, (0, s))
    while q:
        (dd, vv) = heapq.heappop(q)
        if d[vv] < dd:
            continue
        for (to, cost) in graph[vv]:
            if d[to] > (d[vv] + cost):
                d[to] = d[vv] + cost
                counter[to] = counter[vv]
                heapq.heappush(q, (d[to], to))
            elif d[to] == (d[vv] + cost):
                counter[to] += counter[vv]
    return d, counter


def solve(N: int, a: "List[int]", b: "List[int]", c: "List[int]", Q: int, K: int, x: "List[int]", y: "List[int]"):
    G = [[] for _ in range(N)]
    for i in range(N-1):
        G[a[i]-1].append((b[i]-1, c[i]))
        G[b[i]-1].append((a[i]-1, c[i]))
    dist, _ = dijkstra(G, K-1)

    for i in range(Q):
        print(dist[x[i]-1] + dist[y[i]-1])

    return


# Generated by 1.1.5 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    a = [int()] * (N - 1)  # type: "List[int]"
    b = [int()] * (N - 1)  # type: "List[int]"
    c = [int()] * (N - 1)  # type: "List[int]"
    for i in range(N - 1):
        a[i] = int(next(tokens))
        b[i] = int(next(tokens))
        c[i] = int(next(tokens))
    Q = int(next(tokens))  # type: int
    K = int(next(tokens))  # type: int
    x = [int()] * (Q)  # type: "List[int]"
    y = [int()] * (Q)  # type: "List[int]"
    for i in range(Q):
        x[i] = int(next(tokens))
        y[i] = int(next(tokens))
    solve(N, a, b, c, Q, K, x, y)

if __name__ == '__main__':
    main()