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()

mod = 1000000007

eps = 10**-9





def main():

    import sys

    from collections import deque

    input = sys.stdin.buffer.readline



    N = int(eval(input()))

    adj = [[] for _ in range(N+1)]

    for _ in range(N-1):

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

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

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

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



    que = deque()

    que.append(root)

    seen = [-1] * (N+1)

    seen[root] = 0

    par = [0] * (N+1)

    child = [[] for _ in range(N+1)]

    seq = []

    while que:

        v = que.popleft()

        seq.append(v)

        for u, c in adj[v]:

            if seen[u] == -1:

                seen[u] = seen[v] + c

                par[u] = v

                child[v].append(u)

                que.append(u)

    seq.reverse()



    for _ in range(Q):

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

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





if __name__ == '__main__':

    main()

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()

mod = 1000000007
eps = 10**-9


def main():
    import sys
    from collections import deque

    input = sys.stdin.buffer.readline
    N = int(eval(input()))
    adj = [[] for _ in range(N + 1)]
    for _ in range(N - 1):
        a, b, c = list(map(int, input().split()))
        adj[a].append((b, c))
        adj[b].append((a, c))
    Q, root = list(map(int, input().split()))
    que = deque()
    que.append(root)
    seen = [-1] * (N + 1)
    seen[root] = 0
    par = [0] * (N + 1)
    child = [[] for _ in range(N + 1)]
    seq = []
    while que:
        v = que.popleft()
        seq.append(v)
        for u, c in adj[v]:
            if seen[u] == -1:
                seen[u] = seen[v] + c
                par[u] = v
                child[v].append(u)
                que.append(u)
    seq.reverse()
    for _ in range(Q):
        x, y = list(map(int, input().split()))
        print((seen[x] + seen[y]))


if __name__ == "__main__":
    main()

-import sys -from collections import deque +mod = 1000000007 +eps = 10**-9 -def solve(): -    input = sys.stdin.readline +def main(): +    import sys +    from collections import deque + +    input = sys.stdin.buffer.readline -    Edge = [[] for _ in range(N)] +    adj = [[] for _ in range(N + 1)] -        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): +        adj[a].append((b, c)) +        adj[b].append((a, c)) +    Q, root = list(map(int, input().split())) +    que = deque() +    que.append(root) +    seen = [-1] * (N + 1) +    seen[root] = 0 +    par = [0] * (N + 1) +    child = [[] for _ in range(N + 1)] +    seq = [] +    while que: +        v = que.popleft() +        seq.append(v) +        for u, c in adj[v]: +            if seen[u] == -1: +                seen[u] = seen[v] + c +                par[u] = v +                child[v].append(u) +                que.append(u) +    seq.reverse() +    for _ in range(Q): -        Ans[i] = str(Dist[x - 1] + Dist[y - 1]) -    print(("\n".join(Ans))) -    return 0 +        print((seen[x] + seen[y])) -    solve() +    main()