CodeEff/ECCO huggingface dataset:
# Original solution
from collections import deque


def nearlist(N, LIST):  # 隣接リスト
    NEAR = [set() for _ in range(N)]
    COST = {}
    for a, b, c in LIST:
        NEAR[a - 1].add(b - 1)
        NEAR[b - 1].add(a - 1)
        COST[(a - 1, b - 1)] = c
        COST[(b - 1, a - 1)] = c
    return NEAR, COST


def bfs(NEAR, S, N):  # 幅優先探索  # キュー
    DIST = [-1 for _ in range(N)]  # 前処理
    DIST[S] = 0
    que, frag = deque([S]), set([S])

    while len(que) > 0:
        q = que.popleft()
        for i in NEAR[q]:  # 移動先の候補
            if i in frag:  # 処理済みか否か
                continue
            DIST[i] = DIST[q] + cost[(q, i)]
            que.append(i), frag.add(i)
    return DIST


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)]

near, cost = nearlist(n, abc)
dist = bfs(near, k - 1, n)
for x, y in xy:
    print((dist[x - 1] + dist[y - 1]))


# Optimized solution
from collections import deque


def nearlist(N, LIST):  # 隣接リスト
    NEAR = [{} for _ in range(N)]
    for a, b, c in LIST:
        NEAR[a - 1][b - 1] = c
        NEAR[b - 1][a - 1] = c
    return NEAR


def bfs(NEAR, S, N):  # 幅優先探索  # キュー
    DIST = [-1 for _ in range(N)]  # 前処理
    DIST[S] = 0
    que, frag = deque([S]), set([S])

    while len(que) > 0:
        q = que.popleft()
        for i, c in list(NEAR[q].items()):  # 移動先の候補
            if i in frag:  # 処理済みか否か
                continue
            DIST[i] = DIST[q] + c
            que.append(i), frag.add(i)
    return DIST


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)]

near = nearlist(n, abc)
dist = bfs(near, k - 1, n)
for x, y in xy:
    print((dist[x - 1] + dist[y - 1]))
# Original solution
from collections import deque


def nearlist(N, LIST):  # 隣接リスト
    NEAR = [{} for _ in range(N)]
    for a, b, c in LIST:
        NEAR[a - 1][b - 1] = c
        NEAR[b - 1][a - 1] = c
    return NEAR


def dfs(NEAR, S, N):  # 深優先探索  # スタック
    DIST = [-1 for _ in range(N)]  # 前処理
    DIST[S] = 0
    stack, frag = [S], set([S])

    while len(stack) > 0:
        q = stack.pop()
        for i, c in list(NEAR[q].items()):  # 移動先の候補
            if i in frag:  # 処理済みか否か
                continue
            DIST[i] = DIST[q] + c
            stack.append(i), frag.add(i)
    return DIST


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)]

near = nearlist(n, abc)
dist = dfs(near, k - 1, n)
for x, y in xy:
    print((dist[x - 1] + dist[y - 1]))


# Optimized solution
def nearlist(N, LIST):  # 隣接リスト
    NEAR = [{} for _ in range(N)]
    for a, b, c in LIST:
        NEAR[a - 1][b - 1] = c
        NEAR[b - 1][a - 1] = c
    return NEAR


def dfs(NEAR, S, N):  # 深優先探索  # スタック
    DIST = [-1 for _ in range(N)]  # 前処理
    DIST[S] = 0
    stack, frag = [S], set([S])

    while len(stack) > 0:
        q = stack.pop()
        for i, c in list(NEAR[q].items()):  # 移動先の候補
            if i in frag:  # 処理済みか否か
                continue
            DIST[i] = DIST[q] + c
            stack.append(i), frag.add(i)
    return DIST


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)]

near = nearlist(n, abc)
dist = dfs(near, k - 1, n)
for x, y in xy:
    print((dist[x - 1] + dist[y - 1]))
# Original solution
from collections import deque

N = int(eval(input()))

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

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

visited = [False] * N
visited[K-1] = True

distance = [-1]*N

que = deque([(K-1, 0)])
while que:
    cur, d = que.popleft()
    distance[cur] = d

    for nx, nd in enumerate(G[cur]):
        if nd != -1 and visited[nx] is False:
            visited[nx] = 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]))


# Optimized solution
from collections import deque

N = int(eval(input()))

