Scanf.scanf "%d" (fun n ->
    let nei = Array.make n [] in
    for i = 1 to n - 1 do
        Scanf.scanf " %d %d %d" (fun a b c ->
            let a, b = a - 1, b - 1 in
            nei.(a) <- (c, b) :: nei.(a);
            nei.(b) <- (c, a) :: nei.(b);
        )
    done;
    Scanf.scanf " %d %d" (fun q k ->
        let k = k - 1 in
        let dist = Array.make n max_int in
        dist.(k) <- 0;
        let rec loop next = function
            | [] -> if next <> [] then loop [] next
            | hd :: tl ->
                    let cur = dist.(hd) in
                    let next = List.fold_left (fun next (c, v) ->
                        if dist.(v) > cur + c then (
                            dist.(v) <- cur + c;
                            v :: next
                        ) else next
                    ) next nei.(hd) in
                    loop next tl
        in
        loop [] [ k ];
        for i = 1 to q do
            Scanf.scanf " %d %d" (fun x y ->
                let x, y = x - 1, y - 1 in
                Printf.printf "%d\n" (dist.(x) + dist.(y))
            )
        done
    )
)