object Main extends App {
  import scala.annotation.tailrec

  val stack = new scala.collection.mutable.Stack[Int]

  @tailrec
  def loop() {
    val line = scala.io.StdIn.readLine()
    if (line == null) return
    else {
      val n = line.toInt
      if (n == 0) println(stack.pop())
      else stack.push(n)
      loop()
    }
  }

  loop()
}