open Printf
open Scanf
open Num


module Pqueue = struct

  type 'a t = {mutable cnt : int; hp : 'a array; cmp : 'a -> 'a -> int}

  let create n v f = {cnt = 0; hp = Array.make n v; cmp = f}

  let push k pq =
    let swap i j =
      let t = pq.hp.(i) in
      pq.hp.(i) <- pq.hp.(j);
      pq.hp.(j) <- t in
    let rec iter i =
      let p = i / 2 in
      if p = 0 || pq.cmp pq.hp.(p) pq.hp.(i) >= 0 then ()
      else (swap p i; iter p)
    in
    pq.cnt <- pq.cnt + 1;
    pq.hp.(pq.cnt) <- k;
    iter (pq.cnt)

  let max_heapify pq n =
    let swap i j =
      let t = pq.hp.(i) in
      pq.hp.(i) <- pq.hp.(j);
      pq.hp.(j) <- t in
    let rec iter i =
      let l = 2 * i in
      let r = 2 * i + 1 in
      let mi =
        let ti = if l <= pq.cnt && pq.cmp pq.hp.(l) pq.hp.(i) > 0 then l else i in
        if r <= pq.cnt && pq.cmp pq.hp.(r) pq.hp.(ti) > 0 then r else ti in
      if mi <> i then
        begin
          swap mi i;
          iter mi
        end
    in
    iter n

  let pop pq =
    let ret = pq.hp.(1) in
    pq.hp.(1) <- pq.hp.(pq.cnt);
    pq.cnt <- pq.cnt - 1;
    max_heapify pq 1;
    ret

  let top pq = pq.hp.(1)

  let is_empty pq = pq.cnt = 0

  let length pq = pq.cnt

end

let id x = x

let dijkstra g n s =
  let d = Array.make n (num_of_string "200000000000000") in
  let av = Array.make n true in
  let pq = Pqueue.create 2000000 ((num_of_int 0),0) (fun x y -> let x1 = fst x and y1 = fst y in if y1 >/ x1 then 1 else if y1 =/ x1 then 0 else (-1)) in
  let rec loop () =
    if Pqueue.is_empty pq then d
    else let du, u = Pqueue.pop pq in
         if av.(u) then begin
             av.(u) <- false;
             List.iter (fun (v, c) ->
                        if du +/ c </ d.(v) then begin
                            d.(v) <- du +/ c;
                            Pqueue.push (d.(v), v) pq
                          end
                       ) g.(u);
             loop ()
           end
         else loop ()
  in
  d.(s) <- num_of_int 0;
  Pqueue.push (d.(s), s) pq;
  loop ()

let () =
  let n = scanf "%d " id in
  let g = Array.make n [] in
  let rec make_g x =
    if x = 0 then ()
    else
      let a, b, c = scanf "%d %d %s " (fun i j k -> (i-1, j-1, num_of_string k)) in
      g.(a) <- (b, c) :: g.(a);
      g.(b) <- (a, c) :: g.(b);
      make_g (x-1) in
  make_g (n-1);
  let q, k = scanf "%d %d " (fun i j -> (i, j-1)) in
  let d = dijkstra g n k in
  let rec loop z =
    if z = 0 then ()
    else
      let x, y = scanf "%d %d " (fun i j -> (i-1, j-1)) in
      printf "%s\n" (string_of_num (d.(x) +/ d.(y)));
      loop (z-1)
  in
  loop q
