CodeEff/ECCO huggingface dataset:
# Original solution
import heapq

n = int(eval(input()))

g = [[] for _ in range(n)]
for i 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))

ans = []
q, k = list(map(int, input().split()))
for i in range(q):
    x, y = list(map(int, input().split()))
    
    res = 0
    # x -> k
    dis = [float("inf")] * n
    dis[x-1] = 0
    pq = [(0, x-1)]
    while pq:
        d, node = heapq.heappop(pq)
        if d > dis[node]: continue

        for nxt, cost in g[node]:
            if d + cost < dis[nxt]:
                dis[nxt] = d + cost
                heapq.heappush(pq, (dis[nxt], nxt))
    res += dis[k-1]
    # k -> y
    dis = [float("inf")] * n
    dis[k-1] = 0
    pq = [(0, k-1)]
    while pq:
        d, node = heapq.heappop(pq)
        if d > dis[node]: continue

        for nxt, cost in g[node]:
            if d + cost < dis[nxt]:
                dis[nxt] = d + cost
                heapq.heappush(pq, (dis[nxt], nxt))
    res += dis[y-1]
    ans.append(res)

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

# Optimized solution
import heapq

n = int(eval(input()))

g = [[] for _ in range(n)]
for i 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()))

dis = [float("inf")] * n
dis[k-1] = 0
pq = [(0, k-1)]
while pq:
    d, node = heapq.heappop(pq)
    if d > dis[node]: continue

    for nxt, cost in g[node]:
        if d + cost < dis[nxt]:
            dis[nxt] = d + cost
            heapq.heappush(pq, (dis[nxt], nxt))

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

    ans.append(dis[x-1]+dis[y-1])

print(("\n".join(map(str, ans))))
# Original solution
import queue

from sys import stdin
input = stdin.readline

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

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

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

d = [float("inf") for _ in range(N)]

def dijkstra(s):
    que = queue.PriorityQueue()
    d[s] = 0
    que.put([0, s])

    while not que.empty():
        p = que.get()
        v = p[1]
        if d[v] < p[0]:
            continue
        for i, j in tree[v]:
            if d[i] > d[v] + j:
                d[i] = d[v] + j
                que.put([d[i], i])

dijkstra(K)

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


# Optimized solution
#!/usr/bin/env python2
# -*- coding: utf-8 -*-

import sys

sys.setrecursionlimit(200000)

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

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

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

depth = [0 for _ in range(N)]

def dfs(v, p = -1, d = 0):
    depth[v] = d
    for i, j in tree[v]:
        if i == p:
            continue
        dfs(i, v, d + j)

dfs(K)

for i in range(Q):
    x, y = list(map(int, input().split()))
    x -= 1; y -= 1
    print(depth[x] + depth[y])
# Original solution
#!/usr/bin/env python2
# -*- coding: utf-8 -*-

import queue

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

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

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

d = [float("inf") for _ in range(N)]

def dijkstra(s):
    que = queue.PriorityQueue()
    d[s] = 0
    que.put([0, s])

    while not que.empty():
        p = que.get()
        v = p[1]
        if d[v] < p[0]:
            continue
        for i, j in tree[v]:
            if d[i] > d[v] + j:
                d[i] = d[v] + j
                que.put([d[i], i])

dijkstra(K)

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

# Optimized solution
#!/usr/bin/env python2
# -*- coding: utf-8 -*-

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

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

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

depth = [0 for _ in range(N)]

def dfs(v, p = -1, d = 0):
    St = []
    St.append([v, p, d])
    # 空のシーケンスは偽になる
    while St:
        cur = St.pop()
        depth[cur[0]] = cur[2]
        for i, j in tree[cur[0]]:
            if i == cur[1]:
                continue
            St.append([i, cur[0], cur[2] + j])

dfs(K)

for i in range(Q):
    x, y = list(map(int, input().split()))
    x -= 1; y -= 1
    print(depth[x] + depth[y])
# Original solution
import sys
input = sys.stdin.readline
sys.setrecursionlimit(1000000)
from collections import deque
def getN():
    return int(eval(input()))
def getList():
    return list(map(int, input().split()))
import math

n = getN()

vtxs = [[] for i in range(n)]
dist = [0 for i in range(n)]
visit = [0 for i in range(n)]

for i in range(n-1):
    a,b,c = getList()
    vtxs[a-1].append((b-1 ,c))
    vtxs[b-1].append((a-1 ,c))

ques, k = getList()

