#include <deque>
#include <iostream>
using namespace std;

int main() {
	int x;
	deque<int> st;
	while (cin >> x) {
		if (x == 0) {
			cout << st.back() << endl;
			st.pop_back();
		}
		else {
			st.push_back(x);
		}
	}
	return 0;
}