#include #include using namespace std; using ll = long long; #define rep(i, s, t) for (ll i = s; i < (ll)(t); i++) template bool chmin(T &x, T y) { return x > y ? (x = y, true) : false; } template bool chmax(T &x, T y) { return x < y ? (x = y, true) : false; } struct io_setup { io_setup() { ios::sync_with_stdio(false); std::cin.tie(nullptr); cout << fixed << setprecision(15); } } io_setup; using mint = atcoder::modint1000000007; int main(){ int nw,q; cin>>nw>>q; set> st,ust; map mp; vector ans; rep(lp,0,q){ int t; cin>>t; if(t==1){ string s; int r; cin>>s>>r; cin.ignore(); mp[s]=r; st.insert({r,s}); if(nw>0) nw--; else{ auto itr=st.begin(); assert(itr!=st.end()); ans.push_back(itr->second); st.erase(itr); } }else if(t==2){ int x; cin>>x; cin.ignore(); set> rt; rep(llp,0,x){ if(nw>0) nw--; else{ auto itr=st.begin(); if(itr==st.end()){ auto uitr=ust.begin(); assert(uitr!=ust.end()); rt.insert(*uitr); ust.erase(uitr); }else{ rt.insert(*itr); st.erase(itr); } } } for(auto[r,s]:rt) ans.push_back(s); }else if(t==3){ string s; int x; cin>>s>>x; cin.ignore(); if(st.count({mp[s],s})){ st.erase({mp[s],s}); ust.insert({mp[s],s}); } assert(ust.count({mp[s],s})); nw+=x; } } for(auto s:ans) cout<