CodeEff/ECCO huggingface dataset:
# Original solution
n = int(eval(input()))
graph = [dict() for _ in range(n)]

for i in range(n - 1):
    a, b, c = list(map(int, input().split()))
    graph[a - 1][b - 1] = c
    graph[b - 1][a - 1] = c

q, k = list(map(int, input().split()))
xy = []
for i in range(q):
    x, y = list(map(int, input().split()))
    xy.append([x-1, y-1])
depth = [0 for _ in range(n)]
prev = [False for _ in range(n)]
depth[k-1] = 0

import sys
sys.setrecursionlimit(10 ** 6)


def dfs(now, graph, depth, prev):
    if len(graph[now]) == 0 or prev[now]:
        return
    prev[now] = True
    for k, v in list(graph[now].items()):
        if not prev[k]:
            depth[k] += depth[now] + v
            dfs(k, graph, depth, prev)
    return depth


depth = dfs(k-1, graph, depth, prev)
for x, y in xy:
    print((depth[x] + depth[y]))


# Optimized solution
n = int(eval(input()))
graph = [dict() for _ in range(n)]

for i in range(n - 1):
    a, b, c = list(map(int, input().split()))
    graph[a - 1][b - 1] = c
    graph[b - 1][a - 1] = c

q, k = list(map(int, input().split()))
xy = []
for i in range(q):
    x, y = list(map(int, input().split()))
    xy.append([x - 1, y - 1])
depth = [0 for _ in range(n)]
prev = [False for _ in range(n)]
depth[k - 1] = 0

import sys

sys.setrecursionlimit(10 ** 6)


def dfs(now, prev, cost, graph, depth):
    depth[now] = cost
    for k, v in list(graph[now].items()):
        if k == prev:
            continue
        dfs(k, now, cost + v,  graph, depth)
    return depth


depth = dfs(k - 1, -1, 0, graph, depth)
for x, y in xy:
    print((depth[x] + depth[y]))
# Original solution
# disjoint setかと思いきやDFS
# どこが悪いのかさっっっっっっぱりわからん...
# あ 再帰にしてるからその上限か
import sys

sys.setrecursionlimit(10000000)

n = int(eval(input()))
graph = [[] for _ in range(n)]

for _ in range(n - 1):
    a, b, c = list(map(int, input().split()))
    a, b = a - 1, b - 1
    graph[a].append([b, c])
    graph[b].append([a, c])

q, k = list(map(int, input().split()))
k = k - 1
# kからスタートしてぜんぶたどる
ans_list = [0 for _ in range(n)]


def dfs(node, used, weight):
    for data in graph[node]:
        if data[0] not in used:
            next_used = used[:]
            next_used.append(data[0])
            dfs(data[0], next_used, weight + data[1])
    ans_list[node] = weight


dfs(k, [k], 0)

for _ in range(q):
    a, b = [int(x) - 1 for x in input().split()]
    print((ans_list[a] + ans_list[b]))


# Optimized solution
# disjoint setかと思いきやDFS
# したらTLEになったので根本的にやり方が間違ってるということ
# よくよく考えたらdfsなのでそこにたどり着いたときが最短距離だわ ムダに探索してた


import sys

sys.setrecursionlimit(10000000)

n = int(eval(input()))
graph = [[] for _ in range(n)]

for _ in range(n - 1):
    a, b, c = list(map(int, input().split()))
    a, b = a - 1, b - 1
    graph[a].append([b, c])
    graph[b].append([a, c])

q, k = list(map(int, input().split()))
k = k - 1
# kからスタートしてぜんぶたどる
ans_list = [0 for _ in range(n)]


def dfs(node, weight):
    for data in graph[node]:
        if data[0] not in used:
            used.append(data[0])
            dfs(data[0], weight + data[1])
    ans_list[node] = weight


used = [k]
dfs(k, 0)

for _ in range(q):
    a, b = [int(x) - 1 for x in input().split()]
    print((ans_list[a] + ans_list[b]))
# Original solution
from collections import deque

N = int(input())
E = [[] for _ in range(N)]
for _ in range(N-1):
    a, b, c = map(int, input().split())
    E[a-1].append((b-1, c))
    E[b-1].append((a-1, c))

Q, K = map(int, input().split())
distance = [0] * N
stack = deque([K-1])
checked = [False] * N
while stack:
    v = stack.pop()
    for u, c in E[v]:
        if checked[u]:
            continue
        distance[u] = distance[v] + c
        stack.append(u)
    checked[v] = True

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

