#include <algorithm>
#include <iostream>
#include <queue>
#include <unordered_map>
#include <vector>
using namespace std;

vector<int> clanPower;
unordered_map<int, queue<int>> waitingClanMembers;
int HC;
vector<int> waitingClanHeap;
vector<int> whereInHeap;

bool shouldGoHigher(int where, int other) {
    int mc = waitingClanHeap[where];
    int oc = waitingClanHeap[other];
    if (clanPower[mc] != clanPower[oc]) {
        return clanPower[mc] > clanPower[oc];
    } else {
        return mc < oc;
    }
}

void swapInHeap(int where, int other) {
    int mc = waitingClanHeap[where];
    int oc = waitingClanHeap[other];
    
    waitingClanHeap[where] = oc;
    waitingClanHeap[other] = mc;
    
    whereInHeap[oc] = where;
    whereInHeap[mc] = other;
}

void bubbleUp(int where) {
    while (true) {
        if (where == 0) break;
        int parent = (where - 1) / 2;
        if (!shouldGoHigher(where, parent)) break;
        swapInHeap(where, parent);
        where = parent;
    }
}

void bubbleDown(int where) {
    while (true) {
        int cand = where;
        for (int i = 1; i <= 2; ++i) {
            int child = 2 * where + i;
            if (child >= HC) break;
            if (shouldGoHigher(child, cand)) cand = child;
        }
        if (cand == where) break;
        swapInHeap(where, cand);
        where = cand;
    }
}

void addNewPerson(int id, int clan, int power) {
    if (whereInHeap[clan] == -1) {
        waitingClanHeap[HC] = clan;
        whereInHeap[clan] = HC;
        ++HC;
    }

    clanPower[clan] = max(clanPower[clan], power);
    bubbleUp(whereInHeap[clan]);
    waitingClanMembers[clan].push(id);
}

int getNextPatient() {
    if (HC == 0) return -1;
    // We know somebody is waiting
    int clan = waitingClanHeap[0];
    
    // In C++, queue pop() returns void, so we must store front first
    int person = waitingClanMembers[clan].front();
    waitingClanMembers[clan].pop();

    if (!waitingClanMembers[clan].empty()) {
        return person;
    }

    whereInHeap[clan] = -1;

    --HC;
    if (HC == 0) return person;

    int lastClan = waitingClanHeap[HC];
    waitingClanHeap[0] = lastClan;
    whereInHeap[lastClan] = 0;
    bubbleDown(0);
    return person;
}

int main() {
    clanPower.assign(200007, 0);
    waitingClanMembers.clear();
    HC = 0;
    waitingClanHeap.assign(200007, 0);
    whereInHeap.assign(200007, -1);
    int currID = -1;
    while (true) {
        int typ;
        cin >> typ;
        if (typ == 0) return 0;
        if (typ == 2) cout << getNextPatient() << "\n";
        if (typ == 1) {
            int power, clan;
            cin >> power >> clan;
            ++currID;
            addNewPerson(currID, clan, power);
        }
    }
}
