import queue



N = int(eval(input()))

E = {n: set() for n in range(N)}



for _ in range(N - 1):

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

    a -= 1

    b -= 1

    E[a].add((b, c))

    E[b].add((a, c))



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

K -= 1

dist = [-1] * N

already = set()

que = queue.Queue()

que.put((K, 0))

while not que.empty():

    now, d = que.get()

    already.add(now)

    dist[now] = d

    out = E[now]

    for n, c in out:

        if n in already:

            continue

        que.put((n, d + c))



for _ in range(Q):

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

    x -= 1

    y -= 1

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

import queue

import sys

input = sys.stdin.readline



N = int(eval(input()))

E = {n: set() for n in range(N)}



for _ in range(N - 1):

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

    a -= 1

    b -= 1

    E[a].add((b, c))

    E[b].add((a, c))



def m():

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

    K -= 1

    dist = [-1] * N

    already = set()

    que = queue.Queue()

    dist[K] = 0

    que.put(K)

    while not que.empty():

        now = que.get()

        out = E[now]

        for n, c in out:

            if dist[n] != -1:

                continue

            dist[n] = dist[now] + c        

            que.put(n)

    

    res = ""

    for _ in range(Q):

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

        x -= 1

        y -= 1

        res += str(dist[x] + dist[y]) + "\n"

    

    print(res)



if __name__ == "__main__":

    m()

import queue

N = int(eval(input()))
E = {n: set() for n in range(N)}
for _ in range(N - 1):
    a, b, c = list(map(int, input().split()))
    a -= 1
    b -= 1
    E[a].add((b, c))
    E[b].add((a, c))
Q, K = list(map(int, input().split()))
K -= 1
dist = [-1] * N
already = set()
que = queue.Queue()
que.put((K, 0))
while not que.empty():
    now, d = que.get()
    already.add(now)
    dist[now] = d
    out = E[now]
    for n, c in out:
        if n in already:
            continue
        que.put((n, d + c))
for _ in range(Q):
    x, y = list(map(int, input().split()))
    x -= 1
    y -= 1
    print((dist[x] + dist[y]))

import queue
import sys

input = sys.stdin.readline
N = int(eval(input()))
E = {n: set() for n in range(N)}
for _ in range(N - 1):
    a, b, c = list(map(int, input().split()))
    a -= 1
    b -= 1
    E[a].add((b, c))
    E[b].add((a, c))


def m():
    Q, K = list(map(int, input().split()))
    K -= 1
    dist = [-1] * N
    already = set()
    que = queue.Queue()
    dist[K] = 0
    que.put(K)
    while not que.empty():
        now = que.get()
        out = E[now]
        for n, c in out:
            if dist[n] != -1:
                continue
            dist[n] = dist[now] + c
            que.put(n)
    res = ""
    for _ in range(Q):
        x, y = list(map(int, input().split()))
        x -= 1
        y -= 1
        res += str(dist[x] + dist[y]) + "\n"
    print(res)


if __name__ == "__main__":
    m()

+import sys +input = sys.stdin.readline -Q, K = list(map(int, input().split())) -K -= 1 -dist = [-1] * N -already = set() -que = queue.Queue() -que.put((K, 0)) -while not que.empty(): -    now, d = que.get() -    already.add(now) -    dist[now] = d -    out = E[now] -    for n, c in out: -        if n in already: -            continue -        que.put((n, d + c)) -for _ in range(Q): -    x, y = list(map(int, input().split())) -    x -= 1 -    y -= 1 -    print((dist[x] + dist[y])) + + +def m(): +    Q, K = list(map(int, input().split())) +    K -= 1 +    dist = [-1] * N +    already = set() +    que = queue.Queue() +    dist[K] = 0 +    que.put(K) +    while not que.empty(): +        now = que.get() +        out = E[now] +        for n, c in out: +            if dist[n] != -1: +                continue +            dist[n] = dist[now] + c +            que.put(n) +    res = "" +    for _ in range(Q): +        x, y = list(map(int, input().split())) +        x -= 1 +        y -= 1 +        res += str(dist[x] + dist[y]) + "\n" +    print(res) + + +if __name__ == "__main__": +    m()