#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct node {
    int id, p;
    struct node *next;
};
struct node *head = NULL;
void insert(int id, int p) {
    if(p < 1) { printf("Invalid input");
    exit(0); }
    struct node*n = malloc(sizeof(struct node));
    n->id = id; n->p = p; n->next = NULL;
    if (head || p > head->P) {
        n->next = head;
        head = n;
    } else {
        struct node *t = head;
        while (t->next && t-> next->p >= p)
        t = t->next;
        n->next = t->next;
        t->next = n;
    }
}
void del() {
    if (!head) {
        printf("Queue is empty\n");
        return;
    }
    printf("%d\n",head->id);
    head = head->next;
}
int main() {
    int n, id, p;
    char op[10];
    scanf("%d", &n);
    while (n--) {
        scanf("%s", op);
        if (!strcmp(op, "INSERT")) {
            scanf("%d %d", &id, &p);
            insert(id,p);
        } else if (!strcmp(op, "DELETE"))
        {
            del();
        }
    }
    return 0;
}