#!/usr/bin/env python3



import sys

import heapq



def main():

    input = sys.stdin.readline

    n = int(eval(input()))

    adjs = [[] for i in range(n)]

    for i in range(n - 1):

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

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

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

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



    # Dijkstra

    dist = [None] * n

    dist[k - 1] = 0

    hq = [(0, k - 1)]

    queued = [False] * n

    queued[k - 1] = True

    while hq:

        d, j = heapq.heappop(hq)

        dist[j] = d

        for j2, d2 in adjs[j]:

            if queued[j2]:

                continue

            heapq.heappush(hq, (d + d2, j2))

            queued[j2] = True



    # For each query

    for i in range(q):

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

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



if __name__ == "__main__":

    main()

#!/usr/bin/env python3



import sys



def main():

    input = sys.stdin.readline

    n = int(eval(input()))

    adjs = [[] for i in range(n)]

    for i in range(n - 1):

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

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

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

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



    # DFS

    dist = [None] * n

    dist[k - 1] = 0

    hq = [(0, k - 1)]

    queued = [False] * n

    queued[k - 1] = True

    while hq:

        d, j = hq.pop()

        dist[j] = d

        for j2, d2 in adjs[j]:

            if queued[j2]:

                continue

            hq.append((d + d2, j2))

            queued[j2] = True



    # For each query

    for i in range(q):

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

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



if __name__ == "__main__":

    main()

#!/usr/bin/env python3
import sys
import heapq


def main():
    input = sys.stdin.readline
    n = int(eval(input()))
    adjs = [[] for i in range(n)]
    for i in range(n - 1):
        a, b, c = list(map(int, input().split()))
        adjs[a - 1].append((b - 1, c))
        adjs[b - 1].append((a - 1, c))
    q, k = list(map(int, input().split()))
    # Dijkstra
    dist = [None] * n
    dist[k - 1] = 0
    hq = [(0, k - 1)]
    queued = [False] * n
    queued[k - 1] = True
    while hq:
        d, j = heapq.heappop(hq)
        dist[j] = d
        for j2, d2 in adjs[j]:
            if queued[j2]:
                continue
            heapq.heappush(hq, (d + d2, j2))
            queued[j2] = True
    # For each query
    for i in range(q):
        x, y = list(map(int, input().split()))
        print((dist[x - 1] + dist[y - 1]))


if __name__ == "__main__":
    main()

#!/usr/bin/env python3
import sys


def main():
    input = sys.stdin.readline
    n = int(eval(input()))
    adjs = [[] for i in range(n)]
    for i in range(n - 1):
        a, b, c = list(map(int, input().split()))
        adjs[a - 1].append((b - 1, c))
        adjs[b - 1].append((a - 1, c))
    q, k = list(map(int, input().split()))
    # DFS
    dist = [None] * n
    dist[k - 1] = 0
    hq = [(0, k - 1)]
    queued = [False] * n
    queued[k - 1] = True
    while hq:
        d, j = hq.pop()
        dist[j] = d
        for j2, d2 in adjs[j]:
            if queued[j2]:
                continue
            hq.append((d + d2, j2))
            queued[j2] = True
    # For each query
    for i in range(q):
        x, y = list(map(int, input().split()))
        print((dist[x - 1] + dist[y - 1]))


if __name__ == "__main__":
    main()

-import heapq -    # Dijkstra +    # DFS -        d, j = heapq.heappop(hq) +        d, j = hq.pop() -            heapq.heappush(hq, (d + d2, j2)) +            hq.append((d + d2, j2))