G = set()
for _ in range(N-1):
    a, b, c = list(map(int, input().split()))
    G.add((a, b, 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 a, b, c in G:
        if a == cur and visited[b-1] is False:
            visited[b-1] = True
            que.append((b, d+c))
        elif b == cur and visited[a-1] is False:
            visited[a-1] = True
            que.append((a, d+c))

for i in range(Q):
    x, y = list(map(int, input().split()))
    print((distance[x-1]+distance[y-1]))
# Original solution
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]))


# Optimized solution
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]))
# Original solution
# -*- coding: utf-8 -*-


def main():
    from collections import defaultdict
    from collections import deque

    n = int(eval(input()))
    graph = [defaultdict(int) for _ in range(n)]
    visited = [False for _ in range(n)]
    dist = [0 for _ in range(n)]

    for i in range(n - 1):
        ai, bi, ci = list(map(int, input().split()))
        ai -= 1
        bi -= 1
        graph[ai][bi] = ci
        graph[bi][ai] = ci

    q, k = list(map(int, input().split()))
    k -= 1
    dist[k] = 0
    d = deque()
    d.append(k)

    while d:
        di = d.popleft()
        visited[di] = True

        for bi, ci in list(graph[di].items()):
            if visited[bi]:
                continue

            dist[bi] = dist[di] + ci
            d.append(bi)

    for j in range(q):
        xi, yi = list(map(int, input().split()))
        xi -= 1
        yi -= 1
        print((dist[xi] + dist[yi]))


if __name__ == '__main__':
    main()


# Optimized solution
# -*- coding: utf-8 -*-


def main():
    from collections import defaultdict
    from collections import deque

    n = int(eval(input()))
    graph = [defaultdict(int) for _ in range(n)]
    visited = [False for _ in range(n)]
    dist = [float('inf') for _ in range(n)]

    for i in range(n - 1):
        ai, bi, ci = list(map(int, input().split()))
        ai -= 1
        bi -= 1
        graph[ai][bi] = ci
        graph[bi][ai] = ci

    q, k = list(map(int, input().split()))
    k -= 1
    dist[k] = 0
    d = deque()
    d.append(k)

    while d:
        di = d.popleft()
        visited[di] = True

        for bi, ci in list(graph[di].items()):
            if visited[bi]:
                continue

            dist[bi] = dist[di] + ci
            d.append(bi)

    ans = [0 for _ in range(q)]

    for j in range(q):
        xi, yi = list(map(int, input().split()))
        xi -= 1
        yi -= 1
        ans[j] = dist[xi] + dist[yi]

    print(('\n'.join(map(str, ans))))


if __name__ == '__main__':
    main()
# Original solution
import sys
sys.setrecursionlimit(10**6)
n=int(input())
G=[[]for i in[0]*-~n]
for I in[lambda:map(int,input().split())]*~-n:a,b,c=I();G[a]+=(b,c),;G[b]+=(a,c),
q,k=I()
d=[0]*-~n
def f(v,p,u):
 d[v]=u
 for t,c in G[v]:p==t or f(t,v,u+c)
f(k,0,0)
for p in[print]*q:
 x,y=I()
 p(d[x]+d[y])


# Optimized solution
import sys;sys.setrecursionlimit(6**8);I=lambda:list(map(int,input().split()));n,=I();G=[[]for i in[0]*-~n]
for i in[0]*~-n:a,b,c=I();G[a]+=(b,c),;G[b]+=(a,c),
q,k=I()
def f(v,p,u):
 for t,c in G[v]:p==t or f(t,v,u+c)
 G[v]=u
f(k,0,0)
for p in[0]*q:x,y=I();print((G[x]+G[y]))
# Original solution
import queue
import sys
sys.setrecursionlimit(10**7)
n = int(eval(input()))

abc = [[] for i in range(n+1)]

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

Q,k = list(map(int,input().split()))
xy = [[int(i) for i in input().split()] for j in range(Q)]

q = queue.Queue()

q.put(k)

path = [-1]*(n+1)
path[k] = 0

def dfs(p):
    for i in range(len(abc[p])):
        num = abc[p][i][0]
        if path[num] != -1:
            continue
        else:
            path[num] = path[p] + abc[p][i][1]
            dfs(num)

dfs(k)

for i in range(Q):
    print((path[xy[i][0]]+path[xy[i][1]]))

# Optimized solution
import queue
n = int(eval(input()))

abc = [[] for i in range(n+1)]

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

Q,k = list(map(int,input().split()))
xy = [[int(i) for i in input().split()] for j in range(Q)]

q = queue.Queue()

q.put(k)

path = [-1]*(n+1)
path[k] = 0

