CodeEff/ECCO huggingface dataset:
# Original solution
n=int(eval(input()))
edges=[[] for i in range(n)]
for i in range(n-1):
    a,s,d=list(map(int,input().split()))
    edges[a-1].append((s-1,d));edges[s-1].append((a-1,d))
q,k=list(map(int,input().split()))
cost=[0]*n
from collections import deque
dq=deque([])
#pop/append/(append,pop)_left/in/len/count/[]/index/rotate()(右へnずらす)
for to,cos in edges[k-1]:
    dq.append((k-1,to))
    cost[to]=cos
while dq:
    par,now=dq.popleft()
    
    for to,c in edges[now]:
        if to!=par:
            cost[to]=cost[now]+c
            dq.append((now,to))

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

# Optimized solution
import sys
input=sys.stdin.buffer.readline
sys.setrecursionlimit(10**9)
n=int(eval(input()))
edges=[[] for i in range(n)]
for i in range(n-1):
    a,s,d=list(map(int,input().split()))
    edges[a-1].append((s-1,d));edges[s-1].append((a-1,d))
q,k=list(map(int,input().split()))
cost=[0]*n
from collections import deque
dq=deque([])
#pop/append/(append,pop)_left/in/len/count/[]/index/rotate()(右へnずらす)
for to,cos in edges[k-1]:
    dq.append((k-1,to))
    cost[to]=cos
while dq:
    par,now=dq.popleft()
    
    for to,c in edges[now]:
        if to!=par:
            cost[to]=cost[now]+c
            dq.append((now,to))

for i in range(q):
    x,y=list(map(int,input().split()))
    print((cost[x-1]+cost[y-1]))
# Original solution
import sys
import os
from collections import defaultdict
adj = defaultdict(list)
vis =[0 for i in range(1000000)]
dis =[0 for i in range(1000000)]
def dfs(u, p):
	for v, w in adj[u]:
		if v==p:
			continue
		dis[v]=dis[u]+w
		dfs(v, u)

def main():
	n = int(eval(input()))
	for i in range(n-1):
		a, b, w = list(map(int , input().split()))
		adj[a].append((b, w))
		adj[b].append((a, w))

	q, k = list(map(int , input().split()))
	sys.setrecursionlimit(1000000)
	dfs(k , 0)
	for i in range(q):
		x, y  =list(map(int , input().split()))
		print((dis[x]+dis[y]))
#	rec_limit= sys.getrecursionlimit()
#	print(rec_limit)


if __name__ == "__main__":
	main()

# Optimized solution
import sys
import os
from collections import defaultdict
adj = defaultdict(list)
vis =[0 for i in range(1000000)]
dis =[0 for i in range(1000000)]
# since it's a tree shortest path will be only one and unique between two node
# find distance from k to each and every node 
def dfs(u, p):
	for v, w in adj[u]:
		if v==p:
			continue
		dis[v]=dis[u]+w
		dfs(v, u)

def main():
	n = int(eval(input()))
	for i in range(n-1):
		a, b, w = list(map(int , input().split()))
		adj[a].append((b, w))
		adj[b].append((a, w))

	q, k = list(map(int , input().split()))
	sys.setrecursionlimit(1000000)
	dfs(k , 0)
	for i in range(q):
		x, y  =list(map(int , input().split()))
		# print the sum of the two distance from k 
		print((dis[x]+dis[y]))
#	rec_limit= sys.getrecursionlimit()
#	print(rec_limit)


if __name__ == "__main__":
	main()
# Original solution
from heapq import heappop, heappush
n = int(eval(input()))
INF = 10**13
g = [[INF for __ in range(n)] for _ 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


def dijkstra(s, n, edges):
    # edges[i][j] : the cost of i→j
    # import 'heap'
    d = [INF] * n
    d[s] = 0
    q = [(0, s)]
    while q:
        dist_u, u = heappop(q)
        if d[u] < dist_u:
            continue
        for v, c in enumerate(edges[u]):
            if d[v] > dist_u + c:
                d[v] = dist_u + c
                heappush(q, (d[v], v))
    return d


q, k = list(map(int, input().split()))
d = dijkstra(k-1, n, g)

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


# Optimized solution
from heapq import heappop, heappush
n = int(eval(input()))
g = [[] 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))


def dijkstra(s, n, edges):
    # edges[i][j] : the cost of i→j
    # import 'heap'
    d = [10**18] * n
    d[s] = 0
    q = [(0, s)]
    while q:
        dist_u, u = heappop(q)
        if d[u] < dist_u:
            continue
        for v, c in edges[u]:
            if d[v] > dist_u + c:
                d[v] = dist_u + c
                heappush(q, (d[v], v))
    return d


