#include <iostream>
#include <algorithm>
#include <vector>
#include <cstdio>
#include <cmath>
#include <string>
#include <queue>
#include <stack>
#include <sstream>
using namespace std;
int main(){
	stack<int> train;
	int n;
	while(cin>>n){
		if(n!=0){
			train.push(n);
		}else{
			cout<<train.top()<<endl;
			train.pop();
		}
	}
	return 0;
}