#include<iostream>
using namespace std;

int main() {
	int n[50], x, i=0,flag=0,check=0;
	for (;;) {
		cin >> x;
		if (x == 0 && flag == 1) {
			i--;
			cout << n[i - 1];
			flag = 1;
			check = 1;
		}
		else if (x == 0) {
			cout << n[i - 1];
			flag = 1;
			check = 1;
		}
		else if (x > 0) {
			if (check == 1) {
				i--;
				check = 0;
			}
			n[i] = x;
			i++;
			flag = 0;
		}
	}
	return 0;
}