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 sys

from heapq import heappush, heappop, heapify

input = sys.stdin.readline



def main():

    N = int(eval(input()))

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

    

    for i in range(N-1):

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

        a -= 1; b -= 1

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

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

        

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

    K -= 1

    dist = [0] * N

    visited = [False] * N

    

    q = [(c, v) for c, v in g[K]]

    heapify(q)

    

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

    

    while q:

        c, v = heappop(q)

        

        if visited[v]: continue

        dist[v], visited[v] = c, True

        

        for d, u in g[v]:

            if visited[u]: continue

            heappush(q, (c+d, u))

        

    for i in range(Q):

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

        

        print((dist[x-1]+dist[y-1]))

        

if __name__ == '__main__':

    main()
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 sys
from heapq import heappush, heappop, heapify

input = sys.stdin.readline


def main():
    N = int(eval(input()))
    g = [[] for i in range(N)]
    for i in range(N - 1):
        a, b, c = list(map(int, input().split()))
        a -= 1
        b -= 1
        g[a].append((c, b))
        g[b].append((c, a))
    Q, K = list(map(int, input().split()))
    K -= 1
    dist = [0] * N
    visited = [False] * N
    q = [(c, v) for c, v in g[K]]
    heapify(q)
    dist[K], visited[K] = 0, True
    while q:
        c, v = heappop(q)
        if visited[v]:
            continue
        dist[v], visited[v] = c, True
        for d, u in g[v]:
            if visited[u]:
                continue
            heappush(q, (c + d, u))
    for i in range(Q):
        x, y = list(map(int, input().split()))
        print((dist[x - 1] + dist[y - 1]))


if __name__ == "__main__":
    main()

-import heapq +import sys +from heapq import heappush, heappop, heapify -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])) +input = sys.stdin.readline + + +def main(): +    N = int(eval(input())) +    g = [[] for i in range(N)] +    for i in range(N - 1): +        a, b, c = list(map(int, input().split())) +        a -= 1 +        b -= 1 +        g[a].append((c, b)) +        g[b].append((c, a)) +    Q, K = list(map(int, input().split())) +    K -= 1 +    dist = [0] * N +    visited = [False] * N +    q = [(c, v) for c, v in g[K]] +    heapify(q) +    dist[K], visited[K] = 0, True +    while q: +        c, v = heappop(q) +        if visited[v]: +            continue +        dist[v], visited[v] = c, True +        for d, u in g[v]: +            if visited[u]: +                continue +            heappush(q, (c + d, u)) +    for i in range(Q): +        x, y = list(map(int, input().split())) +        print((dist[x - 1] + dist[y - 1])) + + +if __name__ == "__main__": +    main()