import heapq



N = int(eval(input()))

abc = [list(map(int,input().split())) for _ in range(N-1)]

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

xy = [list(map(int,input().split())) for _ in range(Q)]



connect = [[] for _ in range(N)]

INFTY = 10**15

for a,b,c in abc:

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

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



cost = [[INFTY,False] for _  in range(N)]

cost[K-1][0] = 0

q = [(0,K-1)]

while q:

    _,i = heapq.heappop(q)

    if cost[i][1]:

        continue

    cost[i][1] = True

    ci0 = cost[i][0]

    for j,c in connect[i]:

        if cost[j][1]:

            continue

        tmp_cost = ci0+c

        if tmp_cost < cost[j][0]:

            cost[j][0] = tmp_cost

            heapq.heappush(q,(tmp_cost,j))



for x,y in xy:

    print((cost[x-1][0] + cost[y-1][0]))
N = int(eval(input()))

ABC = [list(map(int,input().split())) for _ in [0]*(N-1)]

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

XY = [list(map(int,input().split())) for _ in [0]*Q]



E = [{} for _ in [0]*N]

for a,b,c in ABC:

    E[a-1][b-1] = c

    E[b-1][a-1] = c



def dist_dfs_tree(N,E,start):

    d = [-1]*N

    d[start] = 0

    q = [start]

    while q:

        i = q.pop()

        ci = d[i]

        for j,cj in list(E[i].items()):

            if d[j] !=-1:continue

            d[j] = ci+cj

            q.append(j)

    return d



d = dist_dfs_tree(N,E,K-1)

for x,y in XY:

    print((d[x-1]+d[y-1]))
import heapq

N = int(eval(input()))
abc = [list(map(int, input().split())) for _ in range(N - 1)]
Q, K = list(map(int, input().split()))
xy = [list(map(int, input().split())) for _ in range(Q)]
connect = [[] for _ in range(N)]
INFTY = 10**15
for a, b, c in abc:
    connect[a - 1].append([b - 1, c])
    connect[b - 1].append([a - 1, c])
cost = [[INFTY, False] for _ in range(N)]
cost[K - 1][0] = 0
q = [(0, K - 1)]
while q:
    _, i = heapq.heappop(q)
    if cost[i][1]:
        continue
    cost[i][1] = True
    ci0 = cost[i][0]
    for j, c in connect[i]:
        if cost[j][1]:
            continue
        tmp_cost = ci0 + c
        if tmp_cost < cost[j][0]:
            cost[j][0] = tmp_cost
            heapq.heappush(q, (tmp_cost, j))
for x, y in xy:
    print((cost[x - 1][0] + cost[y - 1][0]))

N = int(eval(input()))
ABC = [list(map(int, input().split())) for _ in [0] * (N - 1)]
Q, K = list(map(int, input().split()))
XY = [list(map(int, input().split())) for _ in [0] * Q]
E = [{} for _ in [0] * N]
for a, b, c in ABC:
    E[a - 1][b - 1] = c
    E[b - 1][a - 1] = c


def dist_dfs_tree(N, E, start):
    d = [-1] * N
    d[start] = 0
    q = [start]
    while q:
        i = q.pop()
        ci = d[i]
        for j, cj in list(E[i].items()):
            if d[j] != -1:
                continue
            d[j] = ci + cj
            q.append(j)
    return d


d = dist_dfs_tree(N, E, K - 1)
for x, y in XY:
    print((d[x - 1] + d[y - 1]))

-import heapq +N = int(eval(input())) +ABC = [list(map(int, input().split())) for _ in [0] * (N - 1)] +Q, K = list(map(int, input().split())) +XY = [list(map(int, input().split())) for _ in [0] * Q] +E = [{} for _ in [0] * N] +for a, b, c in ABC: +    E[a - 1][b - 1] = c +    E[b - 1][a - 1] = c -N = int(eval(input())) -abc = [list(map(int, input().split())) for _ in range(N - 1)] -Q, K = list(map(int, input().split())) -xy = [list(map(int, input().split())) for _ in range(Q)] -connect = [[] for _ in range(N)] -INFTY = 10**15 -for a, b, c in abc: -    connect[a - 1].append([b - 1, c]) -    connect[b - 1].append([a - 1, c]) -cost = [[INFTY, False] for _ in range(N)] -cost[K - 1][0] = 0 -q = [(0, K - 1)] -while q: -    _, i = heapq.heappop(q) -    if cost[i][1]: -        continue -    cost[i][1] = True -    ci0 = cost[i][0] -    for j, c in connect[i]: -        if cost[j][1]: -            continue -        tmp_cost = ci0 + c -        if tmp_cost < cost[j][0]: -            cost[j][0] = tmp_cost -            heapq.heappush(q, (tmp_cost, j)) -for x, y in xy: -    print((cost[x - 1][0] + cost[y - 1][0])) + +def dist_dfs_tree(N, E, start): +    d = [-1] * N +    d[start] = 0 +    q = [start] +    while q: +        i = q.pop() +        ci = d[i] +        for j, cj in list(E[i].items()): +            if d[j] != -1: +                continue +            d[j] = ci + cj +            q.append(j) +    return d + + +d = dist_dfs_tree(N, E, K - 1) +for x, y in XY: +    print((d[x - 1] + d[y - 1]))