import Control.Monad.State

type Stack a = StateT [a] IO a

main = getContents >>=  (\x -> evalStateT (moveCars x) []).map read.lines
pop :: Stack a
pop = do
    (x:xs) <- get
    put xs
    return x

push :: a -> Stack a
push x = do
    xs <- get
    put (x:xs)
    return x

moveCars :: [Int] -> StateT [Int] IO ()
moveCars lst = sequence_ $ map toFunc lst

toFunc :: Int -> Stack Int
toFunc x = if x == 0 then do
                         n <- pop
                         liftIO $ print n
                         return n
                     else push x