from collections import deque



N = int(eval(input()))



G = [set() for i in range(N)]

for _ in range(N-1):

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

    G[a-1].add((b, c))

    G[b-1].add((a, c))



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



visited = [False] * N

visited[K-1] = True



distance = [-1]*N



que = deque([(K, 0)])

while que:

    cur, d = que.popleft()

    distance[cur-1] = d



    for nx, nd in G[cur-1]:

        if visited[nx-1] is False:

            visited[nx-1] = True

            que.append((nx, d+nd))



for i in range(Q):

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

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

import sys

sys.setrecursionlimit(1000000)



N = int(eval(input()))



G = [set() for i in range(N)]

for _ in range(N-1):

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

    G[a-1].add((b, c))

    G[b-1].add((a, c))



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



visited = [False] * N

visited[K-1] = True



distance = [-1]*N





def dfs(v, p, d):  # 現在の頂点v, vの親p, 現在の距離d

    distance[v-1] = d

    for i, c in G[v-1]:

        if visited[i-1] is False:

            if i == p:

                continue

            dfs(i, v, d+c)

    return





dfs(K, -1, 0)



for i in range(Q):

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

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

from collections import deque

N = int(eval(input()))
G = [set() for i in range(N)]
for _ in range(N - 1):
    a, b, c = list(map(int, input().split()))
    G[a - 1].add((b, c))
    G[b - 1].add((a, c))
Q, K = list(map(int, input().split()))
visited = [False] * N
visited[K - 1] = True
distance = [-1] * N
que = deque([(K, 0)])
while que:
    cur, d = que.popleft()
    distance[cur - 1] = d
    for nx, nd in G[cur - 1]:
        if visited[nx - 1] is False:
            visited[nx - 1] = True
            que.append((nx, d + nd))
for i in range(Q):
    x, y = list(map(int, input().split()))
    print((distance[x - 1] + distance[y - 1]))

import sys

sys.setrecursionlimit(1000000)
N = int(eval(input()))
G = [set() for i in range(N)]
for _ in range(N - 1):
    a, b, c = list(map(int, input().split()))
    G[a - 1].add((b, c))
    G[b - 1].add((a, c))
Q, K = list(map(int, input().split()))
visited = [False] * N
visited[K - 1] = True
distance = [-1] * N


def dfs(v, p, d):  # 現在の頂点v, vの親p, 現在の距離d
    distance[v - 1] = d
    for i, c in G[v - 1]:
        if visited[i - 1] is False:
            if i == p:
                continue
            dfs(i, v, d + c)
    return


dfs(K, -1, 0)
for i in range(Q):
    x, y = list(map(int, input().split()))
    print((distance[x - 1] + distance[y - 1]))

-from collections import deque +import sys +sys.setrecursionlimit(1000000) -que = deque([(K, 0)]) -while que: -    cur, d = que.popleft() -    distance[cur - 1] = d -    for nx, nd in G[cur - 1]: -        if visited[nx - 1] is False: -            visited[nx - 1] = True -            que.append((nx, d + nd)) + + +def dfs(v, p, d):  # 現在の頂点v, vの親p, 現在の距離d +    distance[v - 1] = d +    for i, c in G[v - 1]: +        if visited[i - 1] is False: +            if i == p: +                continue +            dfs(i, v, d + c) +    return + + +dfs(K, -1, 0)