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