#include<iostream>
#include<queue>
#include<stack>
using namespace std;

int main()
{
	int n = 0;
	stack<int>s;
	bool flag = false; 

	while (1){
		cin >> n;

		if (n == 0){
			
			cout << s.top() << endl;
			s.pop();
			if (s.empty()){
				break;
			}
		}else{
			s.push(n);
		}
	}

	return 0;
}