q, k = list(map(int, input().split()))
d = dijkstra(k-1, n, g)

for _ in range(q):
    x, y = list(map(int, input().split()))
    print((d[x-1]+d[y-1]))
# Original solution
# 2019/07/11

import sys
from heapq import heappop,heappush

input=sys.stdin.readline

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


def dijkstra(s):
    d=[float('inf')]*n
    visited=[False]*n
    d[s]=0
    visited[s]=True
    que=[]
    for e in edge[s]:
        heappush(que,e)
    while que:
        c,v=heappop(que)
        if visited[v]:continue
        d[v]=c
        visited[v]=True
        for cost,vtx in edge[v]:
            if visited[vtx]:continue
            hq=(cost+d[v],vtx)
            heappush(que,hq)

    return d

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

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



# Optimized solution
# 2019/07/12
# DFS ver

import sys

sys.setrecursionlimit(10**9)
input=sys.stdin.readline

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

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

d=[0]*n
visited=[False]*n

def dfs(s,p):
    visited[s]=True
    for c,v in edge[s]:
        if visited[v] or p==v:continue
        d[v]=c+d[s]
        visited[v]=True
        dfs(v,s)
    return 


dfs(k,-1)
for _ in range(q):
    x,y=list(map(int,input().split()))
    x-=1 ; y-=1
    print((d[x]+d[y]))
# Original solution
from collections import deque
n=int(input())
e=[[] for _ in range(n+1)]
l=[[0]*(n+1) for _ in range(n+1)]
dist=[-1]*(n+1)
for i in range(n-1):
    a,b,c=map(int,input().split())
    e[a].append(b)
    e[b].append(a)
    l[a][b]=c
    l[b][a]=c
q,k=map(int,input().split())
a=deque()
a.append((k,0))
while a:
    x,d=a.popleft()
    dist[x]=d
    for j in e[x]:
        if dist[j]!=-1:
            continue
        else:
            a.append((j,d+l[x][j]))
ans=[]
for i in range(q):
    x,y=map(int,input().split())
    ans.append(dist[x]+dist[y])
print(*ans,sep="\n")


# Optimized solution
from collections import deque
n=int(input())
e=[[] for _ in range(n+1)]
dist=[-1]*(n+1)
for i in range(n-1):
    a,b,c=map(int,input().split())
    e[a].append((b,c))
    e[b].append((a,c))
q,k=map(int,input().split())
a=deque()
a.append((k,0))
while a:
    x,d=a.popleft()
    dist[x]=d
    for j,nc in e[x]:
        if dist[j]!=-1:
            continue
        else:
            a.append((j,d+nc))
ans=[]
for i in range(q):
    x,y=map(int,input().split())
    ans.append(dist[x]+dist[y])
print(*ans,sep="\n")
# Original solution
#%%
n = int(eval(input()))
a, b, c = [0] * n, [0] * n, [0] * n
for i in range(n-1):
    a[i], b[i], c[i] = list(map(int, input().split()))

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

l = [[] for _ in range(n+1)]
for i in range(n-1):
    l[a[i]].append([b[i],c[i]])
    l[b[i]].append([a[i],c[i]])

d = [0] * (n+1)
check = [0] * (n+1)
check[k] = 1
que = [k]

while len(que) > 0:
    tmp = que.pop(0)
    for i in l[tmp]:
        if check[i[0]] == 0:
            que.append(i[0])
            check[i[0]] = 1
            d[i[0]] = d[tmp] + i[1]
        else:
            pass

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

# Optimized solution
#%%
n = int(eval(input()))
a, b, c = [0] * n, [0] * n, [0] * n
for i in range(n-1):
    a[i], b[i], c[i] = list(map(int, input().split()))

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

l = [[] for _ in range(n+1)]
for i in range(n-1):
    l[a[i]].append([b[i],c[i]])
    l[b[i]].append([a[i],c[i]])

d = [0] * (n+1)
check = [0] * (n+1)
check[k] = 1
que = [k]
count = 1

while len(que) > 0:
    tmp = que.pop(0)
    for i in l[tmp]:
        if check[i[0]] == 0:
            que.append(i[0])
            check[i[0]] = 1
            d[i[0]] = d[tmp] + i[1]
            count += 1
        else:
            pass
    if count >= n:
        break

x, y = [0] * q, [0] * q
for i in range(q):
    x[i], y[i] = list(map(int, input().split()))
    print((d[x[i]] + d[y[i]]))
