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

template <typename TYPE>
class my_stack{
public:
	my_stack(){};
	void push(TYPE x){
		vec.push_back(x);
		return;
	};
	TYPE pop(){
		TYPE x = vec.back();
		vec.pop_back();
		return x;
	};

private:
	vector<TYPE> vec;
};

int main()
{
	my_stack<int> stack;
	int n;
	while (cin >> n){
		if (n) stack.push(n);
		else cout << stack.pop() << "\n";
	}
	cout << flush;
}