import sys

sys.setrecursionlimit(1000000)



N = int(eval(input()))

X = [[] for _ in range(N)]



for i in range(N-1):

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

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

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



cost = [-1]*N

def calc(i,c):

    # 指定の頂点までのコストを代入

    cost[i] = c

    # 頂点から移動できる頂点があれば

    for ni,nc in X[i]:

        # 初期状態のままなら計算

        if cost[ni] == -1:

            calc(ni,c+nc)



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

# 事前計算

calc(K-1,0)



for i in range(Q):

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

    print((cost[x-1]+cost[y-1]))



import sys

sys.setrecursionlimit(1000000)



def dfs(n,p,d):

    visited[n] = True

    dist[n] = d

    for (i,c) in A[n]:

        if i != p and not visited[i]:

            dfs(i,n,d+c)



N = int(eval(input()))

A = [[] for _ in range(N)]

for _ in range(N-1):

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

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

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

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

B = []

for _ in range(Q):

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

    B.append((x,y))



dist = [-1]*N

visited = [False]*N

dfs(K-1,N,0)



for (x,y) in B:

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



import sys

sys.setrecursionlimit(1000000)
N = int(eval(input()))
X = [[] for _ in range(N)]
for i in range(N - 1):
    a, b, c = list(map(int, input().split()))
    X[a - 1].append((b - 1, c))
    X[b - 1].append((a - 1, c))
cost = [-1] * N


def calc(i, c):
    # 指定の頂点までのコストを代入
    cost[i] = c
    # 頂点から移動できる頂点があれば
    for ni, nc in X[i]:
        # 初期状態のままなら計算
        if cost[ni] == -1:
            calc(ni, c + nc)


Q, K = list(map(int, input().split()))
# 事前計算
calc(K - 1, 0)
for i in range(Q):
    x, y = list(map(int, input().split()))
    print((cost[x - 1] + cost[y - 1]))

import sys

sys.setrecursionlimit(1000000)


def dfs(n, p, d):
    visited[n] = True
    dist[n] = d
    for (i, c) in A[n]:
        if i != p and not visited[i]:
            dfs(i, n, d + c)


N = int(eval(input()))
A = [[] for _ in range(N)]
for _ in range(N - 1):
    a, b, c = list(map(int, input().split()))
    A[a - 1].append((b - 1, c))
    A[b - 1].append((a - 1, c))
Q, K = list(map(int, input().split()))
B = []
for _ in range(Q):
    x, y = list(map(int, input().split()))
    B.append((x, y))
dist = [-1] * N
visited = [False] * N
dfs(K - 1, N, 0)
for (x, y) in B:
    print((dist[x - 1] + dist[y - 1]))

-N = int(eval(input())) -X = [[] for _ in range(N)] -for i in range(N - 1): -    a, b, c = list(map(int, input().split())) -    X[a - 1].append((b - 1, c)) -    X[b - 1].append((a - 1, c)) -cost = [-1] * N -def calc(i, c): -    # 指定の頂点までのコストを代入 -    cost[i] = c -    # 頂点から移動できる頂点があれば -    for ni, nc in X[i]: -        # 初期状態のままなら計算 -        if cost[ni] == -1: -            calc(ni, c + nc) +def dfs(n, p, d): +    visited[n] = True +    dist[n] = d +    for (i, c) in A[n]: +        if i != p and not visited[i]: +            dfs(i, n, d + c) +N = int(eval(input())) +A = [[] for _ in range(N)] +for _ in range(N - 1): +    a, b, c = list(map(int, input().split())) +    A[a - 1].append((b - 1, c)) +    A[b - 1].append((a - 1, c)) -# 事前計算 -calc(K - 1, 0) -for i in range(Q): +B = [] +for _ in range(Q): -    print((cost[x - 1] + cost[y - 1])) +    B.append((x, y)) +dist = [-1] * N +visited = [False] * N +dfs(K - 1, N, 0) +for (x, y) in B: +    print((dist[x - 1] + dist[y - 1]))