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