#include <iostream>
#include <vector>

int main(){
	std::vector<int> carsQueue;
	int switchingRailroadCarsNumber;
	while (std::cin >> switchingRailroadCarsNumber){
		if (switchingRailroadCarsNumber != 0){
			carsQueue.push_back(switchingRailroadCarsNumber);
		}
		else{
			std::cout << carsQueue.at(carsQueue.size() - 1) << std::endl;
			carsQueue.pop_back();
		}
	}
	return 0;
}
