Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

type Railway struct {
	trains trains
}

type trains []int

func main() {
	var stdin string
	var s = bufio.NewScanner(os.Stdin)

	railway := Railway{}
	for s.Scan() {
		stdin = s.Text()

		n, err := strconv.Atoi(stdin)
		if err != nil {
			continue
		}
		railway.lifo(n)
	}
}

func (r *Railway) lifo(n int) {
	if n == 0 {
		if len(r.trains) == 0 {
			return
		}
		fmt.Println(r.trains[len(r.trains)-1])
		r.trains = r.trains[:len(r.trains)-1]
	} else {
		r.trains = append(r.trains, n)
	}
}

C++: //#define _GRIBCXX_DEBUG
#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <iomanip>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;


// ?????¬???????????¬??????
#pragma region MACRO
#define P(x) cout << (x) << endl
#define p(x) cout << (x)
#define PED cout << "\n"
#define rep(i,n) for(int i=0; i<(int)n; ++i)
#define REP(i,x,n) for(int i=x; i<(int)n; ++i)
#define repi(i,n) for(int i=0; i<=(int)n; ++i)
#define REPI(i,x,n) for(int i=x; i<=(int)n; ++i)
#define ILP while(true)
#define FOR(i,c) for(__typeof((c).begin())!=(c).begin(); i!=(c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
#define mp make_pair
#pragma endregion

// ???
#pragma region TYPE_DEF
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<string, string> pss;
typedef pair<string, int> psi;
typedef pair<int, string> pis;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<long> vl;
typedef vector<long long> vll;
typedef vector<string> vs;
#pragma endregion

// Effective std
#pragma region ESTD
template<typename C, typename T>
constexpr int count(C& c, T t) { return count(ALL(c), t); }
template<typename C, typename F>
constexpr int count_if(C& c, F f) { return count_if(ALL(c), f); }
template<typename C, typename T>
void erase(C& c, T t) { remove(ALL(c), t), c.end(); }
template<typename C>
void remove(vector<C>& c, unsigned int index) { c.erase(c.begin()+index); }
template<typename C, typename T, typename U>
constexpr void replace(C& c, T t, U u) { replace(ALL(c), t, u); }
template<typename C, typename F, typename U>
constexpr void replace_if(C& c, F f, U u) { (ALL(c), f, u); }
template<typename C>
constexpr void reverse(C& c) { reverse(ALL(c)); }
template<typename C>
constexpr void sort(C& c) { sort(ALL(c)); }
template<typename C, typename Pred>
constexpr void sort(C& c, Pred p) { sort(ALL(c), p); }
#pragma endregion

// ?????°
#pragma region CONST_VAL
#define PI (2*acos(0.0))
#define EPS (1e-9)
#define MOD (int)(1e9+7)
#pragma endregion


int main()
{
    int n;
    stack<int> st;
    while(cin>>n) {
        if(n==0){
            P(st.top());
            st.pop();
        }
        else st.push(n);
    }
    return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<stack>
using namespace std;
#define int long long
signed main(){
	stack<int> q;
	int a;
	while( scanf("%lld", &a) != EOF ){
		if(a==0){ cout << q.top() << endl; q.pop();}
		else q.push(a);
	}
	return 0;
}

C++: #include <iostream>
#include <stack>

using namespace std;

void Solution()
{
    stack <int> car;
    
    int n;
    
    while (cin>>n)
    {
        if(n == 0)
        {
            cout<<car.top()<<endl;
            car.pop(); //Removes the element on top of the stack
        }
        else
        {
            car.push(n);
        }

    }

    return;
}

int main()
{
    Solution();
    return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <stack>
#include <vector>
using namespace std;

int main(void) {
    stack<int> unforwardable;
    vector<int> output;
    bool update = false;
    while(!update || !unforwardable.empty()) {
        update = true;
        int car; cin >> car;
        if(car == 0) {
            output.push_back(unforwardable.top());
            unforwardable.pop();
        } else {
            unforwardable.push(car);
        }
    }
    vector<int>::iterator ite;
    for(ite = output.begin(); ite != output.end(); ite++) {
        cout << *ite << endl;
    }
    return(0);
}

C++: #include <iostream>
#include <stack>

using namespace std;

int main()
{
	stack<int> st;

	int n;
	while (cin >> n)
	{
		if (n == 0)
		{
			cout << st.top() << endl;
			st.pop();
			continue;
		}

		st.push(n);
	}

	return 0;
}

C++: #include<stdio.h>

int main(){
	int car[11],i=0,n;
	do{
		if(scanf("%d",&n)==EOF)break;
		if(n){
			car[i]=n;
			i++;
		}else{
			i--;
			printf("%d\n",car[i]);
			
		}
	}while(i>0);
}

C++: #include<iostream>
#include<stack>
using namespace std;
int main(){
  stack<int> s;
  int n;
  //cin>>n;
  // s.push(n);
  do{
    cin>>n;
    if(n==0){
      cout<<s.top()<<endl;
      s.pop();
    }
    else{
      s.push(n);
    }
  } while(!s.empty());
}

C++: #include <cstdio>

using namespace std;

int main(void){
	int x, st[10], p = 0;
	while(scanf("%d",&x) != EOF){
		if(x == 0){
			printf("%d\n",st[--p]);
		}else{
			st[p++] = x;
		}
	}
	return 0;
}

C++: #include <iostream>
#include <stack>
using namespace std;

int main(){
    int i;
    stack<int> s;
    while(cin >> i){
        if (i == 0){
            cout << s.top() << endl;
            s.pop();
        }else{
            s.push(i);
        }
    }

}

C++: #include <iostream>
#include <algorithm>
#include <vector>
#include <cstdio>
#include <cmath>
#include <string>
#include <queue>
#include <stack>
#include <sstream>
using namespace std;
int main(){
	stack<int> train;
	int n;
	while(cin>>n){
		if(n!=0){
			train.push(n);
		}else{
			cout<<train.top()<<endl;
			train.pop();
		}
	}
	return 0;
}

C++: #include<iostream>
using namespace std;

int main(){
	int in[10],out[10];
	int in_num=0;
	int out_num=0;
	int n;
	while(cin >>n){
		if(n == 0){
			in_num--;
			out[out_num] = in[in_num];
			out_num++;
		}
		else {
			in[in_num] = n;
			in_num++;
		}
	}
	for(int i=0;i<out_num;i++)
		cout <<out[i]<<endl;
}

C++: #include<cstdio>
#include<stack>
using namespace std;
int main(){
  stack<int> s;
  do{
    int num;scanf("%d",&num);
    if(num){
      s.push(num);
    }else{
      printf("%d\n",s.top());
      s.pop();
    }
  }while(!s.empty());
}

C++: #include<iostream>

using namespace std;

int main() {
	int train[10] = { 0 }, junban[10] = { 0 }, a = 0, n, i;
	while (cin >> n) {
		if (n == 0) {
			for (i = 0; train[i] > 0; i++) {}
			junban[a] = train[i - 1];
			train[i - 1] = 0;
			a++;
		}
		else {
			for (i = 0; train[i] > 0; i++) {}
			train[i] = n;
		}
	}
	for (i = 0; junban[i] > 0; i++) {
		cout << junban[i] << endl;
	}
	return 0;
}