#include <cstdio>
#include <stack>
#define FOR(variable,a,b) for(variable=(a);variable<(b);variable++)

using namespace std;

int main() {
    // import phase
    int target;
    stack<int> map;
    
    // calculate phase
    while(scanf("%d", &target) != EOF) {
        if(target != 0) {
            map.push(target);
        } else {
            printf("%d\n", map.top());
            map.pop();
        }
    }

    return 0;
}