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