module MakeBinaryHeap (M : sig type t val compare : t -> t -> int end) = struct

  type t = { node : M.t array; mutable size : int }

  let make n (init : M.t) = { node = Array.make n init; size = 0 }

  let empty_p t = if t.size = 0 then true else false

  let pop t =
    if t.size <= 0 then failwith "out of size" else
    let rec max_heapify i =
      let l = 2*i in
      let r = 2*i + 1 in
      let m = if l <= t.size && M.compare t.node.(l) t.node.(i) > 0 then l else i in
      let m = if r <= t.size && M.compare t.node.(r) t.node.(m) > 0 then r else m in
      if m = i then ()
      else begin
        let tmp = t.node.(i) in
        t.node.(i) <- t.node.(m);
        t.node.(m) <- tmp;
        max_heapify m
      end in
    let ret = t.node.(1) in
    t.node.(1) <- t.node.(t.size);
    t.size <- t.size - 1;
    max_heapify 1;
    ret

  let push x t =
    let parent i = int_of_float (floor (float_of_int i) /. 2.) in
    let rec doit i =
      let p = parent i in
      if i <= 1 || M.compare t.node.(p) t.node.(i) >= 0 then ()
      else begin
        let tmp = t.node.(i) in
        t.node.(i) <- t.node.(p);
        t.node.(p) <- tmp;
        doit p
      end in
    t.size <- t.size + 1;
    t.node.(t.size) <- x;
    doit t.size

end

module H = MakeBinaryHeap(struct type t = int * int let compare (_, x) (_, y) = y - x end)

let dijkstra t n k =
  let reached_p = Array.make n false in
  let d = Array.make n max_int in
  d.(k) <- 0;
  let h = H.make 10000 (0, 0) in
  H.push (k, 0) h;
  while not (H.empty_p h) do
    let (u, x) = H.pop h in
    reached_p.(u) <- true;
    if x > d.(u) then ()
    else
      List.iter (fun (v, c) ->
        if c = max_int then ()
        else begin
          let x = d.(u) + c in
          if not reached_p.(v) && x < d.(v) then begin
            d.(v) <- x;
            H.push (v, x) h;
          end
        end) t.(u);
  done;
  d

let () =
  let n = Scanf.scanf "%d " (fun i -> i) in
  let t = Array.make n [] in
  for _ = 1 to n - 1 do
    let a, b, c = Scanf.scanf "%d %d %d " (fun a b c -> a, b, c) in
    let a, b = a - 1, b - 1 in
    t.(a) <- (b, c) :: t.(a);
    t.(b) <- (a, c) :: t.(b);
  done;
  let q, k = Scanf.scanf "%d %d " (fun q k -> q, k) in
  let k = k - 1 in
  let d = dijkstra t n k in
  for _ = 0 to q - 1 do
    let x, y = Scanf.scanf "%d %d " (fun x y -> x, y) in
    let x, y = x - 1, y - 1 in
    Printf.printf "%d\n" (d.(x) + d.(y))
  done