import sys

import heapq

input = sys.stdin.readline

n = int(eval(input()))

edges = [[]for _ in range(n)]

for _ in range(n-1):

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

    a -= 1

    b -= 1

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

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

INF = float('inf')





def dijkstra(init_v):

    dist = [INF]*n

    dist[init_v] = 0

    tasks = [(0, init_v)]

    while tasks:

        d, v = heapq.heappop(tasks)

        if dist[v] < d:

            continue

        for d2, v2 in edges[v]:

            if dist[v2] <= dist[v]+d2:

                continue

            dist[v2] = dist[v]+d2

            heapq.heappush(tasks, ((dist[v2], v2)))

    return dist





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

dist = dijkstra(k-1)

for _ in range(q):

    x, y = [int(x)-1 for x in input().split()]

    print((dist[x]+dist[y]))
import sys

input = sys.stdin.readline

sys.setrecursionlimit(10**7)

n = int(eval(input()))

edges = [[]for _ in range(n)]

for _ in range(n-1):

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

    a -= 1

    b -= 1

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

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

INF = float('inf')





def dfs(v, parent, distance):

    dist[v] = distance

    for d, v2 in edges[v]:

        if parent == v2:

            continue

        dfs(v2, v, distance+d)





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

dist = [INF]*n

dfs(k-1, -1, 0)

for _ in range(q):

    x, y = [int(x)-1 for x in input().split()]

    print((dist[x]+dist[y]))
import sys
import heapq

input = sys.stdin.readline
n = int(eval(input()))
edges = [[] for _ in range(n)]
for _ in range(n - 1):
    a, b, c = list(map(int, input().split()))
    a -= 1
    b -= 1
    edges[a].append((c, b))
    edges[b].append((c, a))
INF = float("inf")


def dijkstra(init_v):
    dist = [INF] * n
    dist[init_v] = 0
    tasks = [(0, init_v)]
    while tasks:
        d, v = heapq.heappop(tasks)
        if dist[v] < d:
            continue
        for d2, v2 in edges[v]:
            if dist[v2] <= dist[v] + d2:
                continue
            dist[v2] = dist[v] + d2
            heapq.heappush(tasks, ((dist[v2], v2)))
    return dist


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

import sys

input = sys.stdin.readline
sys.setrecursionlimit(10**7)
n = int(eval(input()))
edges = [[] for _ in range(n)]
for _ in range(n - 1):
    a, b, c = list(map(int, input().split()))
    a -= 1
    b -= 1
    edges[a].append((c, b))
    edges[b].append((c, a))
INF = float("inf")


def dfs(v, parent, distance):
    dist[v] = distance
    for d, v2 in edges[v]:
        if parent == v2:
            continue
        dfs(v2, v, distance + d)


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

-import heapq +sys.setrecursionlimit(10**7) -def dijkstra(init_v): -    dist = [INF] * n -    dist[init_v] = 0 -    tasks = [(0, init_v)] -    while tasks: -        d, v = heapq.heappop(tasks) -        if dist[v] < d: +def dfs(v, parent, distance): +    dist[v] = distance +    for d, v2 in edges[v]: +        if parent == v2: -        for d2, v2 in edges[v]: -            if dist[v2] <= dist[v] + d2: -                continue -            dist[v2] = dist[v] + d2 -            heapq.heappush(tasks, ((dist[v2], v2))) -    return dist +        dfs(v2, v, distance + d) -dist = dijkstra(k - 1) +dist = [INF] * n +dfs(k - 1, -1, 0)