#include using namespace std; using dict = unordered_map; int main() { cin.tie(0); ios::sync_with_stdio(false); vector order(21, dict()); int N; cin >> N; while (N--){ int type; cin >> type; if (type == 0){ int n, m; cin >> n >> m; while (m--){ string neta; cin >> neta; ++order[n][neta]; } }else if (type == 1){ string neta; cin >> neta; int who = -1; for (int seat = 1; seat <= 20; ++seat){ if (order[seat][neta] > 0){ --order[seat][neta]; who = seat; break; } } cout << who << '\n'; }else if (type == 2){ int seat; cin >> seat; order[seat].clear(); } } return 0; }