#include #include using ll = long long; using namespace std; int main(){ int n; string str; map want[21]; cin >> n; for(int i=0;i> flag; if(flag==0){ int place,m; cin >> place >> m; for(int j=0;j> str; want[place][str]++; } } else if(flag==1){ cin >> str; bool check=true; for(int j=1;j<=20;j++){ if(want[j][str]>0){ want[j][str]--; cout << j << endl; check=false; break; } } if(check) cout << -1 << endl; } else{ int place; cin >> place; want[place].clear(); } } return 0; }