CodeEff/ECCO huggingface dataset:
# Original solution
import heapq
N = int(eval(input()))
G = [[] for i in range(N)]
for i in range(N - 1):
    a, b, c = list(map(int, input().split()))
    a, b = a - 1, b - 1
    G[a].append([b, c])
    G[b].append([a, c])

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


def dijkstra(x):
    d = [float('inf')] * N
    d[x] = 0
    visited = {x}

    # d, u
    hq = [(0, x)]

    while hq:
        u = heapq.heappop(hq)[1]
        visited.add(u)

        for node, cost in G[u]:
            if (node not in visited) and d[node] > d[u] + cost:
                d[node] = d[u] + cost
                heapq.heappush(hq, (d[u]+cost, node))
    return d


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


# Optimized solution
import sys
sys.setrecursionlimit(10 ** 9)
N = int(eval(input()))
G = [[] for i in range(N)]
for i in range(N - 1):
    a, b, c = list(map(int, input().split()))
    a, b = a - 1, b - 1
    G[a].append([b, c])
    G[b].append([a, c])

Q, K = list(map(int, input().split()))
K -= 1
dist = [-1] * N
dist[K] = 0


def dfs(n):
    for v, c in G[n]:
        if dist[v] < 0:
            dist[v] = dist[n] + c
            dfs(v)


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

# 頂点 s からの最短距離を求める (存在しないときはinf)
# n: 頂点数, edge[i]: 頂点 i からの [cost, dst] の list (heapq のため cost が先)
# O(|E|log|V|)
import heapq
def dijkstra(s,n,edge):
    d = [float("inf")] * n # s からの距離
    d[s] = 0
    q = [] # 頂点への距離を管理する。ヒープにする
    for e in edge[s]:
        heapq.heappush(q, e)
    
    while q:
        minedge = heapq.heappop(q)
        v = minedge[1]
        if d[v] < minedge[0]:
            continue # 最小のもの以降はスルー
        d[v] = minedge[0]
        for e in edge[v]:
            heapq.heappush(q, [e[0]+d[v], e[1]])

    return d

def main():
    N = int(eval(input()))
    edge = [[] for i in range(N)]
    # edge[i] : 頂点 i から出る経路の [重み,行先] の配列
    for _ in range(N-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()))
    D = dijkstra(K-1, N, edge)

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

if __name__ == '__main__':
    main()


# Optimized solution
import sys
input=sys.stdin.readline
sys.setrecursionlimit(3*10**5) # 大丈夫か？

N = int(eval(input()))
edge = [[] for i in range(N)]
# edge[i] : 頂点 i から出る経路の [距離,行先] の隣接リスト
for _ in range(N-1):
    a,b,c = list(map(int, input().split()))
    edge[a-1].append([c,b-1])
    edge[b-1].append([c,a-1])

D = [-1]*N # K からの距離. 未訪問の場合 -1.
# 今回の問題の場合、木なので経路は 1 通りしか無く、
# K から頂点を順に辿りながらコストを足していくだけで良い
def dfs(src, cost):
    D[src] = cost
    for e in edge[src]:
        if D[e[1]] == -1:
            dfs(e[1], cost + e[0])

def main():
    Q,K = list(map(int, input().split()))
    dfs(K-1, 0)

    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

def I(): return int(sys.stdin.readline().rstrip())
def S(): return sys.stdin.readline().rstrip()

N = I()
G = {}
for i in range(1,N+1):
    G[i] = []
for i in range(N-1):
    a,b,c = list(map(int,S().split()))
    G[a].append((b,c))
    G[b].append((a,c))

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

from collections import deque

distance = [-1]*(N+1)  # arrive[i] = 頂点Kからの最短距離
distance[K] = 0

q = deque([K])
while q:
    a = q.pop()
    for b,c in G[a]:
        if distance[b] != -1:
            continue
        distance[b] = distance[a] + c
        q.appendleft(b)

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

# Optimized solution
import sys

def I(): return int(sys.stdin.readline().rstrip())
def S(): return sys.stdin.readline().rstrip()

N = I()
G = [[] for i in range(N+1)]

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

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

from collections import deque

distance = [-1]*(N+1)  # arrive[i] = 頂点Kからの最短距離
distance[K] = 0

