#define _USE_MATH_DEFINES #include "bits/stdc++.h" using namespace std; #define FOR(i,j,k) for(int (i)=(j);(i)<(int)(k);++(i)) #define rep(i,j) FOR(i,0,j) #define each(x,y) for(auto &(x):(y)) #define mp make_pair #define MT make_tuple #define all(x) (x).begin(),(x).end() #define debug(x) cout<<#x<<": "<<(x)<; using vi = vector; using vll = vector; int N, no[21]; void solve() { cin >> N; map > S; rep(i, N) { int U; cin >> U; if (U == 0) { int n, m; cin >> n >> m; rep(j, m) { string t; cin >> t; S[t].insert(n); } } else if (U == 1) { int ans = -1; string t; cin >> t; while (sz(S[t]) > 0 && ans == -1) { auto it = S[t].begin(); if (no[*it]) { S[t].erase(*it); } else { ans = *it; S[t].erase(it); } } cout << ans << endl; } else { int n; cin >> n; no[n] = 1; } } } int main() { ios::sync_with_stdio(false); cin.tie(0); cout << fixed << setprecision(15); solve(); return 0; }