import queue

def main():

    n = int(eval(input()))

    a = []

    b = []

    c = []

    lst = [[] for _ in range(n)]



    for i in range(n - 1):

        aa, bb, cc = list(map(int,input().split()))

        lst[aa - 1].append((bb - 1, cc))

        lst[bb - 1].append((aa - 1, cc))



    q, k = list(map(int,input().split()))

    x = []

    y = []

    for i in range(q):

        xx, yy = list(map(int,input().split()))

        x.append(xx - 1)

        y.append(yy - 1)

    qq = queue.Queue()

    qq.put(k - 1)

    dist = [0] * n

    while not qq.empty():

        now = qq.get()

        for to, w in lst[now]:

            if dist[to] != 0:

                continue

            dist[to] = dist[now] + w

            qq.put(to)



    for i in range(q):

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











if __name__ == '__main__':

    main()

from collections import deque

def main():

    n = int(eval(input()))

    a = []

    b = []

    c = []

    lst = [[] for _ in range(n)]



    for i in range(n - 1):

        aa, bb, cc = list(map(int,input().split()))

        lst[aa - 1].append((bb - 1, cc))

        lst[bb - 1].append((aa - 1, cc))



    q, k = list(map(int,input().split()))

    x = []

    y = []

    for i in range(q):

        xx, yy = list(map(int,input().split()))

        x.append(xx - 1)

        y.append(yy - 1)

    qq = deque()

    qq.append(k - 1)

    dist = [0] * n

    while len(qq) != 0:

        now = qq.popleft()

        for to, w in lst[now]:

            if dist[to] != 0:

                continue

            dist[to] = dist[now] + w

            qq.append(to)



    for i in range(q):

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











if __name__ == '__main__':

    main()

import queue


def main():
    n = int(eval(input()))
    a = []
    b = []
    c = []
    lst = [[] for _ in range(n)]
    for i in range(n - 1):
        aa, bb, cc = list(map(int, input().split()))
        lst[aa - 1].append((bb - 1, cc))
        lst[bb - 1].append((aa - 1, cc))
    q, k = list(map(int, input().split()))
    x = []
    y = []
    for i in range(q):
        xx, yy = list(map(int, input().split()))
        x.append(xx - 1)
        y.append(yy - 1)
    qq = queue.Queue()
    qq.put(k - 1)
    dist = [0] * n
    while not qq.empty():
        now = qq.get()
        for to, w in lst[now]:
            if dist[to] != 0:
                continue
            dist[to] = dist[now] + w
            qq.put(to)
    for i in range(q):
        print((dist[x[i]] + dist[y[i]]))


if __name__ == "__main__":
    main()

from collections import deque


def main():
    n = int(eval(input()))
    a = []
    b = []
    c = []
    lst = [[] for _ in range(n)]
    for i in range(n - 1):
        aa, bb, cc = list(map(int, input().split()))
        lst[aa - 1].append((bb - 1, cc))
        lst[bb - 1].append((aa - 1, cc))
    q, k = list(map(int, input().split()))
    x = []
    y = []
    for i in range(q):
        xx, yy = list(map(int, input().split()))
        x.append(xx - 1)
        y.append(yy - 1)
    qq = deque()
    qq.append(k - 1)
    dist = [0] * n
    while len(qq) != 0:
        now = qq.popleft()
        for to, w in lst[now]:
            if dist[to] != 0:
                continue
            dist[to] = dist[now] + w
            qq.append(to)
    for i in range(q):
        print((dist[x[i]] + dist[y[i]]))


if __name__ == "__main__":
    main()

-import queue +from collections import deque -    qq = queue.Queue() -    qq.put(k - 1) +    qq = deque() +    qq.append(k - 1) -    while not qq.empty(): -        now = qq.get() +    while len(qq) != 0: +        now = qq.popleft() -            qq.put(to) +            qq.append(to)