//Switching Railroad Cars

//vñF0ªüÍ³ê½çStack©çpopµÄ»êðoÍµëA»êÈOÍpushµÆ¯
//üÍª³µ¢±ÆÍÛá³êÄ¢é@¿ÈÝÉpush³êéàÌÍ1`10çµ¢

//ÅÍX^bNÍóÅ é

#include<iostream>
#include<stack>
using namespace std;

int main(void)
{
	stack<int> s;
	int x;
	while(cin >> x)
	{
		if(!x)
		{
			cout << s.top() << endl;
			s.pop();
		}
		else
			s.push(x);
	}
	return 0;
}