# Original solution
import sys
sys.setrecursionlimit(10**8)
n = int(eval(input()))
data = [[] for i in range(n)]
for i in range(n-1):
    a,b,c = list(map(int,input().split()))
    a -= 1
    b -= 1
    data[a].append([b,c])
    data[b].append([a,c])

dist = [-1 for i in range(n)]
q,k = list(map(int,input().split()))
k -= 1
dist[k] = 0

def dfs(now,pre,d):
    dist[now] = d
    for p,q in data[now]:
        if p == pre:
            continue
        dfs(p,now,d+q)

dfs(k,-1,0)

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

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

dist = [-1 for i in range(n)]
q,k = list(map(int,input().split()))
k -= 1
dist[k] = 0

def dfs(now,pre,d):
    dist[now] = d
    for p,q in data[now]:
        if p == pre:
            continue
        dfs(p,now,d+q)

dfs(k,-1,0)

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

INF = float('inf')

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

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

Q, K = list(map(int, input().split()))
query = [list([int(x) - 1 for x in input().split()]) for _ in range(Q)]


def dijkstra(s, e):
    hq = [(0, s)]
    heapq.heapify(hq) # リストを優先度付きキューに変換
    cost = [INF] * N # 行ったことのないところはinf
    cost[s] = 0 # 開始地点は0
    
    while hq:
        c, v = heapq.heappop(hq)
        if c > cost[v]: # コストが現在のコストよりも高ければスルー
            continue
        for d, u in to[v]:
            tmp = d + cost[v]
            if tmp < cost[u]:
                cost[u] = tmp
                heapq.heappush(hq, (tmp, u))
    return cost[e]


def main():
    min_path = [dijkstra(i, K - 1) for i in range(N)]
    for x, y in query:
        print((min_path[x] + min_path[y]))


if __name__ == "__main__":
    main()

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


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

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

Q, K = list(map(int, input().split()))
K -= 1
query = [list([int(x) - 1 for x in input().split()]) for _ in range(Q)]
depth = [0] * N

def dfs(v, d, p=-1):
    depth[v] = d
    for cost, nv in to[v]:
        if nv == p:
            continue
        dfs(nv, d + cost, v)


def main():
    dfs(K, 0)
    for x, y in query:
        print((depth[x] + depth[y]))


if __name__ == "__main__":
    main()
# Original solution
from heapq import heappush,heappop,heapify
INF=10**30
def dijkstra(G,s,n):
    que=[(0,s)]
    dist=[INF]*n
    dist[s]=0
    while que:
        mincost,u=heappop(que)
        if(mincost>dist[u]):
            continue
        for v,c in G[u]:
            if(dist[u]+c<dist[v]):
                dist[v]=dist[u]+c
                heappush(que,(dist[v],v))
    return dist
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()))
dist=dijkstra(G,K-1,N)
for i in range(Q):
    x,y=[int(x)-1 for x in input().split()]
    print((dist[x]+dist[y]))

# Optimized solution
import sys
sys.setrecursionlimit(10**6)
def dfs(v,p,d):
    dist[v]=d
    for nv,nd in G[v]:
        if nv==p:
            continue
        dfs(nv,v,d+nd)

N=int(eval(input()))
G=[[] for i in range(N)]
dist=[-1]*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()))
dfs(K-1,-1,0)
for i in range(Q):
    x,y=[int(x)-1 for x in input().split()]
    print((dist[x]+dist[y]))
# Original solution
import sys

input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)


def dfs(G, v, p, dist):
    """G: graph, v: vertex, p: parent"""
    # Loop for each child
    for c, d in G[v]:
        # Avoid multiple access to parent
        if c == p:
            continue
        dist[c] = dist[v] + d
        dfs(G, c, v, dist)


def main():
    N = int(eval(input()))
    G = [[] for _ in range(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()))
    x = [0] * Q
    y = [0] * Q
    for i in range(Q):
        x[i], y[i] = list(map(int, input().split()))

    for i in range(Q):
        dist_a = [0] * N
        dist_b = [0] * N
        dfs(G, x[i]-1, -1, dist_a)
        dfs(G, K-1, -1, dist_b)
        ans = dist_a[K-1] + dist_b[y[i]-1]
        print(ans)


if __name__ == "__main__":
    main()


# Optimized solution
import sys

input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)


def dfs(G, v, p, dist):
    """G: graph, v: vertex, p: parent"""
    # Loop for each child
    for c, d in G[v]:
        # Avoid multiple access to parent
        if c == p:
            continue
        dist[c] = dist[v] + d
        dfs(G, c, v, dist)


