#include <cstdio>
#include <queue>

using namespace std;

int main(){
  deque<int> que;
  int n;
  while(~scanf("%d",&n)){
    if(n){
      que.push_back(n);
    }else{
      printf("%d\n",que.back());
      que.pop_back();
    }
  }
}