#include<stdio.h>
#include<stack>

int main(){
	int i=0;
	std::stack<int> train;
	scanf("%d",&i);
	train.push(i);
	while(1){
		scanf("%d",&i);
		if(i==0){
			printf("%d\n",train.top());
			train.pop();
		}else{
			printf("%d\n",train.top());
			train.pop();
			train.push(i);
		}
	}
	return 0;
}
		