#include using namespace std; using ll = long long; #define rep(i, n) for(int i = 0; i < (n); i++) #define rrep(i, n) for(int i = (n) - 1; i >= 0; i--) struct Vertex { int next[26]; int prv = -1; int count = 0; Vertex() { fill(begin(next), end(next),-1); } }; vector trie(1); void add_string(const string& s) { int v = 0; trie[v].count+=1; for(char ch: s) { int c = ch - 'a'; if (trie[v].next[c] == -1) { trie[v].next[c] = trie.size(); trie.emplace_back(); } v = trie[v].next[c]; trie[v].count += 1; } } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int N; cin >> N; for(int i = 0; i < N; i++) { string s; cin >> s; add_string(s); } int it = 0; int ans = trie[it].count; int Q; cin >> Q; while(Q--) { int op; cin >> op; if (op == 1) { char x; cin >> x; int c = x - 'a'; if (trie[it].next[c] == -1) { trie[it].next[c] = trie.size(); trie.emplace_back(); } trie[trie[it].next[c]].prv=it; it = trie[it].next[c]; } else if(op == 2) { it=trie[it].prv; } else { cout<