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