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