#include <iostream>
#include <cmath>
#include <cstdio>
#include <string>
#include <cstring>
#include <iomanip>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
using namespace std;
int main(){
int n;
cin>>n;
stack<int>k;
for(int i=0;i<n;i++){
int op;
cin>>op;
if(op==0){
int c;
cin>>c;
k.push(c);
}else if(op==1&&k.size()>0){
k.pop();
}else if(op==2&&k.size()>0){
int max=0;
stack<int>k2;
for(int j=0;j<k.size();j++){
int t=k.top();
k2.push(t);
if(t>max){
max=t;
}
k.pop();
}
for(int j=0;j<k2.size();j++){
int t=k2.top();
k.push(t);
k2.pop();
}
cout<<max<<endl;
}else{
cout<<0<<endl;
}
}
}