import sys

sys.setrecursionlimit(10**6)

def dfs(v,p,d):

    for i, j in tree[v]:

        if i != p:

            depth[i] = d + j

            dfs(i,v,d+j)



N = int(eval(input()))



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



for i in range(N-1):

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

    a -= 1

    b -= 1

    tree[a].append((b,c))

    tree[b].append((a,c))



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



depth = [0 for i in range(N)]

depth[K-1] = 0



dfs(K-1,-1,0)



for _ in range(Q):

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

    print((depth[x-1] + depth[y-1]))
import heapq

import sys



input = sys.stdin.readline



def dijkstra(s, edge, N):

    d = [float('inf')] * N

    used = [True] * N

    d[s] = 0

    used[s] = False

    edgelist = []

    for a, b in edge[s]:

        heapq.heappush(edgelist, a * (10 ** 6) + b)

    while len(edgelist):

        minedge = heapq.heappop(edgelist)



        if not used[minedge%(10 ** 6)]:

            continue

        v = minedge % (10 ** 6)

        d[v] = minedge // (10 ** 6)

        used[v] = False

        for e in edge[v]:

            if used[e[1]]:

                heapq.heappush(edgelist, (e[0] + d[v]) * (10 ** 6) + e[1])

    return d



def main():

    N = int(eval(input()))

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

    for _ in range(N-1):

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

        a -= 1

        b -= 1

        edge[a].append([c,b])

        edge[b].append([c,a])

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

    K -= 1

    d = dijkstra(K, edge, N)

    for _ in range(Q):

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

        x -= 1

        y -= 1

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







if __name__ == "__main__":

    main()
import sys

sys.setrecursionlimit(10**6)


def dfs(v, p, d):
    for i, j in tree[v]:
        if i != p:
            depth[i] = d + j
            dfs(i, v, d + j)


N = int(eval(input()))
tree = [[] for _ in range(N)]
for i in range(N - 1):
    a, b, c = list(map(int, input().split()))
    a -= 1
    b -= 1
    tree[a].append((b, c))
    tree[b].append((a, c))
Q, K = list(map(int, input().split()))
depth = [0 for i in range(N)]
depth[K - 1] = 0
dfs(K - 1, -1, 0)
for _ in range(Q):
    x, y = list(map(int, input().split()))
    print((depth[x - 1] + depth[y - 1]))

import heapq
import sys

input = sys.stdin.readline


def dijkstra(s, edge, N):
    d = [float("inf")] * N
    used = [True] * N
    d[s] = 0
    used[s] = False
    edgelist = []
    for a, b in edge[s]:
        heapq.heappush(edgelist, a * (10**6) + b)
    while len(edgelist):
        minedge = heapq.heappop(edgelist)
        if not used[minedge % (10**6)]:
            continue
        v = minedge % (10**6)
        d[v] = minedge // (10**6)
        used[v] = False
        for e in edge[v]:
            if used[e[1]]:
                heapq.heappush(edgelist, (e[0] + d[v]) * (10**6) + e[1])
    return d


def main():
    N = int(eval(input()))
    edge = [[] for _ in range(N)]
    for _ in range(N - 1):
        a, b, c = list(map(int, input().split()))
        a -= 1
        b -= 1
        edge[a].append([c, b])
        edge[b].append([c, a])
    Q, K = list(map(int, input().split()))
    K -= 1
    d = dijkstra(K, edge, N)
    for _ in range(Q):
        x, y = list(map(int, input().split()))
        x -= 1
        y -= 1
        print((d[x] + d[y]))


if __name__ == "__main__":
    main()

+import heapq -sys.setrecursionlimit(10**6) +input = sys.stdin.readline -def dfs(v, p, d): -    for i, j in tree[v]: -        if i != p: -            depth[i] = d + j -            dfs(i, v, d + j) +def dijkstra(s, edge, N): +    d = [float("inf")] * N +    used = [True] * N +    d[s] = 0 +    used[s] = False +    edgelist = [] +    for a, b in edge[s]: +        heapq.heappush(edgelist, a * (10**6) + b) +    while len(edgelist): +        minedge = heapq.heappop(edgelist) +        if not used[minedge % (10**6)]: +            continue +        v = minedge % (10**6) +        d[v] = minedge // (10**6) +        used[v] = False +        for e in edge[v]: +            if used[e[1]]: +                heapq.heappush(edgelist, (e[0] + d[v]) * (10**6) + e[1]) +    return d -N = int(eval(input())) -tree = [[] for _ in range(N)] -for i in range(N - 1): -    a, b, c = list(map(int, input().split())) -    a -= 1 -    b -= 1 -    tree[a].append((b, c)) -    tree[b].append((a, c)) -Q, K = list(map(int, input().split())) -depth = [0 for i in range(N)] -depth[K - 1] = 0 -dfs(K - 1, -1, 0) -for _ in range(Q): -    x, y = list(map(int, input().split())) -    print((depth[x - 1] + depth[y - 1])) +def main(): +    N = int(eval(input())) +    edge = [[] for _ in range(N)] +    for _ in range(N - 1): +        a, b, c = list(map(int, input().split())) +        a -= 1 +        b -= 1 +        edge[a].append([c, b]) +        edge[b].append([c, a]) +    Q, K = list(map(int, input().split())) +    K -= 1 +    d = dijkstra(K, edge, N) +    for _ in range(Q): +        x, y = list(map(int, input().split())) +        x -= 1 +        y -= 1 +        print((d[x] + d[y])) + + +if __name__ == "__main__": +    main()