[print(a) for a in ans]


# Optimized solution
from collections import deque

N = int(input())
E = [[] for _ in range(N)]
for _ in range(N-1):
    a, b, c = map(int, input().split())
    E[a-1].append((b-1, c))
    E[b-1].append((a-1, c))

Q, K = map(int, input().split())
distance = [0] * N
stack = deque([K-1])
# checked = [False] * N
while stack:
    v = stack.pop()
    for u, c in E[v]:
        if distance[u] != 0:
            continue
        distance[u] = distance[v] + c
        stack.append(u)
    # checked[v] = True

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

[print(a) for a in ans]
# Original solution
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]))

# Optimized solution
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]))
# Original solution
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
s = [(K - 1, 0)]
while s:
  v, c = s.pop()
  D[v] = c
  for vv, cc in G[v]:
    if D[vv] < 0:
      s.append((vv, cc + c))
  
for x, y in XY:
  print((D[x - 1] + D[y - 1]))

# Optimized solution
import sys
input = sys.stdin.readline

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
s = [(K - 1, 0)]
while s:
  v, c = s.pop()
  D[v] = c
  for vv, cc in G[v]:
    if D[vv] < 0:
      s.append((vv, cc + c))
  
for x, y in XY:
  print((D[x - 1] + D[y - 1]))
# Original solution
import sys
input = sys.stdin.readline

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
s = [(K - 1, 0)]
while s:
  v, c = s.pop()
  D[v] = c
  for vv, cc in G[v]:
    if D[vv] < 0:
      s.append((vv, cc + c))
  
for x, y in XY:
  print((D[x - 1] + D[y - 1]))

# Optimized solution
import sys
input = sys.stdin.readline

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
s = [(K - 1, 0)]
while s:
  v, c = s.pop()
  D[v] = c
  for vv, cc in G[v]:
    if D[vv] < 0:
      s.append((vv, cc + c))

for _ in range(Q):
  x, y = list(map(int, input().split()))
  print((D[x - 1] + D[y - 1]))
# Original solution
def dfs(k, path, n):
    """連結情報path,頂点の個数Nが与えられたとき
    頂点Kから各頂点までの距離をlistで返す"""
    from collections import deque # deque(),popleft()
    dist = [-1] * n
    dist[k] = 0
    que = deque([])
    que.append(k)
    while que:
        label = que.popleft()
        for i,c in path[label]: # 頂点labelと連結している頂点iと距離c
            if dist[i] == -1:
                dist[i] = dist[label] + c
                que += [i] # 次に調べる頂点に
    return dist

n = int(eval(input()))
path = [[] for i in range(n)]
# path = [{1,2}] * 3
# print(path)
for i in range(n-1):
    a, b, c = list(map(int, input().split()))
#     print(a,b,c)
    path[a-1] += [(b-1, c)]
    path[b-1] += [(a-1, c)]

q, k = list(map(int, input().split()))
xy = [list(map(int, input().split())) for i in range(q)]
dis = dfs(k-1, path, n)
for i in range(q):
    print((dis[xy[i][0] - 1] + dis[xy[i][1] - 1]))


# Optimized solution
from sys import stdin
input = stdin.readline

def dfs(k, path, n):
    """連結情報path,頂点の個数Nが与えられたとき
    頂点Kから各頂点までの距離をlistで返す"""
    from collections import deque # deque(),popleft()
    dist = [-1] * n
    dist[k] = 0
    que = deque([])
    que.append(k)
    while que:
        label = que.popleft()
        for i,c in path[label]: # 頂点labelと連結している頂点iと距離c
            if dist[i] == -1:
                dist[i] = dist[label] + c
                que += [i] # 次に調べる頂点に
    return dist

n = int(eval(input()))
path = [[] for i in range(n)]
# path = [{1,2}] * 3
# print(path)
for i in range(n-1):
    a, b, c = list(map(int, input().split()))
#     print(a,b,c)
    path[a-1] += [(b-1, c)]
    path[b-1] += [(a-1, c)]

q, k = list(map(int, input().split()))
xy = [list(map(int, input().split())) for i in range(q)]
dis = dfs(k-1, path, n)
for i in range(q):
    print((dis[xy[i][0] - 1] + dis[xy[i][1] - 1]))
# Original solution
import queue