q = deque([K])
while q:
    a = q.pop()
    for b,c in G[a]:
        if distance[b] != -1:
            continue
        distance[b] = distance[a] + c
        q.appendleft(b)

for i in range(Q):
    x,y = list(map(int,S().split()))
    print((distance[x] + distance[y]))
# Original solution
import sys

def I(): return int(sys.stdin.readline().rstrip())
def S(): return sys.stdin.readline().rstrip()

N = I()
G = [[] for i in range(N+1)]

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

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

from collections import deque

distance = [-1]*(N+1)  # arrive[i] = 頂点Kからの最短距離
distance[K] = 0

q = deque([K])
while q:
    a = q.pop()
    for b,c in G[a]:
        if distance[b] != -1:
            continue
        distance[b] = distance[a] + c
        q.appendleft(b)

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

# Optimized solution
def main():
    import sys

    def I():
        return int(sys.stdin.readline().rstrip())

    def S():
        return sys.stdin.readline().rstrip()

    N = I()
    G = [[] for i in range(N + 1)]

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

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

    from collections import deque

    distance = [-1] * (N + 1)  # arrive[i] = 頂点Kからの最短距離
    distance[K] = 0

    q = deque([K])
    while q:
        a = q.pop()
        for b, c in G[a]:
            if distance[b] != -1:
                continue
            distance[b] = distance[a] + c
            q.appendleft(b)

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

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

stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline()

n = ni()
g = []
for i in range(n+1): g.append([])

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

q,k = na()

def dfs(cur, pre, g, par, dw, w):
    dw[cur] = w
    par[cur] = pre
    for e in g[cur]:
        if e[0] != pre:
            dfs(e[0], cur, g, par, dw, w + e[1])

par = [-1] * (n+2)
dw = [-1] * (n+2)
from queue import Queue
qs = Queue()
qs.put(k)
dw[k] = 0

while not qs.empty():
    cur = qs.get()
    for e in g[cur]:
        if e[0] != par[cur]:
            par[e[0]] = cur
            dw[e[0]] = dw[cur] + e[1]
            qs.put(e[0])


# dfs(k, -1, g, par, dw, 0)

for z in range(q):
    x, y = na()
    print((dw[x] + dw[y]))



# Optimized solution
import sys
sys.setrecursionlimit(1000001)
stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline()

n = ni()
g = []
for i in range(n+1): g.append([])

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

q,k = na()

def dfs(cur, pre, g, par, dw, w):
    dw[cur] = w
    par[cur] = pre
    for e in g[cur]:
        if e[0] != pre:
            dfs(e[0], cur, g, par, dw, w + e[1])

par = [-1] * (n+2)
dw = [-1] * (n+2)
dfs(k, -1, g, par, dw, 0)

for z in range(q):
    x, y = na()
    print((dw[x] + dw[y]))
# Original solution
from collections import deque
n=int(eval(input()))
l=[0]*n
d={}
for i in range(n-1):
    a,b,c=list(map(int,input().split()))
    a-=1
    b-=1
    if a in d:
        d[a].add((b,c))
    else:
        d[a]={(b,c)}
    if b in d:
        d[b].add((a,c))
    else:
        d[b]={(a,c)}
q,k=list(map(int,input().split()))
k-=1
p=deque([k])
while len(p):
    w=p.popleft()
    for i in d[w]:
        if (l[i[0]]==0 and i[0]!=k) or (l[i[0]]!=0 and l[i[0]]>l[w]+i[1]):
            l[i[0]]=l[w]+i[1]
            p.append(i[0])

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


# Optimized solution
from collections import deque

def main():
    n=int(eval(input()))
    l=[0]*n
    d={}
    for i in range(n-1):
        a,b,c=list(map(int,input().split()))
        a-=1
        b-=1
        if a in d:
            d[a].add((b,c))
        else:
            d[a]={(b,c)}
        if b in d:
            d[b].add((a,c))
        else:
            d[b]={(a,c)}
    q,k=list(map(int,input().split()))
    k-=1
    p=deque([k])
    while len(p):
        w=p.popleft()
        for i in d[w]:
            if (l[i[0]]==0 and i[0]!=k) or (l[i[0]]!=0 and l[i[0]]>l[w]+i[1]):
                l[i[0]]=l[w]+i[1]
                p.append(i[0])

    for i in range(q):
        x,y=list(map(int,input().split()))
        print((l[x-1]+l[y-1]))
