import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        TreeMap<Integer, Integer> map = new TreeMap<>();
        while (n-- > 0) {
            int op = in.nextInt();
            switch (op) {
                case 1:
                    int x = in.nextInt();
                    map.put(x, map.getOrDefault(x, 0) + 1);
                    break;
                case 2:
                    System.out.println(map.firstKey());
                    break;
                case 3:
                    System.out.println(map.lastKey());
                    break;
                case 4:
                    if (!map.isEmpty()) {
                        int min = map.firstKey();//获取键
                        int count = map.get(min);
                        if (count == 1) {
                            map.remove(min);
                        } else {
                            map.put(min, count - 1);
                        }
                    }
                    break;
                case 5:
                    if (!map.isEmpty()) {
                        int max = map.lastKey();//获取键
                        int count = map.get(max);
                        if (count == 1) {
                            map.remove(max);
                        } else {
                            map.put(max, count - 1);
                        }
                    }
                    break;
            }
        }
    }
}