n = int(eval(input()))

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



for _ in range(n-1):

    _from, to, dist = list(map(int, input().split()))

    edges[_from-1].append([to-1, dist])

    edges[to-1].append([_from-1, dist])



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

k -= 1



distance_from_k_to = [10**7] * n

distance_from_k_to[k] = 0



todo = []

while edges[k]:

    to, dist = edges[k].pop()

    todo.append([to, dist])



while todo:

    node, dist = todo.pop()

    if distance_from_k_to[node] != 10**7:  # if node has been seen

        continue



    distance_from_k_to[node] = dist

    while edges[node]:

        to, add_dist = edges[node].pop()

        todo.append([to, dist+add_dist])



for _ in range(q):

    start, goal = list(map(int, input().split()))

    start, goal = start-1, goal-1

    ans = distance_from_k_to[start] + distance_from_k_to[goal]

    print(ans)

import sys

input = sys.stdin.readline





n = int(eval(input()))

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



for _ in range(n-1):

    _from, to, dist = list(map(int, input().split()))

    edges[_from-1].append([to-1, dist])

    edges[to-1].append([_from-1, dist])



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

k -= 1



distance_from_k_to = [10**7] * n

distance_from_k_to[k] = 0



todo = []

while edges[k]:

    to, dist = edges[k].pop()

    todo.append([to, dist])



while todo:

    node, dist = todo.pop()

    if distance_from_k_to[node] != 10**7:  # if node has been seen

        continue



    distance_from_k_to[node] = dist

    while edges[node]:

        to, add_dist = edges[node].pop()

        todo.append([to, dist+add_dist])



for _ in range(q):

    start, goal = list(map(int, input().split()))

    start, goal = start-1, goal-1

    ans = distance_from_k_to[start] + distance_from_k_to[goal]

    print(ans)

n = int(eval(input()))
edges = [[] for _ in range(n)]
for _ in range(n - 1):
    _from, to, dist = list(map(int, input().split()))
    edges[_from - 1].append([to - 1, dist])
    edges[to - 1].append([_from - 1, dist])
q, k = list(map(int, input().split()))
k -= 1
distance_from_k_to = [10**7] * n
distance_from_k_to[k] = 0
todo = []
while edges[k]:
    to, dist = edges[k].pop()
    todo.append([to, dist])
while todo:
    node, dist = todo.pop()
    if distance_from_k_to[node] != 10**7:  # if node has been seen
        continue
    distance_from_k_to[node] = dist
    while edges[node]:
        to, add_dist = edges[node].pop()
        todo.append([to, dist + add_dist])
for _ in range(q):
    start, goal = list(map(int, input().split()))
    start, goal = start - 1, goal - 1
    ans = distance_from_k_to[start] + distance_from_k_to[goal]
    print(ans)

import sys

input = sys.stdin.readline
n = int(eval(input()))
edges = [[] for _ in range(n)]
for _ in range(n - 1):
    _from, to, dist = list(map(int, input().split()))
    edges[_from - 1].append([to - 1, dist])
    edges[to - 1].append([_from - 1, dist])
q, k = list(map(int, input().split()))
k -= 1
distance_from_k_to = [10**7] * n
distance_from_k_to[k] = 0
todo = []
while edges[k]:
    to, dist = edges[k].pop()
    todo.append([to, dist])
while todo:
    node, dist = todo.pop()
    if distance_from_k_to[node] != 10**7:  # if node has been seen
        continue
    distance_from_k_to[node] = dist
    while edges[node]:
        to, add_dist = edges[node].pop()
        todo.append([to, dist + add_dist])
for _ in range(q):
    start, goal = list(map(int, input().split()))
    start, goal = start - 1, goal - 1
    ans = distance_from_k_to[start] + distance_from_k_to[goal]
    print(ans)

+import sys + +input = sys.stdin.readline