
import scala.collection.mutable.Stack
import scala.util.control.Breaks
object Main extends App {
  val stack = Stack.empty[Int]

  val b = Breaks

  b.breakable {
    for (line <- io.Source.stdin.getLines()) {
      if (line.isEmpty) b.break()

      val n = line.toInt

      if (n == 0) {
        println(stack.pop)
      } else {
        stack.push(n)
      }
    }
  }
}