from collections import deque
q = deque([k-1])
visit[k-1] = 1
while(q):
    cur = q.popleft()
    cdist = dist[cur]
    for nx in vtxs[cur]:
        nxa, nxc = nx
        if visit[nxa] == 0:
            dist[nxa] = cdist + nxc
            visit[nxa] = 1
            q.append(nxa)

for i in range(ques):
    x, y = getList()
    print((dist[x-1] + dist[y-1]))

# Optimized solution
import sys
# from collections import defaultdict, deque
import math
# import copy
# from bisect import bisect_left, bisect_right
import heapq

# sys.setrecursionlimit(1000000)

# input aliases
input = sys.stdin.readline

getS = lambda: input().strip()
getN = lambda: int(eval(input()))
getList = lambda: list(map(int, input().split()))
getZList = lambda: [int(x) - 1 for x in input().split()]

INF = 10 ** 20
MOD = 1000000007


def dijkstra(graph, start, n):
    h = []
    heapq.heappush(h, (0, start))
    costs = [INF for _ in range(n)]
    while h:
        cost, cur = heapq.heappop(h)
        if costs[cur] != INF:
            continue
        costs[cur] = cost
        for edge in graph[cur]:
            ecost, tgt = edge
            if costs[tgt] == INF:
                heapq.heappush(h, (cost + ecost, tgt))

    return costs

def solve():
    n = getN()
    graph = [[] for _ in range(n)]
    for i in range(n-1):
        a,b,c = getList()
        a,b = a-1, b-1

        graph[a].append([c, b])
        graph[b].append([c, a])
    q, k = getList()
    costs = dijkstra(graph, k-1, n)
    # print(costs)
    for i in range(q):
        s, e = getZList()

        print((costs[s] + costs[e]))

    return

def main():
    n = getN()
    for _ in range(n):
        solve()
if __name__ == "__main__":
    # main()
    solve()
# Original solution
import heapq

V = int(eval(input()))
E = V - 1
G = {}
for i in range(V):
    G[i] = {}

for _ in range(E):
    s, t, c = [int(_) for _ in input().split()]
    s -= 1  # 0-indexed
    t -= 1  # 0-indexed
    G[s][t] = c
    G[t][s] = c
Q, K = [int(_) for _ in input().split()]
K -= 1  # 0-indexed
XY = [[int(_) for _ in input().split()] for _ in range(Q)]

INF = float('inf')

que = []
i = K
d = 0
D = [INF] * V
D[i] = 0
used = [False] * V
used[i] = True

while True:
    for j in list(G[i].keys()):
        heapq.heappush(que, (d + G[i][j], j))
    v = -1
    while que:
        d, j = heapq.heappop(que)
        if not used[j]:
            v = j
            break
    if v == -1:
        break
    else:
        used[v] = True
        D[v] = d
        i = v
for x, y in XY:
    x -= 1  # 0-indexed
    y -= 1  # 0-indexed
    print((D[x] + D[y]))

# Optimized solution
V = int(eval(input()))
E = V - 1
G = {}
for i in range(V):
    G[i] = {}

for _ in range(E):
    s, t, c = [int(_) for _ in input().split()]
    s -= 1  # 0-indexed
    t -= 1  # 0-indexed
    G[s][t] = c
    G[t][s] = c
Q, K = [int(_) for _ in input().split()]
K -= 1  # 0-indexed
XY = [[int(_) for _ in input().split()] for _ in range(Q)]

INF = float('inf')
D = [INF] * V
D[K] = 0
cand = []
for j in list(G[K].keys()):
    cand += [(K, j)]
while True:
    if cand:
        x, y = cand.pop()
        if D[y] == INF:
            D[y] = D[x] + G[x][y]
            for z in list(G[y].keys()):
                cand += [(y, z)]
    else:
        break
for x, y in XY:
    x -= 1
    y -= 1
    print((D[x] + D[y]))
# Original solution
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    #from collections import defaultdict
    from itertools import combinations, permutations, accumulate, groupby
    #from itertools import product
    from bisect import bisect_left,bisect_right
    from heapq import heapify, heappop, heappush
    from math import floor, ceil
    #from operator import itemgetter

    #inf = 10**17
    #mod = 10**9 + 7

    N = int(eval(input()))
    
    def dfs(v):
        visited[v] = 1
        for cost, nv in edge[v]:
            if visited[nv] == 1:
                continue
            d[nv] = d[v] + cost
            dfs(nv)

    #N:頂点数   W:辺数
    edge = [[] for i in range(N)]
    #edge[i] : iから出る道の[重み,行先]の配列
    for _ in range(N-1):
        x,y,z = list(map(int,input().split()))
        edge[x-1].append((z,y-1))
        edge[y-1].append((z,x-1))

    q,k = list(map(int, input().split()))
    d = [-1]*N
    d[k-1] = 0
    visited = [0]*N
    dfs(k-1)

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

