#include using namespace std; #ifndef ONLINE_JUDGE #include "algo/debug.h" #define debug(x...) cerr << "[" << #x << "] = ["; _print(x) #else #define debug(x...) #endif #define int long long void solve() { int n, q; cin >> n >> q; set> spec, nor; map rat; while (q--) { int op; cin >> op; if (op == 1) { string s; int x; cin >> s >> x; nor.insert({x, s}); rat[s] = x; } else if (op == 2) { int x; cin >> x; n -= x; } else { string s; int x; cin >> s >> x; n += x; auto it = (nor.find({rat[s], s})); if (it != nor.end()) { nor.erase(it); spec.insert({rat[s], s}); } } n = min(n, (int)1e9); while (nor.size() + spec.size() > n) { if (nor.size()) { cout << (nor.begin()->second) << "\n"; nor.erase(nor.begin()); continue; } if (spec.size()) { cout << (spec.begin() ->second) << "\n"; spec.erase(spec.begin()); continue; } } } } signed main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #else #endif ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t = 1; //cin >> t; while (t--) { solve(); } // cerr << "Time elapsed: " << ((long double)clock() / CLOCKS_PER_SEC) << " s.\n"; }