#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <set>
#include <utility>
using namespace std;

typedef unsigned long long ull;

// BASE: 29,53,131,257
// MOD: 636094623231363827,348051774975651917,140814840257324803,71777214294589669

int main() {
  ios::sync_with_stdio(false);
  int N; cin >> N;
  vector<pair<string, int>> S(N);
  for(int i = 0; i < N; ++i) {
    cin >> S[i].first;
    S[i].second = i;
  }
  sort(S.begin(), S.end());
  vector<int> M(N);
  for(int i = 0; i < N; ++i) {
    M[S[i].second] = i;
  }
  vector<vector<ull>> hash(N);
  for(int i = 0; i < N; ++i) {
    ull h = 0ULL;
    for(int j = 0; j < (int)S[i].first.length(); ++j) {
      h *= 29;
      h += (S[i].first[j] - 'a');
      h %= 636094623231363827;
      hash[i].push_back(h);
    }
  }
  set<int> D;
  int Q; cin >> Q;
  for(int q = 0; q < Q; ++q) {
    int k, id; cin >> k >> id;
    --id;
    if(k == 1) {
      D.insert(M[id]);
    }
    else if(k == 2) {
      D.erase(M[id]);
    }
    else {
      bool ok = false;
      auto succ = D.lower_bound(M[id]);
      if(succ != D.end() && S[M[id]].first.size() <= S[*succ].first.size() && hash[M[id]][S[M[id]].first.size() - 1] == hash[*succ][S[M[id]].first.size() - 1]) ok = true;
      if(ok) {
        cout << S[*succ].second + 1 << endl;
      }
      else {
        cout << -1 << endl;
      }
    }
  }
}
