#include<cstdio>
#include<queue>
using namespace std;

int main(){
  int x;
  queue<int> q;
  while(scanf("%d", &x) != EOF){
    if(x == 0){
      printf("%d\n", q.front());
      q.pop();
    }
    else{
      q.push(x);
    }
  }
  return 0;
}