if __name__ == '__main__':
    main()
# Original solution
def main():
    from collections import defaultdict
    import sys
    sys.setrecursionlimit(1000000)
    n, *rem = list(map(int, open(0).read().split()))
    t = 3 * (n - 1)
    abc = rem[:t]
    q, k = rem[t], rem[t + 1]
    xy = rem[t + 2:]

    m = [[] for _ in range(n + 1)]
    dist = defaultdict(dict)
    for a, b, c in zip(*[iter(abc)] * 3):
        m[a].append(b)
        m[b].append(a)

        dist[a].update({b: c})
        dist[b].update({a: c})

    vis = [False] * (n + 1)
    cost = [0] * (n + 1)
    vis[k] = True

    def dfs(frm, to):
        if vis[to]:
            return
        vis[to] = True
        cost[to] = cost[frm] + dist[frm][to]
        frm = to
        for to in m[frm]:
            dfs(frm, to)

    frm = k
    for to in m[frm]:
        dfs(frm, to)

    tmp = []
    for x, y in zip(*[iter(xy)] * 2):
        tmp.append(cost[x] + cost[y])

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


if __name__ == '__main__':
    main()


# Optimized solution
def main():
    from collections import defaultdict
    import sys
    sys.setrecursionlimit(1000000)
    n, *rem = list(map(int, open(0).read().split()))
    t = 3 * (n - 1)
    abc = rem[:t]
    q, k = rem[t], rem[t + 1]
    xy = rem[t + 2:]

    dist = defaultdict(dict)
    for a, b, c in zip(*[iter(abc)] * 3):
        dist[a].update({b: c})
        dist[b].update({a: c})

    vis = [False] * (n + 1)
    cost = [0] * (n + 1)
    vis[k] = True

    def dfs(a, b):
        if vis[b]:
            return
        vis[b] = True
        cost[b] = cost[a] + dist[a][b]
        a = b
        for b in list(dist[a].keys()):
            dfs(a, b)

    for b in list(dist[k].keys()):
        dfs(k, b)

    tmp = []
    for x, y in zip(*[iter(xy)] * 2):
        tmp.append(cost[x] + cost[y])

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


if __name__ == '__main__':
    main()
# Original solution
# !入力
# グラフの構築
# クエリの入力
# Kからの各点のの距離をdfs
# クエリの処理
from collections import deque
n=int(eval(input()))
edge=[[]for _ in range(n)]
for i 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=[0]*n
seen=[False]*n
todo=deque([k-1])
while todo:
  par=todo.pop()
  seen[par]=True
  for cld,cost in edge[par]:
    if dist[cld]==0:
      if seen[cld]==False:
        dist[cld]=dist[par]+cost
        seen[cld]=True
        todo.append(cld)

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

# Optimized solution
# !入力
# グラフの構築
# クエリの入力
# Kからの各点のの距離をdfs
# クエリの処理
from collections import deque
n=int(eval(input()))
edge=[[]for _ in range(n)]
for i 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=[0]*n
seen=[False]*n
todo=deque([k-1])
while todo:
  par=todo.popleft()
  seen[par]=True
  for cld,cost in edge[par]:
    if dist[cld]==0:
      if seen[cld]==False:
        dist[cld]=dist[par]+cost
        seen[cld]=True
        todo.append(cld)

for i in range(q):
  x,y=list(map(int,input().split()))
  print((dist[x-1]+dist[y-1]))
# Original solution
import sys
from collections import defaultdict, deque
import bisect
from heapq import *
from math import factorial
sys.setrecursionlimit(200000)
input = sys.stdin.readline

N, = list(map(int, input().split()))
# N = int(input())
# L = [int(v) for v in input().split()]
# L = [[int(v) for v in input().split()] for _ in range(N)]
# L = [int(input()) for _ in range(N)]
# L = [list(input().strip())  for _ in range(N)]
# S = input().strip()

L = [[int(v) for v in input().split()] for _ in range(N - 1)]
Q, K = list(map(int, input().split()))
QL = [[int(v) for v in input().split()] for _ in range(Q)]