def ans():
    def dfs(v, p, d):
        visited = []
        q = queue.Queue()
        for i in tree[v]:
            q.put((i[0], i[1], p))
        visited.append(v)
        while not q.empty():
            e, cost, parent = q.get()
            if (e in visited): continue
            if e == parent : continue
            visited.append(e)
            depth[e] = cost
            for i in tree[e]:
                if (i[0] in visited): continue
                q.put((i[0], i[1] + cost, e))

    n = int(eval(input()))
    tree = [[] for _ in range(n+1)]
    depth = [0] * (n * 2)
    for _ in range(1,n):
        a, b, c = list(map(int, input().split()))
        tree[a].append((b,c))
        tree[b].append((a,c))

    q, k = list(map(int, input().split()))
    dfs(k, -1, 0)
    for _ in range(q):
        x, y = list(map(int, input().split()))
        print((depth[x] + depth[y]))

ans()

# Optimized solution
import sys
sys.setrecursionlimit(1000000)
 
def ans():
    def dfs(v, p, d):
        depth[v] = d
        for e, cost in tree[v]:
            if e == p:
                continue
            dfs(e, v, d + cost)
 
    n = int(eval(input()))
    tree = [[] for _ in range(n + 1)]
    depth = [0] * (n * 2)
    for _ in range(1, n):
        a, b, c = list(map(int, input().split()))
        tree[a].append((b, c))
        tree[b].append((a, c))
 
    q, k = list(map(int, input().split()))
    dfs(k, -1, 0)
    for _ in range(q):
        x, y = list(map(int, input().split()))
        print((depth[x] + depth[y]))
 
 
ans()
# Original solution
#!usr/bin/env python3
from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS():return list(map(list, sys.stdin.readline().split()))
def S(): return list(sys.stdin.readline())[:-1]
def IR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = I()
    return l
def LIR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = LI()
    return l
def SR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = S()
    return l
def LSR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = LS()
    return l
sys.setrecursionlimit(1000000)
mod = 1000000007
dire4 = [(1,0), (0,1), (-1,0), (0,-1)]
dire8 = [(1, 0), (1, 1), (0, 1), (-1, 1), (-1, 0), (-1, -1), (0, -1), (1, -1)]

N = I()
abc = LIR(N-1)
Q, K = LI()
xy = LIR(Q)

abc = list([[x[0]-1, x[1]-1, x[2]] for x in abc])
xy = list([[x[0]-1, x[1]-1] for x in xy])
K = K-1

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

edge = [[0]*N for i in range(N)]
for a, b, c in abc:
    edge[a][b] = c
    edge[b][a] = c
    utov[a].append(b)
    utov[b].append(a)

bfs_map = [-1]*N

q = deque()
q.append(K)
bfs_map[K] = 0

while q:
    u = q.popleft()
    for v in utov[u]:
        if (bfs_map[v] == -1) or (bfs_map[v] > bfs_map[u]+edge[u][v]):
            bfs_map[v] = bfs_map[u]+edge[u][v]
            q.append(v)

for x, y in xy:
    print((bfs_map[x]+bfs_map[y]))


# Optimized solution
#!usr/bin/env python3
from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS():return list(map(list, sys.stdin.readline().split()))
def S(): return list(sys.stdin.readline())[:-1]
def IR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = I()
    return l
def LIR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = LI()
    return l
def SR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = S()
    return l
def LSR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = LS()
    return l
sys.setrecursionlimit(1000000)
mod = 1000000007
dire4 = [(1,0), (0,1), (-1,0), (0,-1)]
dire8 = [(1, 0), (1, 1), (0, 1), (-1, 1), (-1, 0), (-1, -1), (0, -1), (1, -1)]

N = I()
abc = LIR(N-1)
Q, K = LI()
xy = LIR(Q)


K = K

utov = [[] for i in range(N+1)]
for a, b, c in abc:
    utov[a].append((b,c))
    utov[b].append((a,c))

bfs_map = [-1]*(N+1)

q = deque()
q.append(K)
bfs_map[K] = 0

while q:
    u = q.popleft()
    for v, cost in utov[u]:
        if (bfs_map[v] == -1) or (bfs_map[v] > bfs_map[u]+cost):
            bfs_map[v] = bfs_map[u] + cost
            q.append(v)


for x, y in xy:
    print((bfs_map[x]+bfs_map[y]))
# Original solution
from collections import deque
import sys
sys.setrecursionlimit(10 ** 9)


N = int(eval(input()))

G = [[] for _ in range(N + 1)]
for i in range(N - 1):
    a, b, c = [int(_) for _ in input().split()]
    G[a].append((b, c))
    G[b].append((a, c))