while not q.empty():
    tmp = q.get()
    for i in range(len(abc[tmp])):
        #print(abc[tmp])
        #exit()
        num = abc[tmp][i][0]
        if path[num] != -1:
            continue
        else:
            path[num] = path[tmp] + abc[tmp][i][1]
            q.put(num)
            
for i in range(Q):
    print((path[xy[i][0]]+path[xy[i][1]]))
# Original solution
import sys
import math
import collections
import itertools
import array
import inspect

# Set max recursion limit
sys.setrecursionlimit(1000000)


# Debug output
def chkprint(*args):
    names = {
        id(v): k
        for k, v in list(inspect.currentframe().f_back.f_locals.items())
    }
    print((', '.join(
        names.get(id(arg), '???') + ' = ' + repr(arg) for arg in args)))


# Binary converter
def to_bin(x):
    return bin(x)[2:]


def li_input():
    return [int(_) for _ in input().split()]


# --------------------------------------------

dp = None
D = None
D_dfs_d = None


def dfs_d(v, pre, dist):
    global D_dfs_d

    D_dfs_d[v] = dist

    for i, d in enumerate(D[v]):
        if d != -1 and i != pre:
            dfs_d(i, v, dist + d)

    return


def main():
    global D
    global D_dfs_d

    N = int(eval(input()))
    D = [[-1] * (N + 1) for _ in range(N + 1)]
    D_dfs_d = [-1] * (N + 1)

    for i in range(N - 1):
        a, b, c = li_input()
        D[a][b] = c
        D[b][a] = c

    Q, K = li_input()

    # Kから各頂点までの長さをWFSで求める
    dfs_d(K, -1, 0)

    for i in range(Q):
        x, y = li_input()
        print((D_dfs_d[x] + D_dfs_d[y]))


main()


# Optimized solution
import sys
import math
import collections
import itertools
import array
import inspect

# Set max recursion limit
sys.setrecursionlimit(1000000)


# Debug output
def chkprint(*args):
    names = {
        id(v): k
        for k, v in list(inspect.currentframe().f_back.f_locals.items())
    }
    print((', '.join(
        names.get(id(arg), '???') + ' = ' + repr(arg) for arg in args)))


# Binary converter
def to_bin(x):
    return bin(x)[2:]


def li_input():
    return [int(_) for _ in input().split()]


# --------------------------------------------

dp = None
D = None
D_dfs_d = None


def dfs_d(v, pre, dist):
    global D_dfs_d

    D_dfs_d[v] = dist

    for next_v, d in D[v]:
        if next_v != pre:
            dfs_d(next_v, v, dist + d)

    return


def main():
    global D
    global D_dfs_d

    N = int(eval(input()))
    D_dfs_d = [-1] * (N + 1)
    D = [[] for _ in range(N + 1)]

    for i in range(N - 1):
        a, b, c = li_input()
        D[a].append((b, c))
        D[b].append((a, c))

    Q, K = li_input()

    # Kから各頂点までの長さをWFSで求める
    dfs_d(K, -1, 0)

    for i in range(Q):
        x, y = li_input()
        print((D_dfs_d[x] + D_dfs_d[y]))


main()
# Original solution
n=int(eval(input()))
es=[[] for _ in range(n)]

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

from heapq import heappush, heappop
def dijkstra(edges, size, source):
    distance = [float('inf')] * size
    distance[source] = 0
    visited = [False] * size
    pq = []
    heappush(pq, (0, source))
    while pq:
        dist_v, v = heappop(pq)
        visited[v] = True
        for u, weight in edges[v]:
            if not visited[u]:
                new_dist = dist_v + weight
                if distance[u] > new_dist:
                    distance[u] = new_dist
                    heappush(pq, (new_dist, u))
    return distance


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

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

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

q,k=list(map(int,input().split()))
k-=1
dist=[float('inf')]*n

dist[k]=0
stack=[k]
while stack:
    now=stack.pop()
    for e,w in es[now]:
        if dist[e]==float('inf'):
            dist[e]=dist[now]+w
            stack.append(e)

for _ in range(q):
    x,y=list(map(int,input().split()))
    x-=1
    y-=1
    print((dist[x]+dist[y]))
# Original solution
import sys
input = sys.stdin.readline

from collections import deque

N = int(eval(input()))

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

Q, K = list(map(int, input().split()))
distance = [-1] * (N + 1)
distance[K] = 0
que = deque()
q_append = que.append
for i in graph[K]:
    q_append(i)

