from collections import *

from heapq import*

import sys

input=sys.stdin.readline

d=defaultdict(list)

n=int(eval(input()))

 

for s in range(n-1):

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

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

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

#defaultdict(<class 'list'>, {1: [[2, 1], [3, 1]], 2: [[1, 1], [3, 3]], 3: [[1, 1], [2, 3]]})

 

def DIJKSTRA(point,d):

    cost      = {node:float("inf") for node in list(d.keys())}

    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

 

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

d=defaultdict(list)

n=int(eval(input()))

 

for s in range(n-1):

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

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

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

#defaultdict(<class 'list'>, {1: [[2, 1], [3, 1]], 2: [[1, 1], [3, 3]], 3: [[1, 1], [2, 3]]})

 

def DIJKSTRA(point,d):

    cost      = {node:1e18 for node in list(d.keys())}

    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

 

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
d = defaultdict(list)
n = int(eval(input()))
for s in range(n - 1):
    a, b, c = list(map(int, input().split()))
    d[a].append([b, c])
    d[b].append([a, c])
# defaultdict(<class 'list'>, {1: [[2, 1], [3, 1]], 2: [[1, 1], [3, 3]], 3: [[1, 1], [2, 3]]})
def DIJKSTRA(point, d):
    cost = {node: float("inf") for node in list(d.keys())}
    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


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
d = defaultdict(list)
n = int(eval(input()))
for s in range(n - 1):
    a, b, c = list(map(int, input().split()))
    d[a].append([b, c])
    d[b].append([a, c])
# defaultdict(<class 'list'>, {1: [[2, 1], [3, 1]], 2: [[1, 1], [3, 3]], 3: [[1, 1], [2, 3]]})
def DIJKSTRA(point, d):
    cost = {node: 1e18 for node in list(d.keys())}
    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


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

-    cost = {node: float("inf") for node in list(d.keys())} +    cost = {node: 1e18 for node in list(d.keys())}