# ダイクストラ法

from heapq import heappop, heappush



N = int(input())

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

for _ in range(N - 1):

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

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

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



Q, K = map(int, input().split())



d = [float('inf')] * (N + 1)

d[K] = 0

#prev = [None] * (N + 1)

q = [(0, K)]

while q:

    _, u = heappop(q)

    for v, c in links[u]:

        alt = d[u] + c

        if d[v] > alt:

            d[v] = alt

            #prev[v] = u

            heappush(q, (alt, v))



result = []

for _ in range(Q):

    x, y = map(int, input().split())

    result.append(d[x] + d[y])

print(*result, sep='\n')

#print('\n'.join(str(v) for v in result))

# 幅優先探索

from collections import deque



N = int(eval(input()))

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

for _ in range(N - 1):

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

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

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



Q, K = list(map(int, input().split()))



d = [float('inf')] * (N + 1)

d[K] = 0

q = deque([K])

while q:

    i = q.popleft()

    for j, c in links[i]:

        if d[i] + c < d[j]:

            d[j] = d[i] + c

            q.append(j)



result = []

for _ in range(Q):

    x, y = list(map(int, input().split()))

    result.append(d[x] + d[y])

print(('\n'.join(str(v) for v in result)))

#print(*result, sep='\n')

# ダイクストラ法
from heapq import heappop, heappush

N = int(input())
links = [[] for _ in range(N + 1)]
for _ in range(N - 1):
    a, b, c = map(int, input().split())
    links[a].append((b, c))
    links[b].append((a, c))
Q, K = map(int, input().split())
d = [float("inf")] * (N + 1)
d[K] = 0
# prev = [None] * (N + 1)
q = [(0, K)]
while q:
    _, u = heappop(q)
    for v, c in links[u]:
        alt = d[u] + c
        if d[v] > alt:
            d[v] = alt
            # prev[v] = u
            heappush(q, (alt, v))
result = []
for _ in range(Q):
    x, y = map(int, input().split())
    result.append(d[x] + d[y])
print(*result, sep="\n")
# print('\n'.join(str(v) for v in result))

# 幅優先探索
from collections import deque

N = int(eval(input()))
links = [[] for _ in range(N + 1)]
for _ in range(N - 1):
    a, b, c = list(map(int, input().split()))
    links[a].append((b, c))
    links[b].append((a, c))
Q, K = list(map(int, input().split()))
d = [float("inf")] * (N + 1)
d[K] = 0
q = deque([K])
while q:
    i = q.popleft()
    for j, c in links[i]:
        if d[i] + c < d[j]:
            d[j] = d[i] + c
            q.append(j)
result = []
for _ in range(Q):
    x, y = list(map(int, input().split()))
    result.append(d[x] + d[y])
print(("\n".join(str(v) for v in result)))
# print(*result, sep='\n')

-# ダイクストラ法 -from heapq import heappop, heappush +# 幅優先探索 +from collections import deque -N = int(input()) +N = int(eval(input())) -    a, b, c = map(int, input().split()) +    a, b, c = list(map(int, input().split())) -Q, K = map(int, input().split()) +Q, K = list(map(int, input().split())) -# prev = [None] * (N + 1) -q = [(0, K)] +q = deque([K]) -    _, u = heappop(q) -    for v, c in links[u]: -        alt = d[u] + c -        if d[v] > alt: -            d[v] = alt -            # prev[v] = u -            heappush(q, (alt, v)) +    i = q.popleft() +    for j, c in links[i]: +        if d[i] + c < d[j]: +            d[j] = d[i] + c +            q.append(j) -    x, y = map(int, input().split()) +    x, y = list(map(int, input().split())) -print(*result, sep="\n") -# print('\n'.join(str(v) for v in result)) +print(("\n".join(str(v) for v in result))) +# print(*result, sep='\n')