if __name__ == '__main__':
    main()

# Optimized solution
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    #from collections import defaultdict
    from itertools import combinations, permutations, accumulate, groupby
    #from itertools import product
    from bisect import bisect_left,bisect_right
    from heapq import heapify, heappop, heappush
    from math import floor, ceil
    #from operator import itemgetter

    #inf = 10**17
    #mod = 10**9 + 7

    N = int(eval(input()))
    inf = 10**17

    def dijkstra_heap(start,edge):
        #始点から各頂点への最短距離(頂点番号:0~N-1)
        d = [inf]*N
        used = [False]*N
        d[start] = 0
        used[start] = True
        edgelist = []
        #a:重み(//), b:次の頂点(%)
        for a,b in edge[start]:
            heappush(edgelist,a*(10**6)+b)

        while len(edgelist):
            #まだ最短距離が決まっていない頂点の中から最小の距離のものを探す
            minedge = heappop(edgelist)
            if used[minedge%(10**6)]:
                continue
            node = minedge%(10**6)
            d[node] = minedge//(10**6)
            used[node] = True

            for e in edge[node]:
                if not used[e[1]]:
                    heappush(edgelist,(e[0]+d[node])*(10**6)+e[1])
        return d

    #N:頂点数   W:辺数
    edge = [[] for i in range(N)]
    #edge[i] : iから出る道の[重み,行先]の配列
    for _ in range(N-1):
        x,y,z = list(map(int,input().split()))
        edge[x-1].append((z,y-1))
        edge[y-1].append((z,x-1))

    q,k = list(map(int, input().split()))
    d = dijkstra_heap(k-1, edge)

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

if __name__ == '__main__':
    main()
# Original solution
import sys
sys.setrecursionlimit(500000)

N = int(eval(input()))

tree = [[] for _ in range(N+1)]
for _ 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()))

from functools import lru_cache
@lru_cache(maxsize=None)
def dfs(start, end, dist, f=None):
    if start == end:
        return 0
    for n, d in tree[start]:
        if n == f:
            continue
        if n == end:
            return dist + d
        else:
            result = dfs(n, end, dist+d, start)
            if result is not None:
                return result

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

for i in range(Q):
    x, y = xy[i]
    print((dfs(x, K, 0) + dfs(K, y, 0)))


# Optimized solution
import sys
sys.setrecursionlimit(500000)

N = int(eval(input()))

tree = [[] for _ in range(N+1)]
for _ 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()))

def dfs(start, dist, dists, f=None):
    for n, d in tree[start]:
        if n == f:
            continue
        dists[n] = dist + d
        dfs(n, dist+d, dists, start)

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

dists = [0] * (N+1)
dfs(K, 0, dists)

for i in range(Q):
    x, y = xy[i]
    print((dists[x] + dists[y]))
# Original solution
import sys
sys.setrecursionlimit(500000)

N = int(eval(input()))

tree = [[] for _ in range(N+1)]
for _ 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()))

def dfs(start, dist, dists, f=None):
    for n, d in tree[start]:
        if n == f:
            continue
        dists[n] = dist + d
        dfs(n, dist+d, dists, start)

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

dists = [0] * (N+1)
dfs(K, 0, dists)

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


# Optimized solution
import sys
sys.setrecursionlimit(500000)

import sys
def input():
    return sys.stdin.readline()[:-1]

N = int(eval(input()))

tree = [[] for _ in range(N+1)]
for _ 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()))

def dfs(start, dist, dists, f=None):
    for n, d in tree[start]:
        if n == f:
            continue
        dists[n] = dist + d
        dfs(n, dist+d, dists, start)

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

dists = [0] * (N+1)
dfs(K, 0, dists)

for i in range(Q):
    x, y = xy[i]
    print((dists[x] + dists[y]))
# Original solution
from collections import deque

n = int(eval(input()))
gr = { i:[] for i in range(1, n + 1)}
for _ in range(n - 1):
    a, b, c = list(map(int, input().split()))
    gr[a].append((b, c))
    gr[b].append((a, c))
q, k = list(map(int, input().split()))
dmax = 10**14 + 1
dist = [dmax for _ in range(n + 1)]
dist[k] = 0
ls = deque([k])
while 0 < len(ls):
    u = ls[0]
    for v, c in gr[u]:
        if dist[v] == dmax:
            dist[v] = dist[u] + c
            ls.append(v)
    ls.popleft()

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

# Optimized solution
from collections import deque

