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