#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; mp[s]=r; st.insert({r,s}); if(nw>0) nw--; else{ auto itr=st.begin(); ans.push_back(itr->second); st.erase(itr); } }else if(t==2){ int x; cin>>x; 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()); ans.push_back(uitr->second); ust.erase(uitr); }else{ ans.push_back(itr->second); st.erase(itr); } } } }else if(t==3){ string s; int x; cin>>s>>x; if(st.count({mp[s],s})){ st.erase({mp[s],s}); ust.insert({mp[s],s}); } nw+=x; } } for(auto s:ans) cout<