#include <iostream>
#include <set>
using namespace std;
void op1(multiset<int>& mst){
int x;cin>>x;
mst.insert(x);
}
void op2(const multiset<int>& mst){
cout<<*mst.begin()<<'\n';
}
void op3(const multiset<int>& mst){
cout<<*mst.rbegin()<<'\n';
}
void op4(multiset<int>& mst){
if(!mst.empty()) mst.erase(mst.begin());
}
void op5(multiset<int>& mst){
if(!mst.empty()) mst.erase(prev(mst.end()));
}
int main(){
ios::sync_with_stdio(false);cin.tie(nullptr);
multiset<int> mst;
int tests;cin>>tests;
while(tests--){
int op;cin>>op;
switch(op){
case 1:op1(mst);break;
case 2:op2(mst);break;
case 3:op3(mst);break;
case 4:op4(mst);break;
case 5:op5(mst);break;
default:;
}
}
return 0;
}