#include <bits/stdc++.h>
using namespace std;

void fast_io() {
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
}

int main() {
	fast_io();
	int n, q;
	cin >> n >> q;
	map<string, int> rate;
	set<pair<int, string>> st_fixed, st_not_fixed;
	for (; q--;) {
		int type;
		cin >> type;
		if (type == 1) {
			string s;
			int r;
			cin >> s >> r;
			rate[s] = r;
			st_not_fixed.insert({r, s});
			if (st_fixed.size() + st_not_fixed.size() > n) {
				auto it = st_not_fixed.begin();
				cout << it->second << "\n";
				st_not_fixed.erase(it);
			}
		} else if (type == 2) {
			int x;
			cin >> x;
			n -= x;
			vector<string> removed;
			while (st_fixed.size() + st_not_fixed.size() > n) {
				if (!st_not_fixed.empty()) {
					auto it = st_not_fixed.begin();
					removed.push_back(it->second);
					st_not_fixed.erase(it);
				} else {
					auto it = st_fixed.begin();
					removed.push_back(it->second);
					st_fixed.erase(it);
				}
			}
			sort(removed.begin(), removed.end(),
				 [&](string s1, string s2) { return rate[s1] < rate[s2]; });
			for (string s : removed) {
				cout << s << "\n";
			}
		} else {
			string s;
			int x;
			cin >> s >> x;
			n += x;
			if (st_not_fixed.count({rate[s], s})) {
				st_fixed.insert({rate[s], s});
				st_not_fixed.erase({rate[s], s});
			}
		}
	}
}