#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct Node
{
    int id;
    int priority;
    struct Node*next;
};
struct Node*front=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(front==NULL||priority>front->priority)
    {
        newNode->next=front;
        front=newNode;
    }
    else
    {
        struct Node*temp=front;
        while(temp->next!=NULL &&temp->next->priority>=priority)
        {
            temp=temp->next;
        }
        newNode->next=temp->next;
        temp->next=newNode;
    }
}
void delete()
{
    if(front==NULL)
    {
        printf("Queue is empty\n");
        return ;
    }
    printf("%d\n",front->id);
    struct Node*temp=front;
    front=front->next;
    free(temp);
}
int main()
{
    int n;
    scanf("%d",&n);
    if(n<=0)
    {
        printf("Invalid input\n");
        return 0;
    }
    char command[10];
    int id,priority;
    for(int i=0;i<n;i++)
    {
       if (scanf("%s",command)!=1)
       {
           printf("Invalid input");
           return 0;
       }
        if(strcmp(command,"INSERT")==0)
        {
           if( scanf("%d %d",&id,&priority)!=2||id<0||priority<0)
           {
               printf("Invalid input")
               return 0;
           }
            insert(id,priority);
            }
            else if(strcmp(command,"DELETE")==0)
            {
                delete();
            }
                else
                {
                    printf("Invalid input\n");
                     return 0;
                }
            }
            return 0;
    }