#include <iostream>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <unordered_map>
#include <iomanip>
#include <stack>
#include <queue>
#include <deque>
#define uwf(name) if(fopen(name".inp","r")){freopen(name".inp","r",stdin);freopen(name".out","w",stdout);}
#define zukkyng int main()
#define f first
#define s second
#define riel_pragma ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define ll long long
#define pii pair<int,int>
using namespace std;
/*
this code was written by zukkyng/AlephZ from ILS(2024-2027)
*/
stack<int> mmb;
stack<int> bmg;
zukkyng
{
int n,dir,x;
cin>>n;
while (n--)
{
cin>>dir;
if (dir==1)
{
cin>>x;
mmb.push(x);
if (bmg.empty()) bmg.push(x);
else
{
if (x>bmg.top()) bmg.push(x);
}
}
if (dir==2)
{
if (mmb.top()==bmg.top()) bmg.pop();
mmb.pop();
}
if (dir==3) cout<<bmg.top()<<'\n';
}
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8ZnN0cmVhbT4KI2luY2x1ZGUgPGFsZ29yaXRobT4KI2luY2x1ZGUgPGNtYXRoPgojaW5jbHVkZSA8dmVjdG9yPgojaW5jbHVkZSA8dW5vcmRlcmVkX21hcD4KI2luY2x1ZGUgPGlvbWFuaXA+CiNpbmNsdWRlIDxzdGFjaz4KI2luY2x1ZGUgPHF1ZXVlPgojaW5jbHVkZSA8ZGVxdWU+CiNkZWZpbmUgdXdmKG5hbWUpIGlmKGZvcGVuKG5hbWUiLmlucCIsInIiKSl7ZnJlb3BlbihuYW1lIi5pbnAiLCJyIixzdGRpbik7ZnJlb3BlbihuYW1lIi5vdXQiLCJ3IixzdGRvdXQpO30KI2RlZmluZSB6dWtreW5nIGludCBtYWluKCkKI2RlZmluZSBmIGZpcnN0IAojZGVmaW5lIHMgc2Vjb25kIAojZGVmaW5lIHJpZWxfcHJhZ21hIGlvc19iYXNlOjpzeW5jX3dpdGhfc3RkaW8oMCk7Y2luLnRpZSgwKTtjb3V0LnRpZSgwKTsKI2RlZmluZSBsbCBsb25nIGxvbmcKI2RlZmluZSBwaWkgcGFpcjxpbnQsaW50Pgp1c2luZyBuYW1lc3BhY2Ugc3RkOwovKgogICAgdGhpcyBjb2RlIHdhcyB3cml0dGVuIGJ5IHp1a2t5bmcvQWxlcGhaIGZyb20gSUxTKDIwMjQtMjAyNykKKi8KCnN0YWNrPGludD4gbW1iOwpzdGFjazxpbnQ+IGJtZzsKenVra3luZwp7CiAgICBpbnQgbixkaXIseDsKICAgIGNpbj4+bjsKICAgIHdoaWxlIChuLS0pCiAgICB7CiAgICAgICAgY2luPj5kaXI7CiAgICAgICAgaWYgKGRpcj09MSkKICAgICAgICB7CiAgICAgICAgICAgIGNpbj4+eDsKICAgICAgICAgICAgbW1iLnB1c2goeCk7CiAgICAgICAgICAgIGlmIChibWcuZW1wdHkoKSkgYm1nLnB1c2goeCk7CiAgICAgICAgICAgIGVsc2UKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgaWYgKHg+Ym1nLnRvcCgpKSBibWcucHVzaCh4KTsKICAgICAgICAgICAgfQogICAgICAgIH0KICAgICAgICBpZiAoZGlyPT0yKQogICAgICAgIHsKICAgICAgICAgICAgaWYgKG1tYi50b3AoKT09Ym1nLnRvcCgpKSBibWcucG9wKCk7CiAgICAgICAgICAgIG1tYi5wb3AoKTsKICAgICAgICB9CiAgICAgICAgaWYgKGRpcj09MykgY291dDw8Ym1nLnRvcCgpPDwnXG4nOyAKICAgIH0KfQo=