#include <stdio.h>
#include <stack>

using namespace std;

int main(){
  stack<int> stk;

  int n;
  while(scanf("%d", &n)!=EOF){
    if(n==0){
      printf("%d\n", stk.top());
      stk.pop();
    }else{
      stk.push(n);
    }
  }
}