class Switch
   def initialize
	  @cars = []
	  @out =[]
   end
   attr_reader :out
   def input(x)
	  @cars.push(x)
   end
   def output
	  @out.push(@cars[-1])
	  @cars.slice!(-1)
   end
end
s = Switch.new
while line = gets
   num = line.to_i
   if num != 0
	  s.input(num)
   else
	  s.output
   end
end
s.out.each {|each| puts each.to_s }