q_left = que.popleft
while len(que) != 0:
    for _ in range(len(que)):
        abc = q_left()
        if distance[abc[0]] == -1: #動き先の値がまだ決まっていないとき
            distance[abc[0]] = distance[abc[1]] + abc[2]
            for j in graph[abc[0]]:
                q_append(j)
# print (distance)

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

# Optimized solution
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 9)
MOD = 10 ** 9 + 7

N = int(eval(input()))

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

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

d = [-1] * (N + 1)
d[K] = 0
stack = [K]
while stack:
    now = stack.pop()
    for tmp in G[now]:
        next_ = tmp[0]
        if d[next_] == -1:
            d[next_] = d[now] + tmp[1]
            stack.append(next_)

for _ in range(Q):
    x, y = list(map(int, input().split()))
    print((d[x] + d[y]))
# Original solution
import sys
from collections import deque
inf=100000000000000000000
fastinput=sys.stdin.readline
N=int(fastinput())
path=[[0]*N for hoge in range(N)]
for fuga in range(N-1):
    a,b,c=[int(hoge) for hoge in fastinput().split()]
    path[a-1][b-1]=c
    path[b-1][a-1]=c
Q,K=[int(hoge) for hoge in fastinput().split()]
stack=deque()
stack.append(K-1)
Kcost=[0]*N#あとあと初期化
went=[False]*N
went[K-1]=True
while stack:
    cur=stack.pop()
    curcost=Kcost[cur]
    for next,c in enumerate(path[cur]):
        if c and not went[next]:
            went[next]=True
            Kcost[next]=curcost+c
            stack.append(next)
for q in range(Q):
    x,y=[int(hoge) for hoge in fastinput().split()]
    print((Kcost[x-1]+Kcost[y-1]))
    

# Optimized solution
import sys
from collections import deque
inf=100000000000000000000
fastinput=sys.stdin.readline
N=int(fastinput())
path=[[] for hoge in range(N)]
for fuga in range(N-1):
    a,b,c=[int(hoge) for hoge in fastinput().split()]
    path[a-1].append([b-1,c])
    path[b-1].append([a-1,c])
Q,K=[int(hoge) for hoge in fastinput().split()]
stack=deque()
stack.append(K-1)
Kcost=[0]*N#あとあと初期化
went=[False]*N
went[K-1]=True
while stack:
    cur=stack.pop()
    curcost=Kcost[cur]
    for next,c in path[cur]:
        if c and not went[next]:
            went[next]=True
            Kcost[next]=curcost+c
            stack.append(next)
for q in range(Q):
    x,y=[int(hoge) for hoge in fastinput().split()]
    print((Kcost[x-1]+Kcost[y-1]))
# Original solution
import copy
n = int(eval(input()))
d = []
for i in range(n - 1):#h:高さ
    d.append([int(m) for m in input().split()])
q, k = list(map(int, input().split()))
z = []
for i in range(q):#h:高さ
    z.append([int(m) for m in input().split()])
e = []
for i in range(n):
    e.append([])
for i in d:
    e[i[0] - 1].append(i)
    e[i[1] - 1].append(i)
f = {}
for i in range(1, n + 1):
    if i != k:
        f[i] = 0
ar = []
pre = {}

for i in e[k - 1]:
    if k == i[0]:
        ar.append(i[1])
        f[i[1]] = i[2]
        pre[i[1]] = k
    else:
        ar.append(i[0])
        f[i[0]] = i[2]
        pre[i[0]] = k
while min(f.values()) == 0:
    ar2 = copy.deepcopy(ar)
    ar = []
    for j in ar2:
        for m in e[j - 1]:
                if j == m[0]:
                    if m[1] != pre[j]:
                        ar.append(m[1])
                        f[m[1]] = m[2] + f[j]
                        pre[m[1]] = j
                else:
                    if m[0] != pre[j]:
                        ar.append(m[0])
                        f[m[0]] = m[2] + f[j]
                        pre[m[0]] = j
for i in range(q):#h:高さ
    print((f[z[i][0]] + f[z[i][1]]))
"""  
adj = []
for i in range(n):
    adj.append([])
for i in range(n):
    for j in range(n):
        adj[i].append(0)
#鱗屑行列
for i in range(q):
    adj[d[i][0] - 1][d[i][1] - 1] = d[i][2]
    adj[d[i][1] - 1][d[i][0] - 1] = d[i][2]"""

