#include<stdio.h>
#include<stdlib.h
#include<string.h>
struct Node{
    int id;
    int priority;
    struct Node* next;
};
struct Node* head=NULL;
struct Node* createNode(int id,int priority){
    struct Node* newNode =(struct Node*)malloc(sizeof(struct Node));
    newNode->id=id;
    newNode->priority=priority;
    newNode->next =NULL;
    return newNode;
}
void insert(int id,int priority){
    struct Node* newNode= createNode(id,priority);
    if(head==NULL || head->priority<priority){
        newNode->next=head;
        head=newNode;
    }
    else{
        struct Node* temp=head;
        while(temp->next !=NULL && temp->next->priority.=priority){
            temp=temp->next;
        }
        newNode->next=temp->next;
        temp->next=newNode;
    }
}
void delete(){
    if(head==NULL){
        printf("Quesue is empty/n");
        return;
    }
    struct Node*temp=head;
    printf("%d\n",temp->id);
    head=head->next;
    free(temp);
}
int main(){
    int n;
    if(scanf("%d",&n)!=1 || n<=0){
        printf("Invalid input\n");
        return 0;
    }
    for(int i=0;i<n;i++){
        char command[10];
        scanf("%s",command);
        if(strcmp(command,"INSERT")==0){
            int id,priority;
            if(scanf("%d %d",&id,&priority)!=2){
                printf("Invalid input\n");
                return 0;
            }
            insert(id,priority);
        }
        else if(strcmp(command,"DELETE")==0){
            delete();
        }
        else{
            printf("Invalid input\n");
            return 0;
        }
    }
    return 0;
}