#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 insertPatient(int id,int priority){
    if(id<=0 || priority<0 || priority>100){
        printf("invalid input\n");
        return;
    }
    struct Node*newNode=createNode(id,priority);
    if(head==NULL || head->priority<priority){
        newNode->next=head;
        head=newNode;
        return;
    }
    struct Node*temp=head;
    while(temp->next !=NULL && temp->next->priority>=priority)
    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;
    prntf("%d\n",temp->id);
    head=head->next;
    free(temp);
}
int main(){
    int n;
    if(scanf("%d",&N)!=1 || N<1 || N>1000){
        printf("Invalid input\n");
        return 0;
    }
    for(int i=0;i<N;i++){
        char operaion[10];
        scanf("%s",operation);
        
        if(strcmp(operation,'INSERT')==0){
            int id,priority;
            if(scanf("%d %d",&id,&priority)!=2){
                printf("Invalid input\n");
                return 0;
            }
            insertPatient(id,priority);
        }
        else if(strcmp(operation,'DELETE')==0){
            deletePatient();
        }else{
            printf("Invalid input\n";
            retun 0;
        }
    }
    return 0;
}