n = int(eval(input()))
gr = { i:[] for i in range(1, n + 1)}
for _ in range(n - 1):
    a, b, c = list(map(int, input().split()))
    gr[a].append((b, c))
    gr[b].append((a, c))
q, k = list(map(int, input().split()))
dmax = 10**14 + 1
dist = [dmax for _ in range(n + 1)]
dist[k] = 0
ls = deque([k])
while 0 < len(ls):
    u = ls.popleft()
    for v, c in gr[u]:
        if dist[v] == dmax:
            dist[v] = dist[u] + c
            ls.append(v)

for _ in range(q):
    x, y = list(map(int, input().split()))
    print((dist[x] + dist[y]))
# Original solution
n=int(eval(input()))
G=[[] for i in range(n)]
for i 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()))
k=k-1
stack=[k]
dis=[-1]*n
dis[k]=0
from collections import deque
stack=deque(stack)
while len(stack)>0:
    now=stack.pop()
    d=dis[now]
    for i in G[now]:
        if dis[i[0]]==-1:
            dis[i[0]]=d+i[1]
            stack.append(i[0])
for i in range(q):
    x,y=list(map(int,input().split()))
    x=x-1
    y=y-1
    print((dis[x]+dis[y]))



# Optimized solution
n=int(eval(input()))
G=[[] for i in range(n)]
for i 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()))
k=k-1
stack=[k]
dis=[-1]*n
dis[k]=0
while stack!=[]:
    now=stack.pop()
    d=dis[now]
    for i in G[now]:
        if dis[i[0]]==-1:
            dis[i[0]]=d+i[1]
            stack.append(i[0])
for i in range(q):
    x,y=list(map(int,input().split()))
    x=x-1
    y=y-1
    print((dis[x]+dis[y]))
# Original solution
from collections import deque

N = int(input())

T = [[] for i in range(N)]
for i in range(N-1) :
    u,v,c = map(int,input().split())
    u -= 1
    v -= 1
    T[u].append([v,c])
    T[v].append([u,c])

Q,K = map(int,input().split())
K -= 1

L = []

for i in range(Q) :
    u,v = map(int,input().split())
    u -= 1
    v -= 1
    L.append([u,v])

dist = [0]*N

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

explored = [0]*N

C = []
while q :
    now,c = q.pop()
    
    if not explored[now] :
        C.append(c)
        dist[now] = sum(C)
        explored[now] = 1
    
    flg = 1
    
    while T[now] :
        to, c = T[now].pop(0)
        if explored[to] :
            continue
        
        q.append([now, 0])
        q.append([to, c])
        flg = 0
        break
    if flg :
        if len(T[now]) == 0 :
            C.pop(-1)
    
    #print("now:", now, "C:", C)
        
ans = []

for i in range(Q) :
    u,v = L[i]
    calc = dist[u] + dist[v]
    ans.append(calc)

print(*ans, sep="\n")


# Optimized solution
import sys
sys.setrecursionlimit(10**9)

N = int(input())
T = [[] for i in range(N+1)]

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

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

dist = [None]*(N+1)

def dfs(now,d) :
    dist[now] = d
    for to,c in T[now] :
        if dist[to] is not None :
            continue
        dfs(to,d+c)

dfs(K,0)

ans = []

for i in range(Q) :
    u,v = map(int,input().split())
    ans.append(dist[u]+dist[v])

print(*ans,sep="\n")
# Original solution
### bfs ###
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)
N = int(eval(input()))
G = [[] for _ in range(N)]
dis = [-1]*N
que = []
for _ in range(N-1):
    a, b, c = list(map(int, input( ).split( )))
    a -= 1
    b -= 1
    G[a].append([b,c])
    G[b].append([a,c])
Q, K = list(map(int, input().split()))
K -= 1
dis[K] = 0
que.append(K) #dfs
while que:
    p = que.pop(0)
    for i in range(len(G[p])):
        n_p, d = G[p][i]
        if dis[n_p] != -1:
            continue
        que.append(n_p)
        dis[n_p] = dis[p] + d

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

# Optimized solution
### dfs ###
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)
N = int(eval(input()))
G = [[] for _ in range(N)]
dis = [-1]*N
for _ in range(N-1):
    a, b, c = list(map(int, input( ).split( )))
    a -= 1
    b -= 1
    G[a].append([b,c])
    G[b].append([a,c])
Q, K = list(map(int, input().split()))
K -= 1
dis[K] = 0

def dfs(pos):
    for n_pos, n_cost in G[pos]:
        if dis[n_pos] != -1:
            continue
        dis[n_pos] = dis[pos]+n_cost
        dfs(n_pos)