def main():
    N = int(eval(input()))
    G = [[] for _ in range(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()))
    x = [0] * Q
    y = [0] * Q
    for i in range(Q):
        x[i], y[i] = list(map(int, input().split()))

    dist = [0] * N
    dfs(G, K-1, -1, dist)
    for i in range(Q):
        ans = dist[x[i]-1] + dist[y[i]-1]
        print(ans)


if __name__ == "__main__":
    main()
# Original solution
# Kから各頂点への距離をメモする
# xからKの距離とyからKの距離を足したものが答え

import sys
readline = sys.stdin.readline

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

dist = [-1 for i in range(N)] # Kからの距離
Q,K = list(map(int,readline().split()))

dist[K - 1] = 0
from collections import deque
# 頂点、Kからの距離, 親
q = deque([[K - 1, 0, -1]])
while q:
  v, d, parent = q.popleft()
  dist[v] = d
  for child in G[v]:
    if child[0] == parent:
      continue
    q.append([child[0],d + child[1], v])

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

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

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

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

dist = [0] * N
stack = [(K - 1, -1, 0)]
while stack:
  v,parent,cost = stack.pop()
  dist[v] = cost
  for child, c in G[v]:
    if child == parent:
      continue
    stack.append((child, v, cost + c))
    
for i in range(Q):
  x,y = list(map(int,readline().split()))
  print((dist[x - 1] + dist[y - 1]))
# Original solution
from collections import Counter,defaultdict,deque
import sys
import copy
from itertools import permutations, combinations
from heapq import heappop, heappush
input = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9+7

def main():
    n=int(eval(input()))
    graph = [[] for _ in range(n)]
    for _ in range(n-1):
        a,b,w=list(map(int,input().split()))
        a-=1
        b-=1
        graph[a].append((b,w))
        graph[b].append((a,w))
    graph = tuple(graph)
    q,k = list(map(int,input().split()))
    pre = []
    que = deque()
    for e in range(n):
        dis = [-1 for _ in range(n)]
        dis[e]=0
        if e == k-1:
            pre.append(0)
        else:
            que.append(e)
        while que:
            i = que.pop()
            for j in range(len(graph[i])):
                go = graph[i][j][0]
                d = graph[i][j][1]
                if dis[go]!=-1:
                    continue
                dis[go] = dis[i] + d
                if k-1 == go:
                    pre.append(dis[go])
                    que=deque()
                    break
                que.append(go)
    pre = tuple(pre)
    for _ in range(q):
        x,y = list(map(int,input().split()))
        print(( pre[x-1] + pre[y-1] )) 

if __name__ == "__main__":
    main()

# Optimized solution
from collections import Counter,defaultdict,deque
import sys
import copy
from itertools import permutations, combinations
from heapq import heappop, heappush
input = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9+7

def main():
    n=int(eval(input()))
    graph = [[] for _ in range(n)]
    for _ in range(n-1):
        a,b,w=list(map(int,input().split()))
        a-=1
        b-=1
        graph[a].append((b,w))
        graph[b].append((a,w))
    graph = tuple(graph)
    q,k = list(map(int,input().split()))
    que = deque([k-1])
    dis = [-1 for _ in range(n)]
    dis[k-1]=0
    while que:
        i = que.pop()
        for j in range(len(graph[i])):
            go = graph[i][j][0]
            d = graph[i][j][1]
            if dis[go]==-1:
                dis[go] = dis[i] + d
                que.append(go)
    for _ in range(q):
        x,y = list(map(int,input().split()))
        print(( dis[x-1] + dis[y-1] )) 

if __name__ == "__main__":
    main()
# Original solution
import queue
N=int(eval(input()))
d=[-1 for _ in range(N+1)]
e=[[] for _ in range(N+1)]
for _ in range(N-1):
	a,b,c=list(map(int,input().split()))
	e[a].append([b, c])
	e[b].append([a, c])
Q,K=list(map(int,input().split()))
d[K]=0
q=queue.Queue()
q.put(K)
while not q.empty():
	now=q.get()
	for nx,c in e[now]:
		if d[nx] == -1:
			d[nx] = c+d[now]
			q.put(nx)
for _ in range(Q):
	x,y=list(map(int,input().split()))
	print((d[x]+d[y]))


# Optimized solution
import sys
sys.setrecursionlimit(1145141919)
N=int(eval(input()))
d=[-1 for _ in range(N+1)]
e=[[] for _ in range(N+1)]
for _ in range(N-1):
	a,b,c=list(map(int,input().split()))
	e[a].append((b, c))
	e[b].append((a, c))
