import heapq

N = int(eval(input()))

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

for i in range(N-1):

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

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

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

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



C = [float("inf") for _ in range(N)]

V = [False for _ in range(N)]

C[K-1] = 0

H = [(0, K-1)] 

heapq.heapify(H)

while H:

    cost, node = heapq.heappop(H)

    V[node] = True

    for n in P[node]:

        if V[n[1]] == False:

            if n[0] + cost < C[n[1]]:

                C[n[1]] = n[0] + cost

                heapq.heappush(H, (n[0] + cost, n[1]))

Ans = []

for i in range(Q):

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

    Ans.append(C[x-1]+C[y-1])

for i in range(Q):

    print((Ans[i]))
import sys

from collections import deque



def solve():

    input = sys.stdin.readline

    N = int(eval(input()))

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

    for _ in range(N-1):

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

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

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

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

    q = deque()

    Dist = [-1] * N

    q.append((K-1, K-1, 0))

    while q:

        nowN, preN, nD = q.popleft()

        if Dist[nowN] == -1:

            Dist[nowN] = nD

            for e, add in Edge[nowN]:

                if e != preN: q.append((e, nowN, nD + add))

    Ans = [None] * Q

    for i in range(Q):

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

        Ans[i] = str(Dist[x-1] + Dist[y-1])

    print(("\n".join(Ans)))



    return 0



if __name__ == "__main__":

    solve()
import heapq

N = int(eval(input()))
P = [[] for i in range(N)]
for i in range(N - 1):
    a, b, c = list(map(int, input().split()))
    P[a - 1].append((c, b - 1))
    P[b - 1].append((c, a - 1))
Q, K = list(map(int, input().split()))
C = [float("inf") for _ in range(N)]
V = [False for _ in range(N)]
C[K - 1] = 0
H = [(0, K - 1)]
heapq.heapify(H)
while H:
    cost, node = heapq.heappop(H)
    V[node] = True
    for n in P[node]:
        if V[n[1]] == False:
            if n[0] + cost < C[n[1]]:
                C[n[1]] = n[0] + cost
                heapq.heappush(H, (n[0] + cost, n[1]))
Ans = []
for i in range(Q):
    x, y = list(map(int, input().split()))
    Ans.append(C[x - 1] + C[y - 1])
for i in range(Q):
    print((Ans[i]))

import sys
from collections import deque


def solve():
    input = sys.stdin.readline
    N = int(eval(input()))
    Edge = [[] for _ in range(N)]
    for _ in range(N - 1):
        a, b, c = list(map(int, input().split()))
        Edge[a - 1].append((b - 1, c))
        Edge[b - 1].append((a - 1, c))
    Q, K = list(map(int, input().split()))
    q = deque()
    Dist = [-1] * N
    q.append((K - 1, K - 1, 0))
    while q:
        nowN, preN, nD = q.popleft()
        if Dist[nowN] == -1:
            Dist[nowN] = nD
            for e, add in Edge[nowN]:
                if e != preN:
                    q.append((e, nowN, nD + add))
    Ans = [None] * Q
    for i in range(Q):
        x, y = list(map(int, input().split()))
        Ans[i] = str(Dist[x - 1] + Dist[y - 1])
    print(("\n".join(Ans)))
    return 0


if __name__ == "__main__":
    solve()

-import heapq +import sys +from collections import deque -N = int(eval(input())) -P = [[] for i in range(N)] -for i in range(N - 1): -    a, b, c = list(map(int, input().split())) -    P[a - 1].append((c, b - 1)) -    P[b - 1].append((c, a - 1)) -Q, K = list(map(int, input().split())) -C = [float("inf") for _ in range(N)] -V = [False for _ in range(N)] -C[K - 1] = 0 -H = [(0, K - 1)] -heapq.heapify(H) -while H: -    cost, node = heapq.heappop(H) -    V[node] = True -    for n in P[node]: -        if V[n[1]] == False: -            if n[0] + cost < C[n[1]]: -                C[n[1]] = n[0] + cost -                heapq.heappush(H, (n[0] + cost, n[1])) -Ans = [] -for i in range(Q): -    x, y = list(map(int, input().split())) -    Ans.append(C[x - 1] + C[y - 1]) -for i in range(Q): -    print((Ans[i])) + +def solve(): +    input = sys.stdin.readline +    N = int(eval(input())) +    Edge = [[] for _ in range(N)] +    for _ in range(N - 1): +        a, b, c = list(map(int, input().split())) +        Edge[a - 1].append((b - 1, c)) +        Edge[b - 1].append((a - 1, c)) +    Q, K = list(map(int, input().split())) +    q = deque() +    Dist = [-1] * N +    q.append((K - 1, K - 1, 0)) +    while q: +        nowN, preN, nD = q.popleft() +        if Dist[nowN] == -1: +            Dist[nowN] = nD +            for e, add in Edge[nowN]: +                if e != preN: +                    q.append((e, nowN, nD + add)) +    Ans = [None] * Q +    for i in range(Q): +        x, y = list(map(int, input().split())) +        Ans[i] = str(Dist[x - 1] + Dist[y - 1]) +    print(("\n".join(Ans))) +    return 0 + + +if __name__ == "__main__": +    solve()