#include
#include
using namespace std;
int main() {
long long n,max=0; cin>>n;
stack s;
stack m;
for(long long i=0; i<n ; i++) {
unsigned long long k; cin>>k;
if(k==1) {
unsigned long long x; cin>>x;
s.push(x);
if(x>max) max=x;
m.push(max);
}
if(k==2) {
if(!s.empty()) {
s.pop();
m.pop();
}
}
if(k==3) {
if(!m.empty())
cout<<m.top()<<"\n";
else cout<<-1;
}
}
return 0;
}