# Optimized solution
n = int(eval(input()))
d = []
for i in range(n - 1):#h:高さ
    d.append([int(m) for m in input().split()])
q, k = list(map(int, input().split()))
z = []
for i in range(q):#h:高さ
    z.append([int(m) for m in input().split()])

for i in range(n - 1):
    d[i][0] -= 1
    d[i][1] -= 1
for i in range(q):
    z[i][0] -= 1
    z[i][1] -= 1
def BFS(k, edges, N):
  roots=[ [] for i in range(N)]
  for a,b,c in edges:
    roots[a]+=[(b,c)]
    roots[b]+=[(a,c)]
  dist=[-1]*N
  stack=[]
  stack.append(k)
  dist[k]=0
  while stack:
    label=stack.pop(-1)
    for i,c in roots[label]:
      if dist[i]==-1:
        dist[i]=dist[label]+c
        stack+=[i]
  return dist

root = BFS(k - 1, d, n)

for i in range(q):
    print((root[z[i][0]] + root[z[i][1]]))
# Original solution
import collections

def bfs(tree, s, distance):
    queue = [s]
    distance[s] = 0

    while queue:
        v = queue.pop(0)
        v_c = distance[v]
        for node, cost in tree[v]:
            if distance[node] == -1:
                distance[node] = v_c + cost
                queue.append(node)

N = int(eval(input()))

Tree = collections.defaultdict(list)

for i in range(N - 1):
    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()))

distance = collections.defaultdict(lambda : -1)
bfs(Tree, k, distance)

for i in range(q):
    x, y = list(map(int, input().split()))
    print((distance[x] + distance[y]))


# Optimized solution
import collections

def bfs(tree, s, distance):
    queue = collections.deque([s])
    distance[s] = 0

    while queue:
        v = queue.popleft()
        v_c = distance[v]
        for node, cost in tree[v]:
            if distance[node] == -1:
                distance[node] = v_c + cost
                queue.append(node)

N = int(eval(input()))

Tree = collections.defaultdict(list)

for i in range(N - 1):
    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()))

distance = collections.defaultdict(lambda : -1)
bfs(Tree, k, distance)

for i in range(q):
    x, y = list(map(int, input().split()))
    print((distance[x] + distance[y]))
# Original solution
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]))



# Optimized solution
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]))
# Original solution
from heapq import heappush, heappop

INF = 10**15

def dijkstra(s, edges):
    n = len(edges)
    que = []
    d = [INF] * n
    prev = [-1] * n
    d[s] = 0
    heappush(que, (0, s))

    while len(que) > 0:
        (c, v) = heappop(que)
        if d[v] < c:
            continue
        for (cc, w) in edges[v]:
            if d[w] > d[v] + cc:
                d[w] = d[v] + cc
                prev[w] = v
                heappush(que, (d[w], w))

    return d, prev

n = int(eval(input()))
edge = [[] for i in range(n)]
for i in range(n-1):
    ai, bi, ci = list(map(int, input().split()))
    ai, bi = ai-1, bi-1
    edge[ai].append((ci, bi))
    edge[bi].append((ci, ai))

q, k = list(map(int, input().split()))
x, y = [], []
for i in range(q):
    xi, yi = list(map(int, input().split()))
    xi, yi = xi-1, yi-1
    x.append(xi)
    y.append(yi)

dist, prev = dijkstra(k-1, edge)

for i in range(q):
    print((dist[x[i]]+dist[y[i]]))

# Optimized solution
from heapq import heappush, heappop
INF = 10**15
def dijkstra(s, edges):
    n = len(edges)
    que = []
    d = [INF] * n
    prev = [-1] * n
    d[s] = 0
    heappush(que, (0, s))
    while len(que) > 0:
        (c, v) = heappop(que)
        if d[v] < c:
            continue
        for (cc, w) in edges[v]:
            if d[w] > d[v] + cc:
                d[w] = d[v] + cc
                prev[w] = v
                heappush(que, (d[w], w))
    return d, prev

n = int(eval(input()))
edges = [[] for i in range(n)]
for i in range(n-1):
    ai, bi, ci = list(map(int, input().split()))
    ai, bi = ai-1, bi-1
    edges[ai].append((ci, bi))
    edges[bi].append((ci, ai))

q, k = list(map(int, input().split()))
k -= 1
d, prev = dijkstra(k, edges)
ans = []
for i in range(q):
    xi, yi = list(map(int, input().split()))
    xi, yi = xi-1, yi-1
    ans.append(d[xi]+d[yi])

for i in ans:
    print(i)