#include<bits/stdc++.h>
using namespace std;

int n,op,x;

multiset<int> mst;

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	
	cin>>n;
	while(n--){
		cin>>op;
		if(op==1){
			cin>>x;
			mst.insert(x);
		}else if(op==2){
			cout<<*mst.begin()<<endl;
		}else if(op==3){
			cout<<*(--mst.end())<<endl;
		}else if(op==4){
			mst.erase(mst.begin());
		}else if(op==5){
			mst.erase(--mst.end());
		}
	}

    return 0;
}