n=int(eval(input()))

dist=[[] for i in range(n)]

for i in range(n-1):

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

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

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



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



INF=10**9+1

distFromK=[-1 for i in range(n)]



# start from K

stack=[]

stack.append([K-1,0,-1])

while stack:

  node=stack.pop()

  v=node[0]

  d=node[1]

  parent=node[2]

  if distFromK[v]==-1:

    distFromK[v]=d

  else:

    distFromK[v]=min(distFromK[v],d)

  targets=dist[v]

  for child in targets:

    if child[0]==parent or child[0]==v:

      continue

    stack.append([child[0],d+child[1],v])



for i in range(Q):

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

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

# Kから各頂点への距離をメモする

# xからKの距離とyからKの距離を足したものが答え



import sys

readline = sys.stdin.readline



N = int(readline())

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

for i in range(N - 1):

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

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

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



dist = [-1 for i in range(N)] # Kからの距離

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



dist[K - 1] = 0

from collections import deque

# 頂点、Kからの距離, 親

q = deque([[K - 1, 0, -1]])

while q:

  v, d, parent = q.popleft()

  dist[v] = d

  for child in G[v]:

    if child[0] == parent:

      continue

    q.append([child[0],d + child[1], v])



for i in range(Q):

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

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

  
n = int(eval(input()))
dist = [[] for i in range(n)]
for i in range(n - 1):
    a, b, c = list(map(int, input().split()))
    dist[a - 1].append([b - 1, c])
    dist[b - 1].append([a - 1, c])
Q, K = list(map(int, input().split()))
INF = 10**9 + 1
distFromK = [-1 for i in range(n)]
# start from K
stack = []
stack.append([K - 1, 0, -1])
while stack:
    node = stack.pop()
    v = node[0]
    d = node[1]
    parent = node[2]
    if distFromK[v] == -1:
        distFromK[v] = d
    else:
        distFromK[v] = min(distFromK[v], d)
    targets = dist[v]
    for child in targets:
        if child[0] == parent or child[0] == v:
            continue
        stack.append([child[0], d + child[1], v])
for i in range(Q):
    x, y = list(map(int, input().split()))
    print((distFromK[x - 1] + distFromK[y - 1]))

# Kから各頂点への距離をメモする
# xからKの距離とyからKの距離を足したものが答え
import sys

readline = sys.stdin.readline
N = int(readline())
G = [[] for i in range(N)]
for i in range(N - 1):
    a, b, c = list(map(int, readline().split()))
    G[a - 1].append((b - 1, c))
    G[b - 1].append((a - 1, c))
dist = [-1 for i in range(N)]  # Kからの距離
Q, K = list(map(int, readline().split()))
dist[K - 1] = 0
from collections import deque

# 頂点、Kからの距離, 親
q = deque([[K - 1, 0, -1]])
while q:
    v, d, parent = q.popleft()
    dist[v] = d
    for child in G[v]:
        if child[0] == parent:
            continue
        q.append([child[0], d + child[1], v])
for i in range(Q):
    x, y = list(map(int, readline().split()))
    print((dist[x - 1] + dist[y - 1]))

-n = int(eval(input())) -dist = [[] for i in range(n)] -for i in range(n - 1): -    a, b, c = list(map(int, input().split())) -    dist[a - 1].append([b - 1, c]) -    dist[b - 1].append([a - 1, c]) -Q, K = list(map(int, input().split())) -INF = 10**9 + 1 -distFromK = [-1 for i in range(n)] -# start from K -stack = [] -stack.append([K - 1, 0, -1]) -while stack: -    node = stack.pop() -    v = node[0] -    d = node[1] -    parent = node[2] -    if distFromK[v] == -1: -        distFromK[v] = d -    else: -        distFromK[v] = min(distFromK[v], d) -    targets = dist[v] -    for child in targets: -        if child[0] == parent or child[0] == v: +# Kから各頂点への距離をメモする +# xからKの距離とyからKの距離を足したものが答え +import sys + +readline = sys.stdin.readline +N = int(readline()) +G = [[] for i in range(N)] +for i in range(N - 1): +    a, b, c = list(map(int, readline().split())) +    G[a - 1].append((b - 1, c)) +    G[b - 1].append((a - 1, c)) +dist = [-1 for i in range(N)]  # Kからの距離 +Q, K = list(map(int, readline().split())) +dist[K - 1] = 0 +from collections import deque + +# 頂点、Kからの距離, 親 +q = deque([[K - 1, 0, -1]]) +while q: +    v, d, parent = q.popleft() +    dist[v] = d +    for child in G[v]: +        if child[0] == parent: -        stack.append([child[0], d + child[1], v]) +        q.append([child[0], d + child[1], v]) -    x, y = list(map(int, input().split())) -    print((distFromK[x - 1] + distFromK[y - 1])) +    x, y = list(map(int, readline().split())) +    print((dist[x - 1] + dist[y - 1]))