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

int main(){
	stack<int> st;
	queue<int> que;
	int train;
	while(cin >> train){
		if(train)
			st.push(train);
		else{
			que.push(st.top());
			st.pop();
		}
	}
	while(!que.empty()){
		cout << que.front() << endl;
		que.pop();
	}
	return 0;
}