//#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
//#include "bits/stdc++.h"

#define rep(i,n) for(ll (i)=0;(i)<(ll)(n);(i)++)
#define all(x) (x).begin(),(x).end()

#define MOD 1000000007LL
#define INF (1LL<<60LL)
#define int long long
#define MAX_N (100001)
typedef long long ll;

using namespace std;




signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	deque<int> d;
	while (1) {
		int k;
		cin >> k;
		if (cin.eof()) break;
		if (k == 0) {
			cout << d.back() << endl;
			d.pop_back();
		}
		else {
			d.push_back(k);
		}
	}
}

