

from heapq import*

import sys

input=sys.stdin.readline

 

def DIJKSTRA(point,d):

    cost      = [1e18]*(n+1)

    Q         = []  #頂点のリスト

    cost[point]=0  #開始地点のコストをゼロとする（当たり前

    heappush(Q,(point,cost[point]))

    while Q:

        prefnd,tmpC=heappop(Q)

        if cost[prefnd]<tmpC:

            continue

        for node,c in d[prefnd]:

            altC=c+tmpC

            if cost[node]>altC:

                cost[node]=altC

                heappush(Q,(node,altC))

    return cost



n=int(eval(input()))

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

for i in range(n-1):

    a,b,c=list(map(int,input().split()))

    d[a].append([b,c])

    d[b].append([a,c])



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

y=DIJKSTRA(k,d)

 

for i in range(q):

    a,b=list(map(int,input().split()))

    print((y[a]+y[b]))

from collections import*

from heapq import*

import sys

input=sys.stdin.readline

 

def BFS(point,d):

    cost=[1e18]*(n+1)

    cost[point]=0

    Q=deque()

    Q.appendleft((0,point))

    

    while Q:

        c,p=Q.pop()

        for np,co in d[p]:

            if cost[np]==1e18:

                cost[np]=c+co

                Q.appendleft((c+co,np))

    return cost



n=int(eval(input()))

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

for i in range(n-1):

    a,b,c=list(map(int,input().split()))

    d[a].append([b,c])

    d[b].append([a,c])

#print(d)

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

y=BFS(k,d)

 

for i in range(q):

    a,b=list(map(int,input().split()))

    print((y[a]+y[b]))

from heapq import *
import sys

input = sys.stdin.readline


def DIJKSTRA(point, d):
    cost = [1e18] * (n + 1)
    Q = []  # 頂点のリスト
    cost[point] = 0  # 開始地点のコストをゼロとする（当たり前
    heappush(Q, (point, cost[point]))
    while Q:
        prefnd, tmpC = heappop(Q)
        if cost[prefnd] < tmpC:
            continue
        for node, c in d[prefnd]:
            altC = c + tmpC
            if cost[node] > altC:
                cost[node] = altC
                heappush(Q, (node, altC))
    return cost


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

from collections import *
from heapq import *
import sys

input = sys.stdin.readline


def BFS(point, d):
    cost = [1e18] * (n + 1)
    cost[point] = 0
    Q = deque()
    Q.appendleft((0, point))
    while Q:
        c, p = Q.pop()
        for np, co in d[p]:
            if cost[np] == 1e18:
                cost[np] = c + co
                Q.appendleft((c + co, np))
    return cost


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

+from collections import * -def DIJKSTRA(point, d): +def BFS(point, d): -    Q = []  # 頂点のリスト -    cost[point] = 0  # 開始地点のコストをゼロとする（当たり前 -    heappush(Q, (point, cost[point])) +    cost[point] = 0 +    Q = deque() +    Q.appendleft((0, point)) -        prefnd, tmpC = heappop(Q) -        if cost[prefnd] < tmpC: -            continue -        for node, c in d[prefnd]: -            altC = c + tmpC -            if cost[node] > altC: -                cost[node] = altC -                heappush(Q, (node, altC)) +        c, p = Q.pop() +        for np, co in d[p]: +            if cost[np] == 1e18: +                cost[np] = c + co +                Q.appendleft((c + co, np)) +# print(d) -y = DIJKSTRA(k, d) +y = BFS(k, d)