from collections import defaultdict as dd

from collections import deque as dq

N = int(eval(input()))

e = dd(list)

inf = 10 ** 15

for _ in range(N - 1):

  u, v, d = list(map(int, input().split()))

  e[u].append((v, d))

  e[v].append((u, d))

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

d = [inf] * (N + 1)

d[K] = 0

q = dq([])

q.append(K)

while len(q):

  p = q.popleft()

  for t in e[p]:

    temp = d[p] + t[1]

    if d[t[0]] > temp:

      d[t[0]] = temp

      q.append(t[0])

for _ in range(Q):

  u, v = list(map(int, input().split()))

  print((d[u] + d[v]))
import sys

from collections import defaultdict as dd

input = sys.stdin.readline

N = int(eval(input()))

e = dd(list)

for _ in range(N - 1):

  u, v, c = list(map(int, input().split()))

  e[u].append((v, c))

  e[v].append((u, c))

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

import heapq

class dijkstra:

  def __init__(self, n, e):

    self.e = e

    self.n = n

  def path(self, s, t):

    d = [float("inf")] * (self.n + 1)

    vis = set()

    d[s] = 0

    h = [s]

    while not t in vis and len(h):

      v = heapq.heappop(h)

      v1 = v % (10 ** 6)

      v0 = v // (10 ** 6)

      if v1 in vis: continue

      vis.add(v1)

      for p in self.e[v1]:

        d[p[0]] = min(d[p[0]], d[v1] + p[1])

        if p[0] in vis: continue

        heapq.heappush(h, d[p[0]] * (10 ** 6) + p[0])

    return d

dij = dijkstra(N, e)

p = dij.path(K, 0)



for _ in range(Q):

  u, v = list(map(int, input().split()))

  print((p[u] + p[v]))
from collections import defaultdict as dd
from collections import deque as dq

N = int(eval(input()))
e = dd(list)
inf = 10**15
for _ in range(N - 1):
    u, v, d = list(map(int, input().split()))
    e[u].append((v, d))
    e[v].append((u, d))
Q, K = list(map(int, input().split()))
d = [inf] * (N + 1)
d[K] = 0
q = dq([])
q.append(K)
while len(q):
    p = q.popleft()
    for t in e[p]:
        temp = d[p] + t[1]
        if d[t[0]] > temp:
            d[t[0]] = temp
            q.append(t[0])
for _ in range(Q):
    u, v = list(map(int, input().split()))
    print((d[u] + d[v]))

import sys
from collections import defaultdict as dd

input = sys.stdin.readline
N = int(eval(input()))
e = dd(list)
for _ in range(N - 1):
    u, v, c = list(map(int, input().split()))
    e[u].append((v, c))
    e[v].append((u, c))
Q, K = list(map(int, input().split()))
import heapq


class dijkstra:
    def __init__(self, n, e):
        self.e = e
        self.n = n

    def path(self, s, t):
        d = [float("inf")] * (self.n + 1)
        vis = set()
        d[s] = 0
        h = [s]
        while not t in vis and len(h):
            v = heapq.heappop(h)
            v1 = v % (10**6)
            v0 = v // (10**6)
            if v1 in vis:
                continue
            vis.add(v1)
            for p in self.e[v1]:
                d[p[0]] = min(d[p[0]], d[v1] + p[1])
                if p[0] in vis:
                    continue
                heapq.heappush(h, d[p[0]] * (10**6) + p[0])
        return d


dij = dijkstra(N, e)
p = dij.path(K, 0)
for _ in range(Q):
    u, v = list(map(int, input().split()))
    print((p[u] + p[v]))

+import sys -from collections import deque as dq +input = sys.stdin.readline -inf = 10**15 -    u, v, d = list(map(int, input().split())) -    e[u].append((v, d)) -    e[v].append((u, d)) +    u, v, c = list(map(int, input().split())) +    e[u].append((v, c)) +    e[v].append((u, c)) -d = [inf] * (N + 1) -d[K] = 0 -q = dq([]) -q.append(K) -while len(q): -    p = q.popleft() -    for t in e[p]: -        temp = d[p] + t[1] -        if d[t[0]] > temp: -            d[t[0]] = temp -            q.append(t[0]) +import heapq + + +class dijkstra: +    def __init__(self, n, e): +        self.e = e +        self.n = n + +    def path(self, s, t): +        d = [float("inf")] * (self.n + 1) +        vis = set() +        d[s] = 0 +        h = [s] +        while not t in vis and len(h): +            v = heapq.heappop(h) +            v1 = v % (10**6) +            v0 = v // (10**6) +            if v1 in vis: +                continue +            vis.add(v1) +            for p in self.e[v1]: +                d[p[0]] = min(d[p[0]], d[v1] + p[1]) +                if p[0] in vis: +                    continue +                heapq.heappush(h, d[p[0]] * (10**6) + p[0]) +        return d + + +dij = dijkstra(N, e) +p = dij.path(K, 0) -    print((d[u] + d[v])) +    print((p[u] + p[v]))