CodeEff/ECCO huggingface dataset:
# Original solution
import sys;sys.setrecursionlimit(int(1e7))
from collections import deque

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

Qn,K = list(map(int,input().split()))
XY = [tuple(map(int,input().split())) for _ in range(Qn)]

dist = [-1]*(N+1)
dist[K] = 0
Q = deque()
Q.append(K)
while len(Q):
    p = Q.popleft()
    for q,c in tree[p]:
        if dist[q] != -1: continue
        dist[q] = dist[p]+c
        Q.append(q)

for i in range(Qn):
    print((dist[XY[i][0]] + dist[XY[i][1]]))

# Optimized solution
import sys;sys.setrecursionlimit(int(1e7))
from sys import stdin
from collections import deque

input = stdin.readline
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))

Qn,K = list(map(int,input().split()))
XY = [tuple(map(int,input().split())) for _ in range(Qn)]

dist = [-1]*(N+1)
dist[K] = 0
Q = deque()
Q.append(K)
while len(Q):
    p = Q.popleft()
    for q,c in tree[p]:
        if dist[q] != -1: continue
        dist[q] = dist[p]+c
        Q.append(q)

for i in range(Qn):
    print((dist[XY[i][0]] + dist[XY[i][1]]))
# Original solution
N=int(eval(input()))
from collections import defaultdict
from collections import deque
branch=defaultdict(set)
for i in range(N-1):
    a,b,c=list(map(int,input().split()))
    branch[a]|={(b,c),}
    branch[b]|={(a,c),}
Q,K=list(map(int,input().split()))
dist=[float('inf')]*(N+1)
dist[K]=0
check=deque([K])
while len(check)>0:
    now=check.popleft()
    for nex,c in branch[now]:
        if dist[nex]>dist[now]+c:
            dist[nex]=dist[now]+c
            check.append(nex)
for q in range(Q):
    x,y=list(map(int,input().split()))
    print((dist[x]+dist[y]))

# Optimized solution
N=int(eval(input()))
from collections import defaultdict
branch=defaultdict(dict)
for i in range(N-1):
    a,b,c=list(map(int,input().split()))
    branch[a-1][b-1]=c
    branch[b-1][a-1]=c
Q,K=list(map(int,input().split()))
dist=[float('inf') for i in range(N)]
K-=1
dist[K]=0
check={K}
while len(check)>0:
    now=check.pop()
    for nex,cost in list(branch[now].items()):
        if dist[nex]>dist[now]+cost:
            dist[nex]=dist[now]+cost
            check|={nex}
for i in range(Q):
    x,y=list(map(int,input().split()))
    print((dist[x-1]+dist[y-1]))
# Original solution
import sys
input = sys.stdin.readline
N = int(eval(input()))

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

cost = [-1] * (N+1)
cost[K] = 0
temp = [K]
while temp:
    p = temp.pop()
    for q in g[p]:
        if cost[q[0]] == -1:
            cost[q[0]] = cost[p] + q[1]
            temp.append(q[0])

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


# Optimized solution
import sys
input = sys.stdin.readline
N = int(eval(input()))

g = [[] for _ in range(N+1)]

for i 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()))

cost = [-1] * (N+1)
cost[K] = 0
temp = [K]
while temp:
    p = temp.pop()
    for q in g[p]:
        if cost[q[0]] == -1:
            cost[q[0]] = cost[p] + q[1]
            temp.append(q[0])

for i in range(Q):
    x, y = list(map(int, input().split()))
    print((cost[x] + cost[y]))
# Original solution
import heapq
def dijkstra_heap(s):
    d = [float("inf")] * n
    used = [True] * n 
    d[s] = 0
    used[s] = False
    edgelist = []
    for e in edge[s]:
        heapq.heappush(edgelist,e)
    while len(edgelist):
        minedge = heapq.heappop(edgelist)
        if not used[minedge[1]]:
            continue
        v = minedge[1]
        d[v] = minedge[0]
        used[v] = False
        for e in edge[v]:
            if used[e[1]]:
                heapq.heappush(edgelist,[e[0]+d[v],e[1]])
    return d
import sys
input = sys.stdin.readline
n =int(eval(input()))

edge = [[] for i in range(n)]
#edge[i] : [weght,to]
for i in range(n-1):
    a, b, c = list(map(int,input().split()))
    a, b = a-1, b-1
    edge[a].append([c,b])
    edge[b].append([c,a]) 
q, k = list(map(int, input().split()))
k = k-1
d = dijkstra_heap(k)
for i in range(q):
  x, y = list(map(int, input().split()))
  x, y = x-1, y-1
  print((d[x]+d[y]))

