#include <iostream>
#include<set>
#include <sys/types.h>
using namespace std;
int main() {
multiset<int> heap;
int n; cin >> n;
for(int i = 0; i < n; i++) {
int op,x; cin >> op;
if(op == 1) {
cin >> x;
heap.insert(x);
continue;
}
if(op == 2) {
if(!heap.empty()) {
cout << *heap.begin() << endl;
}
continue;
}
if(op == 3) {
if(!heap.empty()) {
cout << *(--heap.end()) << endl;
}
continue;
}
if(op == 4) {
if(!heap.empty()) {
heap.erase(heap.begin());
}
continue;
}
if(op == 5) {
if(!heap.empty()) {
heap.erase(--heap.end());
}
continue;
}
}
}
// 64 位输出请用 printf("%lld")