import Control.Applicative
import Control.Monad

main :: IO ()
main = do
  ls <- map read . lines <$> getContents
  sequence_ $ reverse $ snd $ foldl f ([],[]) ls
  where
    f ([], acs) v
      | v /= 0 = ([v], acs)
    f (xss@(x:xs), acs) v
      | v == 0    = (xs, print x : acs)
      | otherwise = (v:xss, acs)