import heapq





N = int(eval(input()))

tree = {i+1:[] for i in range(N)}

route = [10**20 for i in range(N+1)]

visited = [False for i in range(N+1)]



for i in range(N-1):

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

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

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



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



route[K], visited[K] = 0, True

queue = tree[K]



while len(queue):

    c, r = heapq.heappop(queue)



    if not visited[r]:

         visited[r], route[r] = True, c

         for cost, next_r in tree[r]:

             if not visited[next_r]:

                 heapq.heappush(queue, (route[r]+cost, next_r))



for i in range(Q):

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

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

import heapq



N = int(eval(input()))

graph = [[] for i in range(N+1)]

visited = [False] * (N+1)

cost = [10**20 for i in range(N+1)]



for i in range(N-1):

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

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

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

    

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

q = []



for cr in graph[K]:

    heapq.heappush(q, cr)

visited[K] = True

cost[K] = 0



while q:

    c, r = heapq.heappop(q)

    

    if not visited[r]:

        visited[r] = True

        cost[r] = c

        

        for c_n, r_n in graph[r]:

            if not visited[r_n]:

                heapq.heappush(q, (c+c_n, r_n))

                

for i in range(Q):

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

    print((cost[x] + cost[y]))
import heapq

N = int(eval(input()))
tree = {i + 1: [] for i in range(N)}
route = [10**20 for i in range(N + 1)]
visited = [False for i in range(N + 1)]
for i in range(N - 1):
    a, b, c = list(map(int, input().split()))
    tree[a].append((c, b))
    tree[b].append((c, a))
Q, K = list(map(int, input().split()))
route[K], visited[K] = 0, True
queue = tree[K]
while len(queue):
    c, r = heapq.heappop(queue)
    if not visited[r]:
        visited[r], route[r] = True, c
        for cost, next_r in tree[r]:
            if not visited[next_r]:
                heapq.heappush(queue, (route[r] + cost, next_r))
for i in range(Q):
    x, y = list(map(int, input().split()))
    print((route[x] + route[y]))

import heapq

N = int(eval(input()))
graph = [[] for i in range(N + 1)]
visited = [False] * (N + 1)
cost = [10**20 for i in range(N + 1)]
for i in range(N - 1):
    a, b, c = list(map(int, input().split()))
    graph[a].append((c, b))
    graph[b].append((c, a))
Q, K = list(map(int, input().split()))
q = []
for cr in graph[K]:
    heapq.heappush(q, cr)
visited[K] = True
cost[K] = 0
while q:
    c, r = heapq.heappop(q)
    if not visited[r]:
        visited[r] = True
        cost[r] = c
        for c_n, r_n in graph[r]:
            if not visited[r_n]:
                heapq.heappush(q, (c + c_n, r_n))
for i in range(Q):
    x, y = list(map(int, input().split()))
    print((cost[x] + cost[y]))

-tree = {i + 1: [] for i in range(N)} -route = [10**20 for i in range(N + 1)] -visited = [False for i in range(N + 1)] +graph = [[] for i in range(N + 1)] +visited = [False] * (N + 1) +cost = [10**20 for i in range(N + 1)] -    tree[a].append((c, b)) -    tree[b].append((c, a)) +    graph[a].append((c, b)) +    graph[b].append((c, a)) -route[K], visited[K] = 0, True -queue = tree[K] -while len(queue): -    c, r = heapq.heappop(queue) +q = [] +for cr in graph[K]: +    heapq.heappush(q, cr) +visited[K] = True +cost[K] = 0 +while q: +    c, r = heapq.heappop(q) -        visited[r], route[r] = True, c -        for cost, next_r in tree[r]: -            if not visited[next_r]: -                heapq.heappush(queue, (route[r] + cost, next_r)) +        visited[r] = True +        cost[r] = c +        for c_n, r_n in graph[r]: +            if not visited[r_n]: +                heapq.heappush(q, (c + c_n, r_n)) -    print((route[x] + route[y])) +    print((cost[x] + cost[y]))