#include #include #include #include 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 normal, setuped; map mp; vector go_back; auto remove_min = [&] (map& mp) { auto it = mp.lower_bound(-1); go_back.push_back(it->second); mp.erase(it); }; 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]); } } for (auto name : go_back) { cout << name << "\n"; } }