m = {}
for a, b, c in L:
    if m.get(a) == None:
        m[a] = {}
    if m.get(b) == None:
        m[b] = {}

    m[a][b] = c
    m[b][a] = c

def dfs(now, a, l, al):
    ans = 10 ** 18
    if now == a:
        return l
    for k, v in list(m[now].items()):
        if k in al:
            continue
        re = dfs(k, a, l + v, al + [k])
        ans = min(ans, re)
    return ans

for i in range(N):
    a = dfs(K, i + 1, 0, [])
    m[K][i + 1] = a

for x, y in QL:
    print((m[K][x] + m[K][y]))

# Optimized solution
import sys
from collections import defaultdict, deque
import bisect
from heapq import *
from math import factorial
sys.setrecursionlimit(200000)
input = sys.stdin.readline

N, = list(map(int, input().split()))
# N = int(input())
# L = [int(v) for v in input().split()]
# L = [[int(v) for v in input().split()] for _ in range(N)]
# L = [int(input()) for _ in range(N)]
# L = [list(input().strip())  for _ in range(N)]
# S = input().strip()

L = [[int(v) for v in input().split()] for _ in range(N - 1)]
Q, K = list(map(int, input().split()))
QL = [[int(v) for v in input().split()] for _ in range(Q)]

m = {}
l = [set() for _ in range(N)]
for a, b, c in L:
    a -= 1
    b -= 1
    if m.get(a) == None:
        m[a] = {}
    if m.get(b) == None:
        m[b] = {}

    m[a][b] = c
    m[b][a] = c

    l[a].add(b)
    l[b].add(a)

ans = [None] * N
ans[K - 1] = 0
def dfs(now, a):
    for v in l[now]:
        p = a + m[now][v]
        if ans[v] == None:
            ans[v] = p
            dfs(v, ans[v])
        elif ans[v] > p:
            ans[v] = p
            dfs(v, ans[v])

dfs(K - 1, 0)
for x, y in QL:
    print((ans[x - 1] + ans[y - 1]))
# Original solution
from collections import defaultdict
from heapq import heappop, heappush
 
class Graph:
 
    def __init__(self):
        self.graph = defaultdict(list)
        self.vertices = set()
        self.edges = []
 
    def add_edge(self, a, b, weight=1):
        self.graph[a].append((b, weight))
        self.vertices |= {a, b}
        self.edges.append((a, b, weight))

        self.graph[b].append((a, weight))
        self.edges.append((b, a, weight))
 
    def get_vertices(self):
        return sorted(list(self.vertices))
 
    def num_of_vertices(self):
        return len(self.vertices)
 
    def get_edges(self):
        return self.edges
 
    def num_of_edges(self):
        return len(self.edges)
 
class Dijkstra:
 
    def __init__(self, graph, start):
        self.g = graph.graph
 
        self.dist = defaultdict(lambda: float("inf"))
        self.dist[start] = 0
 
        self.prev = defaultdict(lambda: None)
 
        self.Q = []
        heappush(self.Q, (0, start))
 
        while self.Q:
            dist_u, u = heappop(self.Q)
 
            if self.dist[u] < dist_u:
                continue
 
            for v, weight in self.g[u]:
                alt = dist_u + weight
                if self.dist[v] > alt:
                    self.dist[v] = alt
                    self.prev[v] = u
                    heappush(self.Q, (alt, v))
 
 
    def shortest_distance(self, goal):
 
        return self.dist[goal]
 
    def shortest_path(self, goal):
 
        path = []
        vertice = goal
        while vertice is not None:
            path.append(vertice)
            vertice = self.prev[vertice]
 
        return path[::-1]

G = Graph()
N = int(eval(input()))
for i in range(N-1):
    a, b, c = list(map(int, input().split()))
    G.add_edge(a, b, c)

Q, K = list(map(int, input().split()))
for _ in range(Q):
    x, y = list(map(int, input().split()))
    D1 = Dijkstra(G, x)
    D2 = Dijkstra(G, K)
    print((D1.shortest_distance(K)+D2.shortest_distance(y)))
    


