import heapq

N = int(eval(input()))

src = [tuple(map(int,input().split())) for i in range(N-1)]

es = [[] for i in range(N)]

for a,b,c in src:

    es[a-1].append((b-1,c))

    es[b-1].append((a-1,c))



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

qs = [tuple(map(int,input().split())) for i in range(Q)]

K -= 1



INF = float('inf')

dists = [INF]*N

dists[K] = 0

hq = [(0,K)]

heapq.heapify(hq)



while hq:

    d,v = heapq.heappop(hq)

    for to,c in es[v]:

        if d+c >= dists[to]: continue

        dists[to] = d+c

        heapq.heappush(hq, (d+c, to))



for a,b in qs:

    print((dists[a-1] + dists[b-1]))
N = int(input())

ABC = [tuple(map(int,input().split())) for i in range(N-1)]

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

K -= 1

XY = [tuple(map(lambda x:int(x)-1,input().split())) for i in range(Q)]

es = [[] for i in range(N)]

for a,b,c in ABC:

    a,b = a-1,b-1

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

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

INF = float('inf')

dist = [INF] * N

dist[K] = 0

import heapq

hq = [(0,K)]

heapq.heapify(hq)

while hq:

    c,v = heapq.heappop(hq)

    for to,d in es[v]:

        if c+d >= dist[to]: continue

        dist[to] = c+d

        heapq.heappush(hq,(c+d,to))

ans = []

for x,y in XY:

    ans.append(dist[x]+dist[y])

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

import heapq

N = int(eval(input()))
src = [tuple(map(int, input().split())) for i in range(N - 1)]
es = [[] for i in range(N)]
for a, b, c in src:
    es[a - 1].append((b - 1, c))
    es[b - 1].append((a - 1, c))
Q, K = list(map(int, input().split()))
qs = [tuple(map(int, input().split())) for i in range(Q)]
K -= 1
INF = float("inf")
dists = [INF] * N
dists[K] = 0
hq = [(0, K)]
heapq.heapify(hq)
while hq:
    d, v = heapq.heappop(hq)
    for to, c in es[v]:
        if d + c >= dists[to]:
            continue
        dists[to] = d + c
        heapq.heappush(hq, (d + c, to))
for a, b in qs:
    print((dists[a - 1] + dists[b - 1]))

N = int(input())
ABC = [tuple(map(int, input().split())) for i in range(N - 1)]
Q, K = map(int, input().split())
K -= 1
XY = [tuple(map(lambda x: int(x) - 1, input().split())) for i in range(Q)]
es = [[] for i in range(N)]
for a, b, c in ABC:
    a, b = a - 1, b - 1
    es[a].append((b, c))
    es[b].append((a, c))
INF = float("inf")
dist = [INF] * N
dist[K] = 0
import heapq

hq = [(0, K)]
heapq.heapify(hq)
while hq:
    c, v = heapq.heappop(hq)
    for to, d in es[v]:
        if c + d >= dist[to]:
            continue
        dist[to] = c + d
        heapq.heappush(hq, (c + d, to))
ans = []
for x, y in XY:
    ans.append(dist[x] + dist[y])
print(*ans, sep="\n")

+N = int(input()) +ABC = [tuple(map(int, input().split())) for i in range(N - 1)] +Q, K = map(int, input().split()) +K -= 1 +XY = [tuple(map(lambda x: int(x) - 1, input().split())) for i in range(Q)] +es = [[] for i in range(N)] +for a, b, c in ABC: +    a, b = a - 1, b - 1 +    es[a].append((b, c)) +    es[b].append((a, c)) +INF = float("inf") +dist = [INF] * N +dist[K] = 0 -N = int(eval(input())) -src = [tuple(map(int, input().split())) for i in range(N - 1)] -es = [[] for i in range(N)] -for a, b, c in src: -    es[a - 1].append((b - 1, c)) -    es[b - 1].append((a - 1, c)) -Q, K = list(map(int, input().split())) -qs = [tuple(map(int, input().split())) for i in range(Q)] -K -= 1 -INF = float("inf") -dists = [INF] * N -dists[K] = 0 -    d, v = heapq.heappop(hq) -    for to, c in es[v]: -        if d + c >= dists[to]: +    c, v = heapq.heappop(hq) +    for to, d in es[v]: +        if c + d >= dist[to]: -        dists[to] = d + c -        heapq.heappush(hq, (d + c, to)) -for a, b in qs: -    print((dists[a - 1] + dists[b - 1])) +        dist[to] = c + d +        heapq.heappush(hq, (c + d, to)) +ans = [] +for x, y in XY: +    ans.append(dist[x] + dist[y]) +print(*ans, sep="\n")