import sys

input = sys.stdin.readline



def main():

    N = int(eval(input()))

    X = [list(map(int,input().split())) for _ in range(N-1)]

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

    Y = [list(map(int,input().split())) for _ in range(Q)]



    tree = [[] for _ in range(N)]

    dif = [10**15 for _ in range(N)]

    dif[K-1] = 0

    for i in range(N-1):

        tree[X[i][0]-1].append((X[i][1]-1,X[i][2]))

        tree[X[i][1]-1].append((X[i][0]-1,X[i][2]))



    queue = [K-1]

    while queue:

        a = queue.pop()

        for i in tree[a]:

            s,t = i[0],i[1]

            if dif[s] == 10**15:

                dif[s] = dif[a]+t

                queue.append(s)



    for i in range(Q):

        print((dif[Y[i][0]-1]+dif[Y[i][1]-1]))



if __name__ == "__main__":

    main()
import sys

input = sys.stdin.buffer.readline

import heapq



def main():

	N = int(eval(input()))

	edge = [[] for _ in range(N)]

	for _ in range(N-1):

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

		edge[a-1].append((b-1,d))

		edge[b-1].append((a-1,d))

	

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

	dist = [-1]*N

	dist[(K-1)] = 0

	q = [(0,K-1)]

	heapq.heapify(q)

	while q:

		_,now = heapq.heappop(q)

		for fol,d in edge[now]:

			if dist[fol] == -1:

				dist[fol] = dist[now]+d

				heapq.heappush(q,(dist[fol],fol))

	

	for _ in range(Q):

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

		print((dist[a-1]+dist[b-1]))

	

if __name__ == "__main__":

	main()

import sys

input = sys.stdin.readline


def main():
    N = int(eval(input()))
    X = [list(map(int, input().split())) for _ in range(N - 1)]
    Q, K = list(map(int, input().split()))
    Y = [list(map(int, input().split())) for _ in range(Q)]
    tree = [[] for _ in range(N)]
    dif = [10**15 for _ in range(N)]
    dif[K - 1] = 0
    for i in range(N - 1):
        tree[X[i][0] - 1].append((X[i][1] - 1, X[i][2]))
        tree[X[i][1] - 1].append((X[i][0] - 1, X[i][2]))
    queue = [K - 1]
    while queue:
        a = queue.pop()
        for i in tree[a]:
            s, t = i[0], i[1]
            if dif[s] == 10**15:
                dif[s] = dif[a] + t
                queue.append(s)
    for i in range(Q):
        print((dif[Y[i][0] - 1] + dif[Y[i][1] - 1]))


if __name__ == "__main__":
    main()

import sys

input = sys.stdin.buffer.readline
import heapq


def main():
    N = int(eval(input()))
    edge = [[] for _ in range(N)]
    for _ in range(N - 1):
        a, b, d = list(map(int, input().split()))
        edge[a - 1].append((b - 1, d))
        edge[b - 1].append((a - 1, d))
    Q, K = list(map(int, input().split()))
    dist = [-1] * N
    dist[(K - 1)] = 0
    q = [(0, K - 1)]
    heapq.heapify(q)
    while q:
        _, now = heapq.heappop(q)
        for fol, d in edge[now]:
            if dist[fol] == -1:
                dist[fol] = dist[now] + d
                heapq.heappush(q, (dist[fol], fol))
    for _ in range(Q):
        a, b = list(map(int, input().split()))
        print((dist[a - 1] + dist[b - 1]))


if __name__ == "__main__":
    main()

-input = sys.stdin.readline +input = sys.stdin.buffer.readline +import heapq -    X = [list(map(int, input().split())) for _ in range(N - 1)] +    edge = [[] for _ in range(N)] +    for _ in range(N - 1): +        a, b, d = list(map(int, input().split())) +        edge[a - 1].append((b - 1, d)) +        edge[b - 1].append((a - 1, d)) -    Y = [list(map(int, input().split())) for _ in range(Q)] -    tree = [[] for _ in range(N)] -    dif = [10**15 for _ in range(N)] -    dif[K - 1] = 0 -    for i in range(N - 1): -        tree[X[i][0] - 1].append((X[i][1] - 1, X[i][2])) -        tree[X[i][1] - 1].append((X[i][0] - 1, X[i][2])) -    queue = [K - 1] -    while queue: -        a = queue.pop() -        for i in tree[a]: -            s, t = i[0], i[1] -            if dif[s] == 10**15: -                dif[s] = dif[a] + t -                queue.append(s) -    for i in range(Q): -        print((dif[Y[i][0] - 1] + dif[Y[i][1] - 1])) +    dist = [-1] * N +    dist[(K - 1)] = 0 +    q = [(0, K - 1)] +    heapq.heapify(q) +    while q: +        _, now = heapq.heappop(q) +        for fol, d in edge[now]: +            if dist[fol] == -1: +                dist[fol] = dist[now] + d +                heapq.heappush(q, (dist[fol], fol)) +    for _ in range(Q): +        a, b = list(map(int, input().split())) +        print((dist[a - 1] + dist[b - 1]))