import heapq



V = int(eval(input()))

E = V - 1

G = {}

for i in range(V):

    G[i] = {}



for _ in range(E):

    s, t, c = [int(_) for _ in input().split()]

    s -= 1  # 0-indexed

    t -= 1  # 0-indexed

    G[s][t] = c

    G[t][s] = c

Q, K = [int(_) for _ in input().split()]

K -= 1  # 0-indexed

XY = [[int(_) for _ in input().split()] for _ in range(Q)]



INF = float('inf')



que = []

i = K

d = 0

D = [INF] * V

D[i] = 0

used = [False] * V

used[i] = True



while True:

    for j in list(G[i].keys()):

        heapq.heappush(que, (d + G[i][j], j))

    v = -1

    while que:

        d, j = heapq.heappop(que)

        if not used[j]:

            v = j

            break

    if v == -1:

        break

    else:

        used[v] = True

        D[v] = d

        i = v

for x, y in XY:

    x -= 1  # 0-indexed

    y -= 1  # 0-indexed

    print((D[x] + D[y]))
V = int(eval(input()))

E = V - 1

G = {}

for i in range(V):

    G[i] = {}



for _ in range(E):

    s, t, c = [int(_) for _ in input().split()]

    s -= 1  # 0-indexed

    t -= 1  # 0-indexed

    G[s][t] = c

    G[t][s] = c

Q, K = [int(_) for _ in input().split()]

K -= 1  # 0-indexed

XY = [[int(_) for _ in input().split()] for _ in range(Q)]



INF = float('inf')

D = [INF] * V

D[K] = 0

cand = []

for j in list(G[K].keys()):

    cand += [(K, j)]

while True:

    if cand:

        x, y = cand.pop()

        if D[y] == INF:

            D[y] = D[x] + G[x][y]

            for z in list(G[y].keys()):

                cand += [(y, z)]

    else:

        break

for x, y in XY:

    x -= 1

    y -= 1

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

import heapq

V = int(eval(input()))
E = V - 1
G = {}
for i in range(V):
    G[i] = {}
for _ in range(E):
    s, t, c = [int(_) for _ in input().split()]
    s -= 1  # 0-indexed
    t -= 1  # 0-indexed
    G[s][t] = c
    G[t][s] = c
Q, K = [int(_) for _ in input().split()]
K -= 1  # 0-indexed
XY = [[int(_) for _ in input().split()] for _ in range(Q)]
INF = float("inf")
que = []
i = K
d = 0
D = [INF] * V
D[i] = 0
used = [False] * V
used[i] = True
while True:
    for j in list(G[i].keys()):
        heapq.heappush(que, (d + G[i][j], j))
    v = -1
    while que:
        d, j = heapq.heappop(que)
        if not used[j]:
            v = j
            break
    if v == -1:
        break
    else:
        used[v] = True
        D[v] = d
        i = v
for x, y in XY:
    x -= 1  # 0-indexed
    y -= 1  # 0-indexed
    print((D[x] + D[y]))

V = int(eval(input()))
E = V - 1
G = {}
for i in range(V):
    G[i] = {}
for _ in range(E):
    s, t, c = [int(_) for _ in input().split()]
    s -= 1  # 0-indexed
    t -= 1  # 0-indexed
    G[s][t] = c
    G[t][s] = c
Q, K = [int(_) for _ in input().split()]
K -= 1  # 0-indexed
XY = [[int(_) for _ in input().split()] for _ in range(Q)]
INF = float("inf")
D = [INF] * V
D[K] = 0
cand = []
for j in list(G[K].keys()):
    cand += [(K, j)]
while True:
    if cand:
        x, y = cand.pop()
        if D[y] == INF:
            D[y] = D[x] + G[x][y]
            for z in list(G[y].keys()):
                cand += [(y, z)]
    else:
        break
for x, y in XY:
    x -= 1
    y -= 1
    print((D[x] + D[y]))

-import heapq - -que = [] -i = K -d = 0 -D[i] = 0 -used = [False] * V -used[i] = True +D[K] = 0 +cand = [] +for j in list(G[K].keys()): +    cand += [(K, j)] -    for j in list(G[i].keys()): -        heapq.heappush(que, (d + G[i][j], j)) -    v = -1 -    while que: -        d, j = heapq.heappop(que) -        if not used[j]: -            v = j -            break -    if v == -1: +    if cand: +        x, y = cand.pop() +        if D[y] == INF: +            D[y] = D[x] + G[x][y] +            for z in list(G[y].keys()): +                cand += [(y, z)] +    else: -    else: -        used[v] = True -        D[v] = d -        i = v -    x -= 1  # 0-indexed -    y -= 1  # 0-indexed +    x -= 1 +    y -= 1