import heapq



N = int(eval(input()))

edges = {i: [] for i in range(N)}

for i in range(N - 1):

    a, b, c = list(map(int, input().split(" ")))

    edges[a - 1].append((b - 1, c))

    edges[b - 1].append((a - 1, c))

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



# Kからの最短距離を計算

dist = [-1 for i in range(N)]

que = []

heapq.heappush(que, (0, K - 1, -1)) # (distance, index, parent)

while len(que) > 0:

    d, ind, par = heapq.heappop(que)

    if dist[ind] != -1:

        continue

    dist[ind] = d

    for edge in edges[ind]:

        if edge[0] == par:

            continue

        heapq.heappush(que, (d + edge[1], edge[0], ind))



for _ in range(Q):

    x, y = list(map(int, input().split(" ")))

    print((dist[x - 1] + dist[y - 1]))
import heapq

import sys

input = sys.stdin.readline



N = int(eval(input()))

edges = {i: [] for i in range(N)}

for i in range(N - 1):

    a, b, c = list(map(int, input().split(" ")))

    edges[a - 1].append((b - 1, c))

    edges[b - 1].append((a - 1, c))

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



# Kからの最短距離を計算

dist = [-1 for i in range(N)]

que = []

heapq.heappush(que, (0, K - 1, -1)) # (distance, index, parent)

while len(que) > 0:

    d, ind, par = heapq.heappop(que)

    if dist[ind] != -1:

        continue

    dist[ind] = d

    for edge in edges[ind]:

        if edge[0] == par:

            continue

        heapq.heappush(que, (d + edge[1], edge[0], ind))



for _ in range(Q):

    x, y = list(map(int, input().split(" ")))

    print((dist[x - 1] + dist[y - 1]))
import heapq

N = int(eval(input()))
edges = {i: [] for i in range(N)}
for i in range(N - 1):
    a, b, c = list(map(int, input().split(" ")))
    edges[a - 1].append((b - 1, c))
    edges[b - 1].append((a - 1, c))
Q, K = list(map(int, input().split(" ")))
# Kからの最短距離を計算
dist = [-1 for i in range(N)]
que = []
heapq.heappush(que, (0, K - 1, -1))  # (distance, index, parent)
while len(que) > 0:
    d, ind, par = heapq.heappop(que)
    if dist[ind] != -1:
        continue
    dist[ind] = d
    for edge in edges[ind]:
        if edge[0] == par:
            continue
        heapq.heappush(que, (d + edge[1], edge[0], ind))
for _ in range(Q):
    x, y = list(map(int, input().split(" ")))
    print((dist[x - 1] + dist[y - 1]))

import heapq
import sys

input = sys.stdin.readline
N = int(eval(input()))
edges = {i: [] for i in range(N)}
for i in range(N - 1):
    a, b, c = list(map(int, input().split(" ")))
    edges[a - 1].append((b - 1, c))
    edges[b - 1].append((a - 1, c))
Q, K = list(map(int, input().split(" ")))
# Kからの最短距離を計算
dist = [-1 for i in range(N)]
que = []
heapq.heappush(que, (0, K - 1, -1))  # (distance, index, parent)
while len(que) > 0:
    d, ind, par = heapq.heappop(que)
    if dist[ind] != -1:
        continue
    dist[ind] = d
    for edge in edges[ind]:
        if edge[0] == par:
            continue
        heapq.heappush(que, (d + edge[1], edge[0], ind))
for _ in range(Q):
    x, y = list(map(int, input().split(" ")))
    print((dist[x - 1] + dist[y - 1]))

+import sys +input = sys.stdin.readline