from heapq import heappush,heappop

N = int(eval(input()))

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

for i in range(N-1):

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

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

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

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

Que=[[int(i) for i in input().split()] for i in range(Q)]

inf=10**17

dis=[inf]*N

dis[K-1]=0

kei=[]

heappush(kei,[0,K-1])

while kei:

    q,x=heappop(kei)

    if dis[x]<q:

        continue

    for i,d in table[x]:

        if dis[x]+d<dis[i]:

            dis[i]=dis[x]+d

            heappush(kei,[dis[i],i])

ans =[]

for x,y in Que:

    ans.append(dis[x-1]+dis[y-1])

print(('\n'.join(map(str,ans))))
import sys

input = sys.stdin.readline

from heapq import heappop,heappush

N=int(eval(input()))

table=[[] 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

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

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

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

query=[[int(i)-1 for i in input().split()] for i in range(Q)]

K=K-1

H=[]

visit=[10**18]*(N)

visit[K]=0

heappush(H,(0,K))

while H:

    co,pt=heappop(H)

    if co>visit[pt]:

        continue

    for x,c in table[pt]:

        if visit[x]>visit[pt]+c:

            visit[x]=visit[pt]+c

            heappush(H,(visit[x],x))

for a,b in query:

    print((visit[a]+visit[b]))
from heapq import heappush, heappop

N = int(eval(input()))
table = [[] for i in range(N)]
for i in range(N - 1):
    a, b, c = list(map(int, input().split()))
    table[a - 1].append([b - 1, c])
    table[b - 1].append([a - 1, c])
Q, K = list(map(int, input().split()))
Que = [[int(i) for i in input().split()] for i in range(Q)]
inf = 10**17
dis = [inf] * N
dis[K - 1] = 0
kei = []
heappush(kei, [0, K - 1])
while kei:
    q, x = heappop(kei)
    if dis[x] < q:
        continue
    for i, d in table[x]:
        if dis[x] + d < dis[i]:
            dis[i] = dis[x] + d
            heappush(kei, [dis[i], i])
ans = []
for x, y in Que:
    ans.append(dis[x - 1] + dis[y - 1])
print(("\n".join(map(str, ans))))

import sys

input = sys.stdin.readline
from heapq import heappop, heappush

N = int(eval(input()))
table = [[] 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
    table[a].append((b, c))
    table[b].append((a, c))
Q, K = list(map(int, input().split()))
query = [[int(i) - 1 for i in input().split()] for i in range(Q)]
K = K - 1
H = []
visit = [10**18] * (N)
visit[K] = 0
heappush(H, (0, K))
while H:
    co, pt = heappop(H)
    if co > visit[pt]:
        continue
    for x, c in table[pt]:
        if visit[x] > visit[pt] + c:
            visit[x] = visit[pt] + c
            heappush(H, (visit[x], x))
for a, b in query:
    print((visit[a] + visit[b]))

-from heapq import heappush, heappop +import sys + +input = sys.stdin.readline +from heapq import heappop, heappush -    table[a - 1].append([b - 1, c]) -    table[b - 1].append([a - 1, c]) +    a, b = a - 1, b - 1 +    table[a].append((b, c)) +    table[b].append((a, c)) -Que = [[int(i) for i in input().split()] for i in range(Q)] -inf = 10**17 -dis = [inf] * N -dis[K - 1] = 0 -kei = [] -heappush(kei, [0, K - 1]) -while kei: -    q, x = heappop(kei) -    if dis[x] < q: +query = [[int(i) - 1 for i in input().split()] for i in range(Q)] +K = K - 1 +H = [] +visit = [10**18] * (N) +visit[K] = 0 +heappush(H, (0, K)) +while H: +    co, pt = heappop(H) +    if co > visit[pt]: -    for i, d in table[x]: -        if dis[x] + d < dis[i]: -            dis[i] = dis[x] + d -            heappush(kei, [dis[i], i]) -ans = [] -for x, y in Que: -    ans.append(dis[x - 1] + dis[y - 1]) -print(("\n".join(map(str, ans)))) +    for x, c in table[pt]: +        if visit[x] > visit[pt] + c: +            visit[x] = visit[pt] + c +            heappush(H, (visit[x], x)) +for a, b in query: +    print((visit[a] + visit[b]))