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