// editor5
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct Node{
    int id;
    int priority;
    struct Node* next;
};
struct Node* head = NULL;
void insert(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 || 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(){
    if(head == NULL){
        printf("Queue is empty\n");
        return 0;
    }
    struct Node* temp = head;
    printf("%d\n",head->id);
    head = head=>next;
    free(temp);
}
int main(){
    inbr N;
    scanf("%d",&N);
    if(N<=0){
        printf("Invalid input");
        return 0;
    }
    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);
            if(id <= 0 || priority <= 0){
                printf("Invalid input");
                return 0;
            }
            insert(id,priority);
        }
        else if(strcmp(op, "DELETE") == 0){
            delete();
        }
        else{
            printf("Invalid input");
            return 0;
        }
    }
    return 0;
}