let split_string ?(pattern=" ") = Str.split @@ Str.regexp pattern

let n = Scanf.sscanf (read_line ()) "%d" @@ fun n -> n
let ab = Array.init (n - 1) @@ fun _ -> Scanf.sscanf (read_line ()) "%d %d" @@ fun a b -> (a - 1, b - 1)
let c = List.sort (fun a b -> b - a) @@ List.map int_of_string @@ split_string @@ read_line ()

let rec sum = function | ([] | _ :: []) -> 0 | x :: xs -> x + sum xs
let sum = sum c

let rec find_index i arr f = 
  if Array.length arr = i then -1
  else if f arr.(i) then i
  else find_index (i + 1) arr f
let find_index = find_index 0 ab

let s = Array.make n false
let ans = Array.make n 0

let rec loop = function
  | [] -> ()
  | x :: xs ->
    let idx = find_index (fun (a, b) -> a + b > 0 && (s.(a) || s.(b))) in
    let (a, b) = ab.(idx) in
    ab.(idx) <- (-1, -1);
    if s.(a) then begin
      s.(b) <- true; ans.(b) <- x
    end
    else begin
      s.(a) <- true; ans.(a) <- x
    end;
    loop xs

let () =
  match c with
    | x :: [] -> print_endline "0"
    | x :: y :: xs ->
      let (a, b) = ab.(0) in
      Printf.printf "%d %d\n" a b;
      ab.(0) <- (-1, -1);
      ans.(a) <- x; s.(a) <- true;
      ans.(b) <- y; s.(b) <- true;
      loop xs;
      print_endline @@ String.concat " " @@ Array.to_list @@ Array.map string_of_int ans;
    | _ -> failwith ""