Q, K = [int(_) for _ in input().split()]

# make tree
G2 = [-1] * (N + 1)


def f(v, c, p):
    if G2[v] != -1: return
    G2[v] = c

    for nv, nc in G[v]:
        if nv == p: continue
        f(nv, nc + c, v)


f(K, 0, 0)

for i in range(Q):
    x,y = [int(_) for _ in input().split()]
    print((G2[x]+G2[y]))


# Optimized solution
from collections import deque
import sys

sys.setrecursionlimit(10 ** 9)

N = int(eval(input()))

G = [[] for _ in range(N + 1)]
for i in range(N - 1):
    a, b, c = [int(_) for _ in input().split()]
    G[a].append((b, c))
    G[b].append((a, c))
Q, K = [int(_) for _ in input().split()]

# make tree
G2 = [10 ** 12] * (N + 1)

G2[K] = 0


def f():
    que = deque()
    que.append((K, 0))
    while que:
        v, p = que.pop()
        for nv, nc in G[v]:
            if nv == p: continue
            G2[nv] = G2[v] + nc
            que.append((nv, v))


f()

for i in range(Q):
    x, y = [int(_) for _ in input().split()]
    print((G2[x] + G2[y]))
# Original solution
import heapq
 
def dijkstra_heap(s,edge):
    #始点sから各頂点への最短距離
    d = [10**10]*n
    used = [True] * n #True:未確定
    d[s] = 0
    used[s] = False
    edgelist = []
    for a,b in edge[s]:
        heapq.heappush(edgelist,a*(10**6)+b)
    while len(edgelist):
        minedge = heapq.heappop(edgelist)
        #まだ使われてない頂点の中から最小の距離のものを探す
        if not used[minedge%(10**6)]:
            continue
        v = minedge%(10**6)
        d[v] = minedge//(10**6)
        used[v] = False
        for e in edge[v]:
            if used[e[1]]:
                heapq.heappush(edgelist,(e[0]+d[v])*(10**6)+e[1])
    return d
 
################################
N = int(eval(input()))
n = N
w = N
 
edge = tuple([] for i in range(n))
#edge[i] : iから出る道の[重み,行先]の配列
for i in range(w-1):
    a,b,c = list(map(int,input().split()))
    edge[a-1].append([c,b-1])
    edge[b-1].append([c,a-1])

dist = []
for i in range(N):
    dist.append(dijkstra_heap(i,edge))

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

# Optimized solution
import heapq

"""
https://juppy.hatenablog.com/entry/2019/02/18/%E8%9F%BB%E6%9C%AC_python_%E3%83%97%E3%83%A9%E3%82%A4%E3%82%AA%E3%83%AA%E3%83%86%E3%82%A3%E3%82%AD%E3%83%A5%E3%83%BC%28heapq%29%E3%82%92%E7%94%A8%E3%81%84%E3%81%9F%E3%83%97%E3%83%AA%E3%83%A0%E6%B3%95
"""

def dijkstra_heap(s,edge):
    #始点sから各頂点への最短距離
    d = [10**10]*n
    used = [True] * n #True:未確定
    d[s] = 0
    used[s] = False
    edgelist = []
    for a,b in edge[s]:
        heapq.heappush(edgelist,a*(10**6)+b)
    while len(edgelist):
        minedge = heapq.heappop(edgelist)
        #まだ使われてない頂点の中から最小の距離のものを探す
        if not used[minedge%(10**6)]:
            continue
        v = minedge%(10**6)
        d[v] = minedge//(10**6)
        used[v] = False
        for e in edge[v]:
            if used[e[1]]:
                heapq.heappush(edgelist,(e[0]+d[v])*(10**6)+e[1])
    return d
 
################################
N = int(eval(input()))
n = N
w = N
 
edge = tuple([] for i in range(n))
#edge[i] : iから出る道の[重み,行先]の配列
for i in range(w-1):
    a,b,c = list(map(int,input().split()))
    edge[a-1].append([c,b-1])
    edge[b-1].append([c,a-1])


Q,K = list(map(int,input().split()))
dist = dijkstra_heap(K-1,edge)
 
for i in range(Q):
    x,y = list(map(int,input().split()))
    print((int(dist[x-1]+dist[y-1])))
# Original solution

from heapq import heappush, heappop
INF = 10**20
def dijkstra(N, G, s):
    dist = [INF] * N
    que = [(0, s)]
    dist[s] = 0
    while que:
        c, v = heappop(que)
        if dist[v] < c:
            continue
        for t, cost in G[v]:
            if dist[v] + cost < dist[t]:
                dist[t] = dist[v] + cost
                heappush(que, (dist[t], t))
    return dist

