#include <iostream>
#include <stack>

using namespace std;



int main() {
	stack<int> s;
	int in;
	
	while (cin >> in) {
		if (in != 0) s.push(in);
		
		while (!s.empty()) {
			cin >> in;
			if (in==0) {
				cout << s.top() << endl;
				s.pop();
			}
			else s.push(in);
		}
	}
	
	return 0;
}