#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct Node{
    int id;
    int pr;
    struct Node *next;
}Node;
Node* make_node(int id,int pr){
    Node *p=(Node*)malloc(sizeof(Node));
    if(!p)
    exit(1);
    p->id =id;p->pr =pr;p->next =NULL;
    return p;
}
Node* insert_sorted(Node *head,int id,int pr){
    Node *newn =make_node(id,pr);
    if(head ==NULL ||pr >head->pr){
        newn ->next =head;
        return newn;
    }
    Node *cur =head;
    while(cur ->next !=NULL && cur->next->pr >=pr){
        cur=cur->next;
    }
    newn->next =cur->next;
    cur ->next =newn;
    return head;
}
Node* delete_first(Node *head,int *rid,int *found){
    if(head ==NULL){
        *found =0;
        return head;
    }
    Node *tmp =head;
    *rid =tmp->id;
    *found =1;
    head =head ->next;
    free(tmp);
    return head;
}
int main(void){
    int N;
    if(scanf("%d",&N)!=1)return 0;
    Node *head =NULL;
    char cmd[16];
    for(int i=0;i<N;++i){
        if(scanf("%15s",cmd)!=1)break;
        if(strcmp(cmd,"INSERT")==0){
            int id,pr;
            if(scanf("%d%d",&id,&pr)!=2){
                return 0;
            }
            head =insert_sorted(head,id,pr);
        }else if(strcmp(cmd,"DELETE")==0){
            int remove_id,found;
            head =delete_first(head,&remove_id,&found);
            if(!found){
                printf("Queue is empty\n");
            }else{
                printf("%d\n",removed_id);
            }
        }
            else{
                int c;
                while((c=getchar())!='\n'&& c!=EOF){}
            }
        }
        Node *cur =head;
        while(cur){
            Node *nx =cur->next;
            free(cur);
            cur =nx;
        }
        return 0;
    }