#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#define MAX_INPUT 100
typedef struct patient{
    int id;
    int priority;
    struct patient*next;
}Patient;
Patient*head=NULL;
void insert_patient(int id,int priority){
    Patient*new_patient=(Patient*)malloc(sizeof(Patient));
    new_patient->id=id;
    new_patient->priority=priority;
    new_patient->next=NULL;
    
    if(!head||priority>head->priority){
        new_patient->next=head;
        head=new_patient;
    }else{
        Patient*current=head;
        while(current->next&&(current->next->priority>priority||(current->next->priority==priority))){
            current=current->next;
        }
        new_patient->next=current->next;
        current->next=new_patient;
    }
}
void delete_patient(){
    if(!head){
        printf("Queue is empty\n");
        return;
    }
    Patient*temp=head;
    printf("%d\n",temp->id);
    head=head->next;
    free(temp);
}
int main(){
    int N;
    scanf("%d",&N);
    getchar();
    
    char operation[MAX_INPUT];
    for(int i=0;i<N;i++){
        fgets(operation,sizeof(operation),stdin);
        if(strncmp(operation,"INSERT",6)==0){
            int ifd,priority;
            if(sscanf(operation,"INSERT%d %d",&d,&id,&priority)==2){
                insert_patient(id,priority);
            }else{
                printf("Invalid input\n");
                return 0;
            }
        }else if(strncmp(operation,"DELETE",6)==0){
            delete_patient();
        }else{
            printf("Invalid input\n");
            return 0;
        }
    }
    return 0;
}