# Optimized solution
import heapq
def dijkstra_heap(s):
    d = [float("inf")] * n
    used = [True] * n #True: not used
    d[s] = 0
    used[s] = False
    edgelist = []
    for e in edge[s]:
        heapq.heappush(edgelist,e)
    while len(edgelist):
        minedge = heapq.heappop(edgelist)
        if not used[minedge[1]]:
            continue
        v = minedge[1]
        d[v] = minedge[0]
        used[v] = False
        for e in edge[v]:
            if used[e[1]]:
                heapq.heappush(edgelist,(e[0]+d[v],e[1]))
    return d
import sys
input = sys.stdin.readline
n =int(eval(input()))
 
edge = [[] for i in range(n)]
#edge[i] : [weght,to]
for i in range(n-1):
    a, b, c = list(map(int,input().split()))
    a, b = a-1, b-1
    edge[a].append((c,b))
    edge[b].append((c,a)) 
q, k = list(map(int, input().split()))
k = k-1
d = dijkstra_heap(k)
for i in range(q):
  x, y = list(map(int, input().split()))
  x, y = x-1, y-1
  print((d[x]+d[y]))
# Original solution
n = int(eval(input()))
g = {}
for i in range(1,n+1):
    g[i] = []
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()))
q = [list(map(int,input().split())) for _ in range(Q)]
from collections import deque
queue = deque(g[k])
shortest = [0]*(n+1)
while queue:
    a,c = queue.pop()
    shortest[a] = c
    for v in g[a]:
        if shortest[v[0]]==0:
            queue.appendleft([v[0],v[1]+c])
for x,y in q:
    print((shortest[x]+shortest[y]))

# Optimized solution
n = int(eval(input()))
g = {}
from collections import deque
for i in range(1,n+1):
    g[i] = deque([])
for _ in range(n-1):
    a,b,c = list(map(int,input().split()))
    g[a].appendleft([b,c])
    g[b].appendleft([a,c])
Q,k = list(map(int,input().split()))
q = [list(map(int,input().split())) for _ in range(Q)]
queue = deque(g[k])
shortest = [0]*(n+1)
while queue:
    a,c = queue.pop()
    shortest[a] = c
    while g[a]:
        v = g[a].pop()
        if shortest[v[0]]==0:
            queue.appendleft([v[0],v[1]+c])
for x,y in q:
    print((shortest[x]+shortest[y]))
# Original solution
import heapq

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

n = int(eval(input()))
e = [[] for _ in range(n)]
for _ in range(n-1):
	a,b,c = list(map(int,input().split()))
	a,b = a-1, b-1
	e[a].append((c, b))
	e[b].append((c, a))
q,k = list(map(int,input().split()))
for _ in range(q):
	x,y = list(map(int,input().split()))
	print((dijkstra(x-1)[k-1] + dijkstra(k-1)[y-1]))

# Optimized solution
import heapq

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

n = int(eval(input()))
e = [[] for _ in range(n)]
for _ in range(n-1):
	a,b,c = list(map(int,input().split()))
	a,b = a-1, b-1
	e[a].append((c, b))
	e[b].append((c, a))
q,k = list(map(int,input().split()))
l = dijkstra(k-1)
for _ in range(q):
	x,y = list(map(int,input().split()))
	print((l[x-1] + l[y-1]))
# Original solution
#!/usr/bin/env python3
#ABC70 D

import sys
import math
import bisect
sys.setrecursionlimit(1000000000)
from heapq import heappush, heappop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7

n = int(eval(input()))
abc = [list(map(int,input().split())) for _ in range(n-1)]
q,k = list(map(int,input().split()))

g = [[] for _ in range(n)]
cost = [[float('inf')]*n for _ in range(n)]
for a,b,c in abc:
    g[a-1].append(b-1)
    g[b-1].append(a-1)
    cost[a-1][b-1] = c
    cost[b-1][a-1] = c

que = deque()
que.append((0,k-1))
c = [False]*n
dist = [float('inf')]*n
while que:
    d,v = que.popleft()
    dist[v] = min(dist[v],d)
    if not c[v]:
        c[v] = True
        for u in g[v]:
            que.append((d+cost[v][u],u))
for _ in range(q):
    x,y = list(map(int,input().split()))
    print((dist[x-1] + dist[y-1]))


# Optimized solution
#!/usr/bin/env python3
#ABC70 D

import sys
import math
import bisect
sys.setrecursionlimit(1000000000)
from heapq import heappush, heappop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7

