#include #include #include #include using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int N; if (!(cin >> N)) return 0; // 接頭辞のハッシュ出現回数を記録するマップ unordered_map hash_count; const unsigned long long BASE = 10007; // 固定のBase(狙い撃ちされやすい) // 空文字列(ハッシュ値 0)の初期件数は N hash_count[0] = N; for (int i = 0; i < N; ++i) { string A; cin >> A; unsigned long long h = 0; for (char c : A) { h = h * BASE + c; hash_count[h]++; } } int Q; cin >> Q; // S のハッシュ値の履歴(末尾削除に対応するためスタックで管理) vector hash_history; hash_history.push_back(0); // 初期状態(空文字列) while (Q--) { int type; cin >> type; if (type == 1) { char x; cin >> x; unsigned long long next_hash = hash_history.back() * BASE + x; hash_history.push_back(next_hash); } else if (type == 2) { hash_history.pop_back(); } else if (type == 3) { unsigned long long current_hash = hash_history.back(); auto it = hash_count.find(current_hash); if (it != hash_count.end()) { cout << it->second << "\n"; } else { cout << 0 << "\n"; } } } return 0; }