#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct Node {
    int id,priority;
    struct node *next;
} Node;
node *head = NULL;
void insert(int id,int p) {
    Node *n = malloc(sizeof(Node0)),*t = head,*prev = NULL;
    n->id = id;n->priority = p;n->next = NULL;
    while( t && t-> priority >=p) { prev = t; t=t->next; }
    if(!prev) { n->next = head;head = n; }
    else { n-> next = prev->next;prev->next = n; }
}
void delete() {
    if(!head) puts("queue is empty");
    else { printf("%d\n",head->id);Node *t = head; head = head->next; free(t); }
}
int main() {
    int N; scanf("%d",&N);
    if(N <= 0) { puts("Invalid input");
    return 0; }
    while(N--) {
        char op[10]; scanf("%s",op);
        if(!strcmp(op,"INSERT")) { int id,p; scanf("%d%d",&id,&p); insert(id,p);
        else delete ();
    }
return 0;
}