n = int(eval(input()))
abc = [list(map(int,input().split())) for _ in range(n-1)]
q,k = list(map(int,input().split()))

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

que = deque()
que.append(k-1)
dist = [-1]*n
dist[k-1] = 0
while que:
    v = que.popleft()
    for x,c in g[v]:
        if dist[x] == -1:
            dist[x] = dist[v] + c
            que.append(x)

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

N = int(eval(input()))
edges = {i: [] for i in range(N)}
for i in range(N - 1):
    a, b, c = list(map(int, input().split(" ")))
    edges[a - 1].append((b - 1, c))
    edges[b - 1].append((a - 1, c))
Q, K = list(map(int, input().split(" ")))

# Kからの最短距離を計算
dist = [-1 for i in range(N)]
que = []
heapq.heappush(que, (0, K - 1, -1)) # (distance, index, parent)
while len(que) > 0:
    d, ind, par = heapq.heappop(que)
    if dist[ind] != -1:
        continue
    dist[ind] = d
    for edge in edges[ind]:
        if edge[0] == par:
            continue
        heapq.heappush(que, (d + edge[1], edge[0], ind))

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

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

N = int(eval(input()))
edges = {i: [] for i in range(N)}
for i in range(N - 1):
    a, b, c = list(map(int, input().split(" ")))
    edges[a - 1].append((b - 1, c))
    edges[b - 1].append((a - 1, c))
Q, K = list(map(int, input().split(" ")))

# Kからの最短距離を計算
dist = [-1 for i in range(N)]
que = []
heapq.heappush(que, (0, K - 1, -1)) # (distance, index, parent)
while len(que) > 0:
    d, ind, par = heapq.heappop(que)
    if dist[ind] != -1:
        continue
    dist[ind] = d
    for edge in edges[ind]:
        if edge[0] == par:
            continue
        heapq.heappush(que, (d + edge[1], edge[0], ind))

for _ in range(Q):
    x, y = list(map(int, input().split(" ")))
    print((dist[x - 1] + dist[y - 1]))
# Original solution
# -*- coding: utf-8 -*-
from collections import defaultdict,deque
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)]

graph = defaultdict(lambda: defaultdict(int))

for a,b,c in abc:
    graph[a-1][b-1] = c
    graph[b-1][a-1] = c

def xfs(v,g):
    que = deque()
    que.append([v,0])
    while len(que) > 0:
        # i,dist = que.pop()  # 深さ優先探索        
        i,dist = que.popleft() #幅優先探索
        if i == g:
            return dist
        for n, val in list(graph[i].items()):
            que.append([n,dist+val])
    return -1

for x,y in xy:
    print((xfs(x-1,K-1) + xfs(K-1,y-1)))


# Optimized solution
# -*- coding: utf-8 -*-
from collections import defaultdict,deque
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)]

# graph = defaultdict(lambda: defaultdict(int))
graph = [defaultdict(int) for _ in range(N)]

for a,b,c in abc:
    graph[a-1][b-1] = c
    graph[b-1][a-1] = c

def xfs(v):
    dist = [-1 for _ in range(N)]
    dist[v] = 0
    que = deque()
    que.append(v)
    while len(que) > 0:
        i = que.pop()  # 深さ優先探索        
        # i = que.popleft() #幅優先探索
        for n, val in list(graph[i].items()):
            if dist[n] == -1:
                dist[n] = dist[i] + val
                que.append(n)
    return dist

dist = xfs(K-1)

for x,y in xy:
    print((dist[x-1] + dist[y-1]))
# Original solution
#dojkstra (prioroty queue ver.)
import queue
G=[]
inf=3*10**15
#V, E=map(int,input().strip().split(' '))
V=int(input().strip())
E=V-1

for i in range(V):
    G.append([])
for i in range(E):
    at,bt,ct=list(map(int,input().strip().split(' ')))
    G[at-1].append([ct,bt-1])
    G[bt-1].append([ct,at-1]) #無向グラフのときはon
'''
#入力が0-indexedのとき
    G[at].append([ct,bt])
    G[bt].append([ct,at]) #無向グラフのときはon
'''
#入力が1-indexedのとき


def dijkstra(s):
    d=[inf]*V
    d[s]=0
    q=queue.PriorityQueue()
    q.put([0,s])
    while q.empty()==False:
        dq,v=q.get()
        if d[v]<dq:
            continue
        for i in range(len(G[v])):
            ec, et=G[v][i]
            #print(ec,et)
            if d[et]>d[v]+ec:
                d[et]=d[v]+ec
                q.put([d[et],et])
    return d