# Optimized solution
from collections import deque
N = int(eval(input()))
D = [[] for _ in range(N)]
for _ in range(N-1):
    a, b, c = list(map(int, input().split()))
    a, b = a-1, b-1
    D[a].append((b, c))
    D[b].append((a, c))

cnt, K = list(map(int, input().split()))
K -= 1
Q = deque([(K, 0)])
Dist = [0]*N
check = [0]*N
while Q:
    n, d = Q.popleft()
    check[n] = 1
    Dist[n] = d
    for v, e in D[n]:
        if check[v]:
            continue
        else:
            Q.append((v, d+e))

for _ in range(cnt):
    x, y = list(map(int, input().split()))
    x, y = x-1, y-1
    print((Dist[x]+Dist[y]))
# Original solution
def getN():
    return int(eval(input()))
def getNM():
    return list(map(int, input().split()))
def getList():
    return list(map(int, input().split()))
def getArray(intn):
    return [int(eval(input())) for i in range(intn)]

from collections import defaultdict, deque, Counter
from sys import exit
import heapq
import math
import copy
from bisect import bisect_left, bisect_right

import sys
sys.setrecursionlimit(1000000000)

N = getN()
dist = [[] for i in range(N + 1)]
for i in range(N - 1):
    a, b, c = getNM()
    dist[a].append([b, c])
    dist[b].append([a, c])
d = [[float('inf')] * (N + 1) for i in range(N + 1)]
ignore = [-1] * (N + 1)

# Kからの最短距離をbfsで測る
def distance(sta):
    # 木をKから順にたどる（戻るの禁止）
    pos = deque([sta])

    while len(pos) > 0:
        u = pos.popleft()
        for i in dist[u]:
            if ignore[i[0]] == -1:
                ignore[i[0]] = ignore[u] + i[1]
                pos.append(i[0])

Q, K = getNM()
ignore[K] = 0
distance(K)
# 答えはK~xまでの距離+K~yまでの距離
ans = []
for i in range(Q):
    x, y = getNM()
    ans.append(ignore[x] + ignore[y])
for i in ans:
    print(i)

# Optimized solution
def getN():
    return int(eval(input()))
def getNM():
    return list(map(int, input().split()))
def getList():
    return list(map(int, input().split()))
def getArray(intn):
    return [int(eval(input())) for i in range(intn)]
def input():
    return sys.stdin.readline().rstrip()

from collections import defaultdict, deque, Counter
from sys import exit
import heapq
import math
import copy
from bisect import bisect_left, bisect_right

import sys
sys.setrecursionlimit(1000000000)

N = getN()
dist = [[] for i in range(N + 1)]
for i in range(N - 1):
    a, b, c = getNM()
    dist[a].append([b, c])
    dist[b].append([a, c])
ignore = [-1] * (N + 1)

# Kからの最短距離をbfsで測る
def distance(sta):
    # 木をKから順にたどる（戻るの禁止）
    pos = deque([sta])

    while len(pos) > 0:
        u = pos.popleft()
        for i in dist[u]:
            if ignore[i[0]] == -1:
                ignore[i[0]] = ignore[u] + i[1]
                pos.append(i[0])

Q, K = getNM()
ignore[K] = 0
distance(K)
# 答えはK~xまでの距離+K~yまでの距離
ans = []
for i in range(Q):
    x, y = getNM()
    ans.append(ignore[x] + ignore[y])
for i in ans:
    print(i)
# Original solution
from queue import Queue

INF=float('inf')

n=int(eval(input()))
g=[[] for _ in range(n)]
for _ in range(n-1):
    u,v,w=list(map(int,input().split()))
    g[u-1].append((v-1,w))
    g[v-1].append((u-1,w))

q,k=list(map(int,input().split()))
ds=[INF]*n
que=Queue()
que.put((k-1,0))
while not que.empty():
    u,d=que.get()
    if ds[u] != INF:
        continue
    ds[u]=d
    for v,w in g[u]:
        que.put((v,d+w))

for _ in range(q):
    u,v=list(map(int,input().split()))
    print((ds[u-1]+ds[v-1]))


# Optimized solution
from queue import Queue

INF=float('inf')

n=int(eval(input()))
g=[[] for _ in range(n)]
for _ in range(n-1):
    u,v,w=list(map(int,input().split()))
    g[u-1].append((v-1,w))
    g[v-1].append((u-1,w))

