#include <iostream>
#include <vector>
#include <map>
#include <string>
#include <algorithm>

using namespace std;
using ll = long long;

constexpr int iINF = 1'000'000'000;
constexpr ll llINF = 1'000'000'000'000'000'000;

int main () {
    int N, Q; cin >> N >> Q;
    map<int, string> normal, setuped;
    map<string, int> mp;
    vector<string> go_back;

    vector<pair<int, string>> buf;

    auto remove_min = [&] (map<int, string>& mp) {
        auto it = mp.lower_bound(-1);
        buf.push_back(make_pair(it->first, it->second));
        mp.erase(it);
    };

    auto clear_buf = [&] () {
        sort(buf.begin(), buf.end(), [&](pair<int, string>& a, pair<int, string>& b) { return a.first < b.first; });
        for (auto v : buf) go_back.push_back(v.second);
        buf.resize(0);
    };

    for (int i = 0; i < Q; i++) {
        int t; cin >> t;
        if (t == 1) {
            string s;
            int r;
            cin >> s >> r;
            normal[r] = s;
            mp[s] = r;

            if (N < normal.size() + setuped.size()) {
                remove_min(normal);
            }
        }

        if (t == 2) {
            int x; cin >> x;
            N -= x;
            while (N < normal.size() + setuped.size()) {
                if (0 < normal.size()) {
                    remove_min(normal);
                    continue;
                }
                remove_min(setuped);
            }
        }

        if (t == 3) {
            string s;
            int x;
            cin >> s >> x;
            N += x;
            setuped[mp[s]] = s;
            normal.erase(mp[s]);
        }

        clear_buf();
    }

    for (auto name : go_back) {
        cout << name << "\n";
    }
}