#include<stdio.h>

int	i	=	0,	result;
int	train[100];

void	push(int	k){
	train[i]	=	k;
	i++;
}

void	pop(int train[]){
	int	a,	check	=	0;
	for(a	=	i	-	1;	a	>=	0;	a--){
		if(train[a]	!=	0){
			result	=	train[a];
			train[a]	=	0;
			check	+=	1;
		}
		if(check	!=	0)break;
	}
}


int	main(){
	int	k;
	while(1){
		scanf("%d",	&k);
		push(k);
		if(k	==	0){
			pop(train);
			i	-=	2;
			printf("%d\n",	result);
		}
		if(i	==	0)break;
	}
}


	