q,k=list(map(int,input().split()))
ds=[INF]*n
ds[k-1]=0
que=Queue()
que.put((k-1,0))
while not que.empty():
    u,d=que.get()
    for v,w in g[u]:
        if ds[v]==INF:
            ds[v]=d+w
            que.put((v,d+w))

for _ in range(q):
    u,v=list(map(int,input().split()))
    print((ds[u-1]+ds[v-1]))
# Original solution
from queue import Queue

INF=float('inf')

n=int(eval(input()))
g=[[] for _ in range(n)]
for _ in range(n-1):
    u,v,w=list(map(int,input().split()))
    g[u-1].append((v-1,w))
    g[v-1].append((u-1,w))

q,k=list(map(int,input().split()))
ds=[INF]*n
ds[k-1]=0
que=Queue()
que.put((k-1,0))
while not que.empty():
    u,d=que.get()
    for v,w in g[u]:
        if ds[v]==INF:
            ds[v]=d+w
            que.put((v,d+w))

for _ in range(q):
    u,v=list(map(int,input().split()))
    print((ds[u-1]+ds[v-1]))


# Optimized solution
from collections import deque

INF=float('inf')

n=int(eval(input()))
g=[[] for _ in range(n)]
for _ in range(n-1):
    u,v,w=list(map(int,input().split()))
    g[u-1].append((v-1,w))
    g[v-1].append((u-1,w))

q,k=list(map(int,input().split()))
ds=[INF]*n
ds[k-1]=0
que=deque()
que.append((k-1,0))
while len(que):
    u,d=que.popleft()
    for v,w in g[u]:
        if ds[v]==INF:
            ds[v]=d+w
            que.append((v,d+w))

for _ in range(q):
    u,v=list(map(int,input().split()))
    print((ds[u-1]+ds[v-1]))
# Original solution
import sys;sys.setrecursionlimit(int(1e7))

class edge():
    def __init__(self,_to,_cost):
        self.to = _to;
        self.cost = _cost;

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

for i in range(N-1):
    a,b,c = list(map(int,input().split()))
    a = a - 1
    b = b - 1
    tree[a].append(edge(b,c))
    tree[b].append(edge(a,c))
Q,K = list(map(int,input().split()))
K = K - 1
depth = [None]*N

def DFS(v,p,d):
    depth[v] = d
    for e in tree[v]:
        if e.to == p:
            continue
        DFS(e.to, v, d + e.cost)
DFS(K,-1,0)

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


# Optimized solution
import sys;sys.setrecursionlimit(int(1e7))

N = int(eval(input()))

tree = [[] for _ in range(N+1)]
depth = [None]*(N+1)

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

for abc in ABC:
    tree[abc[0]].append((abc[1],abc[2]))
    tree[abc[1]].append((abc[0],abc[2]))

def DFS(v,p,d):
    depth[v] = d
    for e in tree[v]:
        if e[0] != p:
            DFS(e[0], v, d + e[1])

DFS(K,0,0)

for i in range(Q):
    print((depth[XY[i][0]] + depth[XY[i][1]]))
# Original solution
import sys;sys.setrecursionlimit(int(1e7))

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

tree = [[] for _ in range(N+1)]
depth = [None]*(N+1)
for abc in ABC:
    tree[abc[0]].append([abc[1],abc[2]])
    tree[abc[1]].append([abc[0],abc[2]])

def DFS(v,p,d):
    depth[v] = d
    for e in tree[v]:
        if e[0] != p:
            DFS(e[0], v, d + e[1])

DFS(K,0,0)

for i in range(Q):
    print((depth[XY[i][0]] + depth[XY[i][1]]))

# Optimized solution
import sys;sys.setrecursionlimit(int(1e7))

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

tree = [[] for _ in range(N+1)]
depth = [None]*(N+1)
for abc in ABC:
    tree[abc[0]].append((abc[1],abc[2]))
    tree[abc[1]].append((abc[0],abc[2]))

def DFS(v,p,d):
    depth[v] = d
    for e in tree[v]:
        if e[0] != p:
            DFS(e[0], v, d + e[1])

DFS(K,0,0)

for i in range(Q):
    print((depth[XY[i][0]] + depth[XY[i][1]]))