Q,K=list(map(int,input().split()))
def dfs(now, dis):
	d[now] = dis
	for nxt,cos in e[now]:
		if d[nxt] == -1:
			dfs(nxt, dis+cos)
dfs(K, 0)
for _ in range(Q):
	x,y=list(map(int,input().split()))
	print((d[x]+d[y]))
# Original solution
from collections import deque
def main():
    n = int(eval(input()))
    G = [[] for _ in range(n)]
    D = [[] for _ in range(n)]
    for _ in range(n - 1):
        a, b, c = list(map(int, input().split()))
        a -= 1
        b -= 1
        G[a].append(b)
        D[a].append(c)
        G[b].append(a)
        D[b].append(c)
    q, k = list(map(int, input().split()))
    k -= 1
    stack = deque([k, ])
    ans = [-1] * n
    ans[k] = 0
    while stack:
        x = stack.popleft()
        for dx, l in zip(G[x], D[x]):
            if ans[dx] == -1:
                ans[dx] = ans[x] + l
                stack.appendleft(dx)
    for _ in range(q):
        x, y = list([int(x) - 1 for x in input().split()])
        print((ans[x] + ans[y]))

if __name__ == '__main__':
    main()


# Optimized solution
from collections import deque
import sys
def input():
    return sys.stdin.readline().rstrip()
def main():
    n = int(eval(input()))
    G = [[] for _ in range(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
    stack = deque([k, ])
    ans = [-1] * n
    ans[k] = 0
    while stack:
        x = stack.popleft()
        for (dx, l) in G[x]:
            if ans[dx] == -1:
                ans[dx] = ans[x] + l
                stack.appendleft(dx)
    for _ in range(q):
        x, y = list([int(x) - 1 for x in input().split()])
        print((ans[x] + ans[y]))

if __name__ == '__main__':
    main()
# Original solution
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()))

SD=[0]*N

for x,d in G[K-1]:
    SD[x]=d

for i in range(N):
    if i==K-1:continue
    ans=[]
    seen=[0]*N
    stack=[[i,0]]
    seen[i]=1
    while stack:
        x,d=stack.pop()
        if x==K-1:
            ans.append(d)
        elif SD[x]!=0:
            ans.append(d+SD[x])
        else:
            for y,co in G[x]:
                if seen[y]==0:
                    stack.append([y,d+co])
                    seen[y]=1
    SD[i]=min(ans)


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



# Optimized solution
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()))

SD=[0]*N
seen=[0]*N
stack=[]
stack.append([K-1,0])
seen[K-1]=1
while stack:
    x,d=stack.pop()
    for y,d0 in G[x]:
        if seen[y]==0:
            SD[y]=d+d0
            stack.append([y,d+d0])
            seen[y]=1
for i in range(Q):
    x,y=list(map(int,input().split()))
    ans=SD[x-1]+SD[y-1]
    print(ans)
# Original solution
import sys
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline

n = int(input())
from collections import defaultdict
abc = [tuple(map(int,input().split())) for i in range(n-1)]
cost = [[-1]*n for i in range(n)]
for a,b,c in abc:
    a,b = a-1,b-1
    cost[a][b]=c
    cost[b][a]=c

q, k = map(int, input().split())
xy = [tuple(map(int,input().split())) for i in range(q)]

dist = [-1]*n

def dfs(v=0,parent=-1):

    if dist[v]==-1:
        dist[v] = dist[parent] + cost[v][parent]
    for i,d in enumerate(cost[v]):
        if d!=-1 and dist[i]==-1:
            dfs(i,v)

dist[k-1] =0
dfs(k-1)
ans=[]
for x,y in xy:
    x,y=x-1,y-1
    ans.append(dist[x]+dist[y])

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


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

n = int(input())
from collections import defaultdict
abc = [tuple(map(int,input().split())) for i in range(n-1)]
es = [[] for i in range(n)]
for a,b,c in abc:
    a,b = a-1,b-1
    es[a].append((b,c))
    es[b].append((a,c))

q, k = map(int, input().split())
xy = [tuple(map(int,input().split())) for i in range(q)]

dist = [-1]*n

def dfs(v=0,parent=-1,d=0):
    for next_v,dd in es[v]:
        if dist[next_v] == -1:
            dist[next_v] = d + dd
            dfs(next_v,v,dist[next_v])

dist[k-1] =0
dfs(k-1,-1,0)
ans=[]
for x,y in xy:
    x,y=x-1,y-1
    ans.append(dist[x]+dist[y])

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