class SwitchingRails
  def initialize
    @cars = []
  end

  def io_cars(num)
    if num == 0
      puts @cars.pop
    else
      @cars.push(num)
    end
  end
end

obj = SwitchingRails.new()

while line = gets
  num = line.to_i
  obj.io_cars(num)
end