#include <bits/stdc++.h>
#include <set>
using namespace std;
multiset<int>m;int n,op,x;
int main(){
    cin>>n;
    for (int i=0; i<n;++i) {
        cin>>op;
        if (op==1) {
        cin>>x;m.insert(x);
    }else if (op==2) {
        cout<<*(m.begin())<<endl;
    }else if (op==3) {
        cout<<*(--(m.end()))<<endl;
    }else if (op==4) {
        m.erase(m.begin());
    }else if (op==5) {
        m.erase(--(m.end()));
    }else {
        cout<<"error1"<<endl;
    }
    }
    return 0;
}