let n = read_int ()
let g = Array.make n []
let ds = Array.make n 0
let rec f v p d = ds.(v) <- d; List.iter (fun (u, c) -> if u <> p then f u v (d + c)) g.(v)

let l = 16
let ps = Array.make_matrix l n 0
let dps = Array.make n 0
let rec dfs v p d = ps.(0).(v) <- p; dps.(v) <- d; List.iter (fun (u, _) -> if u <> p then dfs u v (d + 1)) g.(v)
let lca a b = let u, v = ref a, ref b in if dps.(a) > dps.(b) then (u := b; v := a); for k = 0 to l - 1 do if ((dps.(!v) - dps.(!u)) lsr k) land 1 = 1 then v := ps.(k).(!v) done; if !u = !v then !u else (for k = l - 1 downto 0 do if ps.(k).(!u) <> ps.(k).(!v) then (u := ps.(k).(!u); v := ps.(k).(!v)) done; ps.(0).(!u))
let init n = dfs 0 ~-1 0; for k = 0 to l - 2 do for v = 0 to n - 1 do ps.(k + 1).(v) <- if ps.(k).(v) < 0 then -1 else ps.(k).(ps.(k).(v)) done done

let h u v = ds.(u) + ds.(v) - 2 * ds.(lca u v)
let _ = for _ = 1 to n - 1 do Scanf.scanf " %d %d %d" @@ fun a b c -> g.(a - 1) <- (b - 1, c) :: g.(a - 1); g.(b - 1) <- (a - 1, c) :: g.(b - 1) done; f 0 ~-1 0
let q, k = Scanf.scanf " %d %d" @@ fun a b -> a, b - 1
let _ = init n; for _ = 1 to q do Scanf.scanf " %d %d" @@ fun x y -> Printf.printf "%d\n" @@ h (x - 1) k + h (y - 1) k done