#include <iostream>
#include <deque>
using namespace std;
 
int main(int argc, char** argv) {
    deque<int> deq;
    int n;
    while(cin >> n) {
        if(n == 0) {
            cout << deq.back() << endl;
            deq.pop_back();
        }else {
            deq.push_back(n);
        }
    }
    return 0;
}
