#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct node{
    int id;
    int prior;
    struct node* next;
};
struct node* head=NULL;
void insert(int id,int prior){
    struct node* newnode =(struct node*)maloc(sizeof(struct node));
    newnode->id=id;
    newnode->prior=-prior;
    newnode->next=NULL;
    if(head==NULL||head->prior<prior){
        newnode->next=head;
        head=newnode;
    }
    else{
        struct node* temp=head;
        while(temp->next=NULL&&temp->next->prior>=prior){
            temp=temp->next;
        }
        newnode->next=temp->next;
        temp->next=newnode;
    }
}
void deletepatient(){
    if(head==NULL){
        printf("Queue is empty\n");
        return;
        
    }
    struct node* temp=head;
    printf("%d\n",temp->id);
    head=head->next;
    free(temp);
}
int main(){
    int n;
    scanf("%d",&n);
    for(int i=0;i<n;i++){
        char op[10];
        scanf("%s",op);
        if(strcmp(op,"INSERT")==0){
            int id,prior;
            scanf("%d%d",&id,&prior);
            if(prior<0){
                printf("Invalid input\n");
                return 0;
                
            }
            insert(id,prior);
        }
        else if(strcmp(op,"DELETE")==0){
            deletepatient();
        }
    }
    return 0;
}