#include<stdio.h>
#include<stdlib.h>
#include<string.h>

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){

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 " ,temp ->id);
    *head = (*head)->next;
    free(temp);
}

int main(){
    int N;
    scanf("%d", &N);
    struct Node*head = NULL;
    
    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);
            insert(&head, id ,priority);
        }
        else if(strcmp(operation,"DELETE")==0){
            delete(&head);
        }
    }
    return 0;
}