let _ =
  let trk = Stack.create () in
  try
    while true do
      let t = Scanf.scanf "%d\n" (fun x -> x) in
      if t = 0 then
        Printf.printf "%d\n" (Stack.pop trk)
      else
        Stack.push t trk
    done
  with
    End_of_file -> ()