N = int(eval(input()))
edge = [[] for _ in range(N)]

for _ in range(N-1):
    a,b,c = list(map(int,input().split()))
    edge[a-1].append((b-1,c))
    edge[b-1].append((a-1,c))

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

dist = dijkstra(N,edge,K-1)

for _ in range(Q):
    x,y = list(map(int,input().split()))
    print((dist[x-1]+dist[y-1]))

# Optimized solution

from heapq import heappush, heappop
INF = float("inf")
def dijkstra(N, G, s):
    dist = [INF] * N
    que = [(0, s)]
    dist[s] = 0
    while que:
        c, v = heappop(que)
        if dist[v] < c:
            continue
        for t, cost in G[v]:
            if dist[v] + cost < dist[t]:
                dist[t] = dist[v] + cost
                heappush(que, (dist[t], t))
    return dist

N = int(eval(input()))
edge = [[] for _ in range(N)]

for _ in range(N-1):
    a,b,c = list(map(int,input().split()))
    edge[a-1].append((b-1,c))
    edge[b-1].append((a-1,c))

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

dist = dijkstra(N,edge,K-1)

for _ in range(Q):
    x,y = list(map(int,input().split()))
    print((dist[x-1]+dist[y-1]))
# Original solution
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from operator import mul
from functools import reduce


sys.setrecursionlimit(2147483647)
INF = 10 ** 13
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007


n = I()
G = [[] for _ in range(n)]
for a, b, c in LIR(n - 1):
    G[a - 1] += [(b - 1, c)]
    G[b - 1] += [(a - 1, c)]

q, k = LI()
dq = deque([k - 1])
dist = [-1] * n
dist[k - 1] = 0
while dq:
    u = dq.pop()
    for v, c in G[u]:
        if dist[v] == -1:
            dist[v] = dist[u] + c
            dq += [v]

for _ in range(q):
    x, y = LI()
    print((dist[x - 1] + dist[y - 1]))




# Optimized solution
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from operator import mul
from functools import reduce


sys.setrecursionlimit(2147483647)
INF = 10 ** 13
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007


n = I()
G = [[] for _ in range(n)]
for a, b, c in LIR(n - 1):
    G[a - 1] += [(b - 1, c)]
    G[b - 1] += [(a - 1, c)]

q, k = LI()
dq = deque([k - 1])
dist = [-1] * n
dist[k - 1] = 0
while dq:
    u = dq.popleft()
    for v, c in G[u]:
        if dist[v] == -1:
            dist[v] = dist[u] + c
            dq += [v]

for _ in range(q):
    x, y = LI()
    print((dist[x - 1] + dist[y - 1]))
# Original solution
from collections import defaultdict
n = int(eval(input()))
tree = dict()
graph = defaultdict(list)

for i in range(n-1):
    a,b,c = (int(x) for x in input().split())
    tree[a,b] = c
    tree[b,a] = c
    graph[a].append(b)
    graph[b].append(a)

q, k = (int(x) for x in input().split())

dist = dict()
stack = [k,]
visited = [False for _ in range(n+1)]
dist[k] = 0
visited[k] = True
while stack:
    new_stack = []
    for node in stack:
        for next_node in graph[node]:
            if not visited[next_node]:
                visited[next_node]  = True
                dist[next_node] = dist[node] + tree[node, next_node]
                new_stack.extend(graph[node])
    stack = new_stack
for _ in range(q):
    x, y = (int(x) for x in input().split())
    print((dist[x] + dist[y]))


# Optimized solution
from collections import defaultdict
n = int(eval(input()))
tree = dict()
graph = defaultdict(list)

for i in range(n-1):
    a,b,c = (int(x) for x in input().split())
    tree[a,b] = c
    tree[b,a] = c
    graph[a].append(b)
    graph[b].append(a)

q, k = (int(x) for x in input().split())

dist = dict()
stack = [k,]
visited = [False for _ in range(n+1)]
dist[k] = 0
visited[k] = True

while stack:
    new_stack = []
    for node in stack:
        for next_node in graph[node]:
            if not visited[next_node]:
                visited[next_node]  = True
                dist[next_node] = dist[node] + tree[node, next_node]
                new_stack.append(next_node)
    stack = new_stack

for _ in range(q):
    x, y = (int(x) for x in input().split())
    print((dist[x] + dist[y]))