d=[]

for i in range(V):
    d.append(dijkstra(i))

Q, K=list(map(int,input().strip().split(' ')))
K-=1 #0-indexedに直す
x=[]
y=[]
for i in range(Q):
    xt, yt=list(map(int,input().strip().split(' ')))
    x.append(xt-1)#0-indexedに直して入れる
    y.append(yt-1)#0-indexedに直して入れる
for i in range(Q):
    print((d[x[i]][K]+d[K][y[i]]))

# Optimized solution
#dojkstra (prioroty queue ver.)
import queue
G=[]
#infの値に注意
inf=3*10**15
#V, E=map(int,input().strip().split(' '))
V=int(input().strip())
E=V-1

for i in range(V):
    G.append([])
for i in range(E):
    at,bt,ct=list(map(int,input().strip().split(' ')))
    G[at-1].append([ct,bt-1])
    G[bt-1].append([ct,at-1]) #無向グラフのときはon
'''
#入力が0-indexedのとき
    G[at].append([ct,bt])
    G[bt].append([ct,at]) #無向グラフのときはon
'''
#入力が1-indexedのとき


def dijkstra(s):
    d=[inf]*V
    d[s]=0
    q=queue.PriorityQueue()
    q.put([0,s])
    while q.empty()==False:
        dq,v=q.get()
        if d[v]<dq:
            continue
        for i in range(len(G[v])):
            ec, et=G[v][i]
            #print(ec,et)
            if d[et]>d[v]+ec:
                d[et]=d[v]+ec
                q.put([d[et],et])
    return d

Q, K=list(map(int,input().strip().split(' ')))
K-=1 #0-indexedに直す
x=[]
y=[]

d=dijkstra(K)

for i in range(Q):
    xt, yt=list(map(int,input().strip().split(' ')))
    x.append(xt-1)#0-indexedに直して入れる
    y.append(yt-1)#0-indexedに直して入れる
for i in range(Q):
    print((d[x[i]]+d[y[i]]))
# Original solution
#dojkstra (prioroty queue ver.)
import queue
G=[]
#infの値に注意
inf=3*10**15
#V, E=map(int,input().strip().split(' '))
V=int(input().strip())
E=V-1

for i in range(V):
    G.append([])
for i in range(E):
    at,bt,ct=list(map(int,input().strip().split(' ')))
    G[at-1].append([ct,bt-1])
    G[bt-1].append([ct,at-1]) #無向グラフのときはon
'''
#入力が0-indexedのとき
    G[at].append([ct,bt])
    G[bt].append([ct,at]) #無向グラフのときはon
'''
#入力が1-indexedのとき


def dijkstra(s):
    d=[inf]*V
    d[s]=0
    q=queue.PriorityQueue()
    q.put([0,s])
    while q.empty()==False:
        dq,v=q.get()
        if d[v]<dq:
            continue
        for i in range(len(G[v])):
            ec, et=G[v][i]
            #print(ec,et)
            if d[et]>d[v]+ec:
                d[et]=d[v]+ec
                q.put([d[et],et])
    return d

Q, K=list(map(int,input().strip().split(' ')))
K-=1 #0-indexedに直す
x=[]
y=[]

d=dijkstra(K)

for i in range(Q):
    xt, yt=list(map(int,input().strip().split(' ')))
    x.append(xt-1)#0-indexedに直して入れる
    y.append(yt-1)#0-indexedに直して入れる
for i in range(Q):
    print((d[x[i]]+d[y[i]]))

# Optimized solution
import sys
#limit=sys.getrecursionlimit()
sys.setrecursionlimit(10000000)
#sys.setrecursionlimit(1000000000)

V=int(input().strip())
E=V-1
inf=3*10**15
G=[]
dist=[inf]*V

for i in range(V):
    G.append([])
for i in range(E):
    at,bt,ct=list(map(int,input().strip().split(' ')))
    G[at-1].append([ct,bt-1])
    G[bt-1].append([ct,at-1])

visited=[False]*V
def dfs(v,p,d):
    #global d
    dist[v]=d
    for q in G[v]:
        if q[1]==p:
            continue
        dfs(q[1],v,d+q[0])

Q, K=list(map(int,input().strip().split(' ')))
K-=1 #0-indexedに直す

dfs(K,-1,0)

x=[]
y=[]

for i in range(Q):
    xt, yt=list(map(int,input().strip().split(' ')))
    x.append(xt-1)#0-indexedに直して入れる
    y.append(yt-1)#0-indexedに直して入れる
