#include<iostream>
#include<stack>

using namespace std;
int num;
stack<int> s;

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