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 sys

from collections import deque



input = sys.stdin.readline



def main():    

    for i in range(N-1):

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

        a -= 1; b -= 1

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

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

        

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

    K -= 1

        

    q = deque([(K, 0, -1)])



    while q:

        v, c, p = q.popleft()

        

        dist[v] = c

        for u, d in g[v]:

            if u == p: continue

            q.append((u, c+d, v))

    

    for i in range(Q):

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

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

 

if __name__ == '__main__':

    N = int(eval(input()))

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

    dist = [0] * N

    main()
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 sys
from collections import deque

input = sys.stdin.readline


def main():
    for i in range(N - 1):
        a, b, c = list(map(int, input().split()))
        a -= 1
        b -= 1
        g[a].append((b, c))
        g[b].append((a, c))
    Q, K = list(map(int, input().split()))
    K -= 1
    q = deque([(K, 0, -1)])
    while q:
        v, c, p = q.popleft()
        dist[v] = c
        for u, d in g[v]:
            if u == p:
                continue
            q.append((u, c + d, v))
    for i in range(Q):
        x, y = list(map(int, input().split()))
        print((dist[x - 1] + dist[y - 1]))


if __name__ == "__main__":
    N = int(eval(input()))
    g = [[] for i in range(N)]
    dist = [0] * N
    main()

-from heapq import heappush, heappop, heapify +from collections import deque -    N = int(eval(input())) -    g = [[] for i in range(N)] -        g[a].append((c, b)) -        g[b].append((c, a)) +        g[a].append((b, c)) +        g[b].append((a, c)) -    dist = [0] * N -    visited = [False] * N -    q = [(c, v) for c, v in g[K]] -    heapify(q) -    dist[K], visited[K] = 0, True +    q = deque([(K, 0, -1)]) -        c, v = heappop(q) -        if visited[v]: -            continue -        dist[v], visited[v] = c, True -        for d, u in g[v]: -            if visited[u]: +        v, c, p = q.popleft() +        dist[v] = c +        for u, d in g[v]: +            if u == p: -            heappush(q, (c + d, u)) +            q.append((u, c + d, v)) +    N = int(eval(input())) +    g = [[] for i in range(N)] +    dist = [0] * N