for i in range(Q):
    print((dist[x[i]]+dist[y[i]]))
# Original solution
#!/usr/bin/python3
# -*- coding:utf-8 -*-

from collections import defaultdict
import sys
sys.setrecursionlimit(10**9)

def main():
  n = int(eval(input()))
  tree = defaultdict(lambda : {'parent':-1, 'score':0, 'children':[]})
  weights = dict()
  for _ in range(n-1):
    a, b, c = list(map(int, input().split()))
    tree[a]['children'].append(b)
    tree[b]['children'].append(a)
    weights[(a, b)] = weights[(b, a)] = c
    
  q, k = list(map(int, input().split()))  
  visited = [False] * (n+1)
  iroot = k
  def dfs(inode, score=0):
    visited[inode] = True
    for ichild in tree[inode]['children']:
      if visited[ichild]:
        continue
      tree[ichild]['score'] = weights[(inode, ichild)] + score
      tree[ichild]['parent'] = inode
      dfs(ichild, tree[ichild]['score'])
  dfs(iroot)
  for _ in range(q):
    x, y = list(map(int, input().split()))
    print((tree[x]['score'] + tree[y]['score']))

if __name__=='__main__':
  main()



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

import sys
sys.setrecursionlimit(10**9)

def main():
  n = int(eval(input()))
  inode2scores = [0] * (n+1)
  tree = [[] for _ in range(n+1)]
  weights = dict()
  for _ in range(n-1):
    a, b, c = list(map(int, input().split()))
    tree[a].append(b)
    tree[b].append(a)
    weights[(a, b)] = weights[(b, a)] = c
    
  q, k = list(map(int, input().split()))  
  visited = [False] * (n+1)
  iroot = k
  def dfs(inode, score=0):
    visited[inode] = True
    for ichild in tree[inode]:
      if visited[ichild]:
        continue
      inode2scores[ichild] = weights[(inode, ichild)] + score
      dfs(ichild, inode2scores[ichild])
  dfs(iroot)
  for _ in range(q):
    x, y = list(map(int, input().split()))
    print((inode2scores[x] + inode2scores[y]))

if __name__=='__main__':
  main()
# Original solution
from heapq import heappush, heappop
from collections import deque
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

INF = float("inf")
d = [INF] * N
d[K] = 0
que = [(0, K)]
while que:
    cost, v = heappop(que)
    if d[v] < cost:
        continue
    for w, c in G[v]:
        if d[v] + c < d[w]:
            d[w] = d[v] + c
            heappush(que, (d[w], w))

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


# Optimized solution
import sys
sys.setrecursionlimit(10 ** 6)
N = int(eval(input()))
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 = [-1] * N


def dfs(u, par, d):
    dist[u] = d
    for w, cost in G[u]:
        if w == par:
            continue
        dfs(w, u, d + cost)


dfs(K - 1, -1, 0)

for i 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
sys.setrecursionlimit(10 ** 6)
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 = [-1] * N


def dfs(u, par, d):
    dist[u] = d
    for w, cost in G[u]:
        if w == par:
            continue
        dfs(w, u, d + cost)


dfs(K - 1, -1, 0)

for i 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 ** 6)
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))
d = [0] * N


def dfs(u, p, k):
    d[u] = k
    for w, c in G[u]:
        if w == p:
            continue
        dfs(w, u, k + c)


Q, K = list(map(int, input().split()))
dfs(K - 1, -1, 0)
for i in range(Q):
    x, y = list(map(int, input().split()))
    x, y = x - 1, y - 1
    print((d[x] + d[y]))
# Original solution
from collections import deque

def bfs(start):
    queue = deque([start])
    visited = []

    while queue:
        label = queue.pop()

        if label not in visited:
            visited.append(label)

            for v in d[label]:
                if dist[v[0]] == float("inf"):
                    dist[v[0]] = dist[label] + v[1]
                    queue.appendleft(v[0])

    return

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

q, k = list(map(int, input().split()))
dist = [float("inf") for _ in range(n)] # nは頂点数
dist[k - 1] = 0

bfs(k - 1)

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


# Optimized solution
from collections import deque

def bfs(start):
    queue = deque([start])
    visited = []

    while queue:
        label = queue.pop()

        #if label not in visited:
        visited.append(label)

        for v in d[label]:
            if dist[v[0]] == float("inf"):
                dist[v[0]] = dist[label] + v[1]
                queue.appendleft(v[0])

    return

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

q, k = list(map(int, input().split()))
dist = [float("inf") for _ in range(n)] # nは頂点数
dist[k - 1] = 0

bfs(k - 1)

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