#include<iostream>
#include<vector>
#include<string>
#include<math.h>
using namespace std;

int main()
{
	while(true)
	{
	vector<int>a;
	vector<int>b;
	vector<int>c;
	int s[20];

for(int i=0;i<20;i++)
{
	cin>>s[i];

if(s[i]==0)
	{
		b.push_back(s[i]);
		a.push_back(c.back());
		c.pop_back();
		if(c.size()==0)
		{
		break;
		}
	}
	else
	{
		c.push_back(s[i]);
	}
	
}
for(int j=0;j<a.size();j++)cout<<a[j]<<endl;
	}
return 0;


}