#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct node{
    int id,prio;
    struct node*next;
};
int main()
{
    int N;
    scanf("%d",&N);
    if(N<0){
        printf("Invalid input");
        return 0;
    }
    else{
    struct node*head=NULL;
    char op[10];
    while(N--)
    {
        scanf("%s",op);
        if(strcmp(op,"INSERT")==0){
            int id,pri;
            scanf("%d %d",&id,&pri);
            struct node*new=(struct node*)malloc(sizeof(struct node));
            new->id=id;
            new->prio=pri;
            new->next=NULL;
            if(!head||head->prio<pri){
                new->next=head;
                head=new;
            }
            else{
                struct node*curr=head;
                while(curr->next&&curr->next->prio>=pri){
                    curr=curr->next;
                }
                new->next=curr->next;
                curr->next=new;
            }
        }
        else if(strcmp(op,"DELETE")==0){
            if(!head){
                printf("Queue is empty\n");
            }
            else{
                printf("%d\n",head->id);
                struct node*temp=head;
                head=head->next;
                free(temp);
            }
        }
    }
    
    
    return 0;
}