import Control.Applicative

main :: IO ()
main = do
    xs <- map read . lines <$> getContents
    mapM_ print $ solve xs []

solve :: [Int] -> [Int] -> [Int]
solve [] _ = []
solve (x:xs) [] = solve xs [x]
solve (x:xs) q'@(q:qs)
    | x == 0    = q:(solve xs qs)
    | otherwise = solve xs (x:q')