import Control.Applicative
main = do
    ss <- lines <$> getContents
    solve [] ss
solve :: [Int] -> [String] -> IO ()
solve ns ss = do
    if null ss then return ()
    else do
        let n = read $ head ss :: Int
        if n == 0
            then do
                putStrLn . show $ last ns
                solve (init ns) (tail ss)
            else
                solve (ns ++ [n]) (tail ss)