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

using namespace std;

int main(void){

	int inp;
	stack<int> train;

	while(scanf("%d",&inp)!=EOF){
		if(inp!=0){
			train.push(inp);
		}else{
			printf("%d\n",train.top());
			train.pop();
		}
	}

	return 0;
}