#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct Node
{
    int id;
    int priority;
    struct Node*next;
};
struct Node*head=NULL;
void insertpatient(int id,int priority)
{
    struct Node*newnode=(struct Node*)malloc(sizeof(struct Node));
    newnode->id=id;
    newnode->next=NULL;
    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 deletepatient()
    {
        if(head==NULL)
        {
            printf("Queue is empty\n");
            return;
        }
        struct Node*temp=head;
        printf("%d\n",temp->id);
        head=head->next;
        free(temp);
    }

int main()
{
    int n;
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
        char operation[10];
        scanf("%s",operation);
        if(strcmp(operation,"INSERT")==0)
        {
        int id,priority;
        scanf("%d %d"&id,&priority);
        if(priority<0)
        {
            printf("Invalid input");
            return 0;
        }
        
        insertpatient(id,priority);
    }
    else if(strcmp(operation,"DELETE")==0)
    {
        deletepatient();
    }
}
    return 0;
}