#include<bits/stdc++.h>
using namespace std;
int main(){
stack <int> a;
int type,weight,n;
cin>>n;
for(int i=0;i<=n-1;i++){
cin>>type;
if(type==0){
cin>>weight;
a.push(weight);
}
else if(type==1&&!a.empty()){
a.pop();
}
else if(a.empty()){
printf("0\n");
}
else{
printf("%d\n",a.top());
}
}
return 0;
}