#include <iostream>
#include<set>
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n,op,x;
    cin>>n;
    multiset<int> se;
    while(n--){
        cin>>op;
        switch (op) {
        case 1:
        cin>>x;
        se.insert(x);
        break;
        case 2:
        if(!se.empty())
        cout<<*se.begin()<<'\n';
        break;
        case 3:
        if(!se.empty())
        cout<<*se.rbegin()<<'\n';
        break;
        case 4:
        if(!se.empty())
        se.erase(se.begin());
        break;
        case 5:
        if(!se.empty())
        se.erase(prev(se.end()));
        break;
        }
    }
}
// 64 位输出请用 printf("%lld")