object Main extends App {
  val src = io.Source.stdin.getLines.map(_.toInt)

  def func(stack: List[Int], num: Int): List[Int] = {
      if (num == 0) { println(stack.head); stack.tail }
      else num :: stack
  }

  (Nil /:[List[Int]] src)(func)
}

