import heapq

N = int(eval(input()))

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

for i in range(N-1):

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

    a,b = a-1, b-1

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

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

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

K -= 1



INF = float('inf')

ds = [INF for i in range(N)]

ds[K] = 0

visited = [0 for i in range(N)]

q = [K]

heapq.heapify(q)

while q:

    now = heapq.heappop(q)

    visited[now] = 1

    for to,cost in es[now]:

        ds[to] = min(ds[to], ds[now]+cost)

        if visited[to]: continue

        heapq.heappush(q,to)



for i in range(Q):

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

    x,y = x-1, y-1

    print((ds[x] + ds[y]))

import heapq

N = int(eval(input()))

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

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

K -= 1

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



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

for a,b,c in src:

    a,b = a-1,b-1

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

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



INF = float('inf')

dists = [INF] * N

dists[K] = 0

hq = [(0,K)]

heapq.heapify(hq)



while hq:

    dist,v = heapq.heappop(hq)

    dists[v] = dist

    for dd,to in es[v]:

        if dists[to] != INF: continue

        heapq.heappush(hq, (dist+dd, to))



for a,b in qs:

    a,b = a-1,b-1

    print((dists[a] + dists[b]))
import heapq

N = int(eval(input()))
es = [[] for i in range(N)]
for i in range(N - 1):
    a, b, c = list(map(int, input().split()))
    a, b = a - 1, b - 1
    es[a].append((b, c))
    es[b].append((a, c))
Q, K = list(map(int, input().split()))
K -= 1
INF = float("inf")
ds = [INF for i in range(N)]
ds[K] = 0
visited = [0 for i in range(N)]
q = [K]
heapq.heapify(q)
while q:
    now = heapq.heappop(q)
    visited[now] = 1
    for to, cost in es[now]:
        ds[to] = min(ds[to], ds[now] + cost)
        if visited[to]:
            continue
        heapq.heappush(q, to)
for i in range(Q):
    x, y = list(map(int, input().split()))
    x, y = x - 1, y - 1
    print((ds[x] + ds[y]))

import heapq

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

-es = [[] for i in range(N)] -for i in range(N - 1): -    a, b, c = list(map(int, input().split())) -    a, b = a - 1, b - 1 -    es[a].append((b, c)) -    es[b].append((a, c)) +src = [tuple(map(int, input().split())) for i in range(N - 1)] +qs = [tuple(map(int, input().split())) for i in range(Q)] +es = [[] for i in range(N)] +for a, b, c in src: +    a, b = a - 1, b - 1 +    es[a].append((c, b)) +    es[b].append((c, a)) -ds = [INF for i in range(N)] -ds[K] = 0 -visited = [0 for i in range(N)] -q = [K] -heapq.heapify(q) -while q: -    now = heapq.heappop(q) -    visited[now] = 1 -    for to, cost in es[now]: -        ds[to] = min(ds[to], ds[now] + cost) -        if visited[to]: +dists = [INF] * N +dists[K] = 0 +hq = [(0, K)] +heapq.heapify(hq) +while hq: +    dist, v = heapq.heappop(hq) +    dists[v] = dist +    for dd, to in es[v]: +        if dists[to] != INF: -        heapq.heappush(q, to) -for i in range(Q): -    x, y = list(map(int, input().split())) -    x, y = x - 1, y - 1 -    print((ds[x] + ds[y])) +        heapq.heappush(hq, (dist + dd, to)) +for a, b in qs: +    a, b = a - 1, b - 1 +    print((dists[a] + dists[b]))