dfs(K)

for i in range(Q):
    x, y = list(map(int, input().split()))
    x -= 1
    y -= 1
    ans = dis[x]+dis[y]
    print(ans)
# Original solution
import sys
from collections import defaultdict
from heapq import heappush, heappop


def input():
    return sys.stdin.readline().strip()


def dijkstra(adj_list, start):
    n = len(adj_list)
    dist = [float("inf")] * n
    dist[start] = 0

    pq = []
    heappush(pq, (0, start))
    visited = set()
    while pq:
        w, v = heappop(pq)
        if dist[v] < w:
            continue
        visited.add(v)
        for nv, nw in adj_list[v]:
            if nv in visited:
                continue
            if dist[nv] > dist[v] + nw:
                dist[nv] = dist[v] + nw
                heappush(pq, (dist[nv], nv))
    return dist


N = int(eval(input()))
g = defaultdict(list)
for _ in range(N - 1):
    a, b, c = list(map(int, input().split()))
    a -= 1
    b -= 1
    g[a].append((b, c))
    g[b].append((a, c))
Q, K = list(map(int, input().split()))
K -= 1
d = dijkstra(g, K)
ans = []
for _ in range(Q):
    x, y = list(map(int, input().split()))
    x -= 1
    y -= 1
    print((d[x] + d[y]))


# Optimized solution
import sys
from collections import deque


def input():
    return sys.stdin.readline().strip()


def bfs(start):
    q = deque([start])
    dist = [-1] * (N + 1)
    dist[start] = 0
    while q:
        v = q.popleft()
        for nv, nw in g[v]:
            if dist[nv] > 0:
                continue
            dist[nv] = dist[v] + nw
            q.append(nv)
    return dist


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 = bfs(K)
ans = []
for _ in range(Q):
    x, y = list(map(int, input().split()))
    print((d[x] + d[y]))
# Original solution
from collections import defaultdict
from collections import deque
n = int(eval(input()))
tree = 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])

def distance(x, y):
    seen = set()
    d = 0
    stack = deque([[tree[x], d]])
    flag = True
    while flag:
        connect, d = stack.popleft()
        for i in connect:
            if i[0] not in seen:
                seen.add(i[0])
                d += i[1]
                if i[0] == y:
                    flag = False
                    break
                stack.append([tree[i[0]], d])
                d -= i[1]
    return d

q, k = list(map(int, input().split()))
dis_l = {}
for i in range(n):
    dis_l[i+1] = distance(i + 1, k)
for i in range(q):
    x, y = list(map(int, input().split()))
    print((dis_l[x] + dis_l[y]))

# Optimized solution
from collections import defaultdict
from collections import deque
n = int(eval(input()))
tree = 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])

def distance(k):
    seen = set()
    dis_l = {}
    d = 0
    stack = deque([[tree[k], d]])
    flag = True
    while stack:
        connect, d = stack.popleft()
        for i in connect:
            if i[0] not in seen:
                seen.add(i[0])
                d += i[1]
                dis_l[i[0]] = d
                stack.append([tree[i[0]], d])
                d -= i[1]
    return dis_l

q, k = list(map(int, input().split()))
dis_l = distance(k)
for i in range(q):
    x, y = list(map(int, input().split()))
    print((dis_l[x] + dis_l[y]))
# Original solution
n = int(eval(input()))
cs = [[0] * (n + 1) for _ in range(n + 1)]
es = [[] for _ in range(n + 1)]
for _ in range(n - 1):
    a, b, c = list(map(int, input().split()))
    cs[a][b] = cs[b][a] = c
    es[a].append(b)
    es[b].append(a)
ds = [0] * (n + 1)
qs = []


def bfs():
    cur, prev = qs.pop()
    ds[cur] = ds[prev] + cs[prev][cur]
    for e in es[cur]:
        if e != prev:
            qs.append((e, cur))


q, k = list(map(int, input().split()))
qs.append((k, k))
while qs:
    bfs()

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

# Optimized solution
n = int(eval(input()))
cs = [[0] * (n + 1) for _ in range(n + 1)]
es = [[] for _ in range(n + 1)]
for _ in range(n - 1):
    a, b, c = list(map(int, input().split()))
    cs[a][b] = cs[b][a] = c
    es[a].append(b)
    es[b].append(a)
ds = [0] * (n + 1)


def dfs(cur, prev):
    ds[cur] = ds[prev] + cs[prev][cur]
    for e in es[cur]:
        if e != prev:
            dfs(e, cur)


q, k = list(map(int, input().split()))
dfs(k, k)

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