#include using namespace std; template struct invertible_binary_indexed_tree{ int N; vector BIT; function f; function inv; T E; invertible_binary_indexed_tree(){ } invertible_binary_indexed_tree(int N, function f, function inv, T E): N(N), BIT(N + 1, E), f(f), inv(inv), E(E){ } void add(int i, T x){ i++; while (i <= N){ BIT[i] = f(BIT[i], x); i += i & -i; } } T sum(int i){ T ans = E; while (i > 0){ ans = f(ans, BIT[i]); i -= i & -i; } return ans; } T sum(int l, int r){ return f(sum(r), inv(sum(l))); } }; struct heavy_light_decomposition{ vector p, sz, in, next; invertible_binary_indexed_tree BIT; void dfs1(vector> &c, int v){ sz[v] = 1; for (int &w : c[v]){ dfs1(c, w); sz[v] += sz[w]; if (sz[w] > sz[c[v][0]]){ swap(w, c[v][0]); } } } void dfs2(vector> &c, int &t, int v){ in[v] = t; t++; for (int w : c[v]){ if (w == c[v][0]){ next[w] = next[v]; } else { next[w] = w; } dfs2(c, t, w); } } heavy_light_decomposition(vector &p, vector> &c, int r = 0): p(p){ int N = p.size(); sz = vector(N); dfs1(c, r); in = vector(N); next = vector(N, r); int t = 0; dfs2(c, t, r); BIT = invertible_binary_indexed_tree(N, plus(), negate(), 0); } void add(int p, int x){ BIT.add(in[p], x); } int sum(int v){ int ans = 0; while (next[v] != 0){ ans += BIT.sum(in[next[v]], in[v] + 1); v = p[next[v]]; } ans += BIT.sum(0, in[v] + 1); return ans; } }; int main(){ int N; cin >> N; vector S(N); for (int i = 0; i < N; i++){ cin >> S[i]; } vector L(N); for (int i = 0; i < N; i++){ L[i] = S[i].size(); } int Q; cin >> Q; vector t(Q), x(Q); vector c(Q); for (int i = 0; i < Q; i++){ cin >> t[i]; if (t[i] == 1){ cin >> x[i] >> c[i]; x[i]--; S[x[i]] += c[i]; } if (t[i] == 2){ cin >> x[i]; x[i]--; } } int V = 1; vector> trie(V); for (int i = 0; i < N; i++){ int v = 0; int L2 = S[i].size(); for (int j = 0; j < L2; j++){ auto itr = trie[v].find(S[i][j]); int w; if (itr != trie[v].end()){ w = (*itr).second; } else { w = V; trie[v][S[i][j]] = V; trie.push_back({}); V++; } v = w; } } vector pr(V, -1); vector> ch(V); for (int i = 0; i < V; i++){ for (auto P : trie[i]){ pr[P.second] = i; ch[i].push_back(P.second); } } heavy_light_decomposition HLD(pr, ch); vector curr(N, 0); for (int i = 0; i < N; i++){ for (int j = 0; j < L[i]; j++){ curr[i] = trie[curr[i]][S[i][j]]; HLD.add(curr[i], 1); } } for (int i = 0; i < Q; i++){ if (t[i] == 1){ curr[x[i]] = trie[curr[x[i]]][L[x[i]]]; L[x[i]]++; HLD.add(curr[x[i]], 1); } if (t[i] == 2){ cout << HLD.sum(curr[x[i]]) << endl; } } }