from collections import deque



N = int(eval(input()))

G = [[] * N for _ in range(N)]

for _ in range(N - 1):

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

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

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



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



D = [-1] * N

D[K - 1] = 0



q = deque([K - 1])

while q:

  n = q.popleft()

  for i, c in G[n]:

    if D[i] < 0:

      q.append(i)

      D[i] = D[n] + c



for _ in range(Q):

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

  print((D[x - 1] + D[y - 1]))
import sys

sys.setrecursionlimit(10**6)



N = int(eval(input()))

ABC = [list(map(int, input().split())) for _ in range(N - 1)]

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

XY = [list(map(int, input().split())) for _ in range(Q)]



G = [[] * N for _ in range(N)]

for a, b, c in ABC:

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

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



D = [-1] * N



def dfs(v, d):

  D[v] = d

  for i, c in G[v]:

    if D[i] < 0:

      dfs(i, d + c)



dfs(K - 1, 0)



for x, y in XY:

  print((D[x - 1] + D[y - 1]))
from collections import deque

N = int(eval(input()))
G = [[] * N for _ in range(N)]
for _ in range(N - 1):
    a, b, c = list(map(int, input().split()))
    G[a - 1].append((b - 1, c))
    G[b - 1].append((a - 1, c))
Q, K = list(map(int, input().split()))
D = [-1] * N
D[K - 1] = 0
q = deque([K - 1])
while q:
    n = q.popleft()
    for i, c in G[n]:
        if D[i] < 0:
            q.append(i)
            D[i] = D[n] + c
for _ in range(Q):
    x, y = list(map(int, input().split()))
    print((D[x - 1] + D[y - 1]))

import sys

sys.setrecursionlimit(10**6)
N = int(eval(input()))
ABC = [list(map(int, input().split())) for _ in range(N - 1)]
Q, K = list(map(int, input().split()))
XY = [list(map(int, input().split())) for _ in range(Q)]
G = [[] * N for _ in range(N)]
for a, b, c in ABC:
    G[a - 1].append((b - 1, c))
    G[b - 1].append((a - 1, c))
D = [-1] * N


def dfs(v, d):
    D[v] = d
    for i, c in G[v]:
        if D[i] < 0:
            dfs(i, d + c)


dfs(K - 1, 0)
for x, y in XY:
    print((D[x - 1] + D[y - 1]))

-from collections import deque +import sys +sys.setrecursionlimit(10**6) +ABC = [list(map(int, input().split())) for _ in range(N - 1)] +Q, K = list(map(int, input().split())) +XY = [list(map(int, input().split())) for _ in range(Q)] -for _ in range(N - 1): -    a, b, c = list(map(int, input().split())) +for a, b, c in ABC: -Q, K = list(map(int, input().split())) -D[K - 1] = 0 -q = deque([K - 1]) -while q: -    n = q.popleft() -    for i, c in G[n]: + + +def dfs(v, d): +    D[v] = d +    for i, c in G[v]: -            q.append(i) -            D[i] = D[n] + c -for _ in range(Q): -    x, y = list(map(int, input().split())) +            dfs(i, d + c) + + +dfs(K - 1, 0) +for x, y in XY: