

from collections import defaultdict





def main():

    num = int(eval(input()))



    link = defaultdict(set)

    weight = defaultdict(int)

    for i in range(num - 1):

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

        link[a].add(b)

        link[b].add(a)

        weight[(min(a, b), max(a, b))] = c



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



    dist_data = [-1 for i in range(num + 1)]

    dist_data[k] = 0

    now_check = set()

    next_check = set()

    now_check.add(k)



    while len(now_check) > 0:

        for ele in now_check:

            now_dist = dist_data[ele]

            for now_ele in link[ele]:

                if dist_data[now_ele] != -1:

                    continue

                next_check.add(now_ele)

                dist_data[now_ele] = now_dist + weight[(min(now_ele, ele), max(now_ele, ele))]

        now_check = next_check

        next_check = set()



    for i in range(q_num):

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

        print((dist_data[a] + dist_data[b]))







if __name__ == '__main__':

    main()





from collections import defaultdict



import sys

input = sys.stdin.readline



def main():

    num = int(eval(input()))



    link = defaultdict(set)

    weight = defaultdict(int)

    for i in range(num - 1):

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

        link[a].add(b)

        link[b].add(a)

        weight[(min(a, b), max(a, b))] = c



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



    dist_data = [-1 for i in range(num + 1)]

    dist_data[k] = 0

    now_check = set()

    next_check = set()

    now_check.add(k)



    while len(now_check) > 0:

        for ele in now_check:

            now_dist = dist_data[ele]

            for now_ele in link[ele]:

                if dist_data[now_ele] != -1:

                    continue

                next_check.add(now_ele)

                dist_data[now_ele] = now_dist + weight[(min(now_ele, ele), max(now_ele, ele))]

        now_check = next_check

        next_check = set()



    for i in range(q_num):

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

        print((dist_data[a] + dist_data[b]))







if __name__ == '__main__':

    main()



from collections import defaultdict


def main():
    num = int(eval(input()))
    link = defaultdict(set)
    weight = defaultdict(int)
    for i in range(num - 1):
        a, b, c = list(map(int, input().split()))
        link[a].add(b)
        link[b].add(a)
        weight[(min(a, b), max(a, b))] = c
    q_num, k = list(map(int, input().split()))
    dist_data = [-1 for i in range(num + 1)]
    dist_data[k] = 0
    now_check = set()
    next_check = set()
    now_check.add(k)
    while len(now_check) > 0:
        for ele in now_check:
            now_dist = dist_data[ele]
            for now_ele in link[ele]:
                if dist_data[now_ele] != -1:
                    continue
                next_check.add(now_ele)
                dist_data[now_ele] = (
                    now_dist + weight[(min(now_ele, ele), max(now_ele, ele))]
                )
        now_check = next_check
        next_check = set()
    for i in range(q_num):
        a, b = list(map(int, input().split()))
        print((dist_data[a] + dist_data[b]))


if __name__ == "__main__":
    main()

from collections import defaultdict
import sys

input = sys.stdin.readline


def main():
    num = int(eval(input()))
    link = defaultdict(set)
    weight = defaultdict(int)
    for i in range(num - 1):
        a, b, c = list(map(int, input().split()))
        link[a].add(b)
        link[b].add(a)
        weight[(min(a, b), max(a, b))] = c
    q_num, k = list(map(int, input().split()))
    dist_data = [-1 for i in range(num + 1)]
    dist_data[k] = 0
    now_check = set()
    next_check = set()
    now_check.add(k)
    while len(now_check) > 0:
        for ele in now_check:
            now_dist = dist_data[ele]
            for now_ele in link[ele]:
                if dist_data[now_ele] != -1:
                    continue
                next_check.add(now_ele)
                dist_data[now_ele] = (
                    now_dist + weight[(min(now_ele, ele), max(now_ele, ele))]
                )
        now_check = next_check
        next_check = set()
    for i in range(q_num):
        a, b = list(map(int, input().split()))
        print((dist_data[a] + dist_data[b]))


if __name__ == "__main__":
    main()

+import sys + +input = sys.stdin.readline