let find x a n =
  let rec doit i =
    if i = n then exit 1
    else if a.(i) = x then i
    else doit (i + 1)
  in doit 0

let () =
  let open Queue in
  let n = read_int () in
  let preorder = create () in
  let rec read i =
    if i < n then (add (Scanf.scanf "%d " (fun i -> i)) preorder; read (i + 1))
  in read 0;
  let inorder = Array.init n (fun _ -> Scanf.scanf "%d " (fun i -> i)) in
  let postorder = create () in
  let rec doit l r =
    if l < r then begin
      let root = take preorder in
      let m = find root inorder n in
      doit l m;
      doit (m + 1) r;
      add root postorder
    end in
  doit 0 n;
  print_int (take postorder);
  iter (fun e -> Printf.printf " %d" e) postorder;
  print_newline ()