#include <stdio.h>
#include <stdlib.h>

#define MAXN 10000

int heap[MAXN];
int size = 0;

void swap(int *a, int *b) {
    int t = *a; *a = *b; *b = t;
}

void heapifyUp(int i) {
    while (i > 0 && heap[(i - 1) / 2] > heap[i]) {
        swap(&heap[(i - 1) / 2], &heap[i]);
        i = (i - 1) / 2;
    }
}

void heapifyDown(int i) {
    int smallest = i;
    int l = 2 * i + 1;
    int r = 2 * i + 2;

    if (l < size && heap[l] < heap[smallest]) smallest = l;
    if (r < size && heap[r] < heap[smallest]) smallest = r;

    if (smallest != i) {
        swap(&heap[i], &heap[smallest]);
        heapifyDown(smallest);
    }
}

void push(int x) {
    if (size >= MAXN) {
        /
        return;
    }
    heap[size++] = x;
    heapifyUp(size - 1);
}

int pop() {
    if (size == 0) return -1;
    int ret = heap[0];
    heap[0] = heap[size - 1];
    size--;
    heapifyDown(0);
    return ret;
}

int peek() {
    if (size == 0) return -1;
    return heap[0];
}


int removeLoad(int val) {
    int idx = -1;
    for (int i = 0; i < size; i++) {
        if (heap[i] == val) {
            idx = i;
            break;
        }
    }
    if (idx == -1) return 0;

    heap[idx] = heap[size - 1];
    size--;
    heapifyDown(idx);
    heapifyUp(idx);
    return 1;
}

int cmpInt(const void *a, const void *b) {
    int x = *(int *)a;
    int y = *(int *)b;
    return x - y;
}

int main() {
    int N;
    if (scanf("%d", &N) != 1 || N < 1) {
        printf("Invalid input\n");
        return 0;
    }

    int lastProcessedLoad = -1; /

    for (int i = 0; i < N; i++) {
        int op;
        if (scanf("%d", &op) != 1) {
            printf("Invalid input\n");
            return 0;
        }
        if (op == 1) {
            int x;
            if (scanf("%d", &x) != 1 || x < 0) {
                printf("Invalid input\n");
                return 0;
            }
            push(x);
        }
        else if (op == 2) {
            
            if (size == 0) {
              
                continue;
            }

            int candidate = -1;
            int candidateIdx = -1;
            for (int j = 0; j < size; j++) {
                if (heap[j] >= lastProcessedLoad) {
                    if (candidate == -1 || heap[j] < candidate) {
                        candidate = heap[j];
                        candidateIdx = j;
                    }
                }
            }

            if (candidate == -1) {
               
                continue;
            }

            heap[candidateIdx] = heap[size - 1];
            size--;
            heapifyDown(candidateIdx);
            heapifyUp(candidateIdx);

            lastProcessedLoad = candidate;
            printf("%d\n", candidate);
        }
        else if (op == 3) {
            if (size == 0) {
                printf("0\n");
            }
            else {
                
                int *arr = (int *)malloc(sizeof(int) * size);
                if (!arr) {
                    printf("Invalid input\n");
                    return 0;
                }
                for (int j = 0; j < size; j++) {
                    arr[j] = heap[j];
                }
                qsort(arr, size, sizeof(int), cmpInt);
                for (int j = 0; j < size; j++) {
                    printf("%d", arr[j]);
                    if (j != size - 1) printf(" ");
                }
                printf("\n");
                free(arr);
            }
        }
        else {
            printf("Invalid input\n");
            return 0;
        }
    }
    return 0;
}