print' :: [String] -> [String] -> IO ()
print' list xs = do
  putStrLn $ head list
  solve (tail list) xs

solve :: [String] -> [String] -> IO ()
solve _ [] = do return ()
solve list (x:xs)
  | x == "0" = do print' list xs
  | otherwise = solve (x:list) xs

main = getContents >>= solve [] . lines