#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->next->priority>=priority){
            temp=temp->next;
        }
        newnode->next=newnode;
        temp->next=newnode;
    }
}
void deletee(){
    if(head==NULL){
        printf("Queue is empty\n");
        return;
    }
    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(strcmp(operation,"DELETE")==0){
            deletee();
        }
    }
    return o;
}