#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct node{
    int id;
    int priority;
    struct node*next;
};
struct node*createnode(int id,int priority){
    struct node*newnode=(struct node*)malloc(sizeof(struct node));
    newnode->id=id;
    newnode->priority=priority;
    newnode->next=NULL;
    return newnode;
}
void insert(struct node**head,int id,int priority){
    struct node*newnode=createnode(id,priority);
    if(*head==NULL||priority>(*head)->priority){
        newnode->next=*head;
        *head=newnode;
        return;
    }
    struct node*temp=*head;
    while(temp->next!=NULL && temp->next->priority>=priority){
        temp=temp->next;
    }
    newnode->next=temp->next;
    temp->next=newnode;
}
void delete(struct node**head){
    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);
    struct node*head=NULL;
    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(&head,id,priority);
        }
        else if(strcmp(operation,"DELETE")==0){
            delete(&head);
        }
    }
    return 0;
}