#include using namespace std; #define rep(i, n) for (int i = 0; i < n; i++) #define rep2(i, x, n) for (int i = x; i <= n; i++) #define rep3(i, x, n) for (int i = x; i >= n; i--) #define each(e, v) for (auto &e : v) #define pb push_back #define eb emplace_back #define all(x) x.begin(), x.end() #define rall(x) x.rbegin(), x.rend() #define sz(x) (int)x.size() using ll = long long; using pii = pair; using pil = pair; using pli = pair; using pll = pair; template bool chmax(T &x, const T &y) { return (x < y) ? (x = y, true) : false; } template bool chmin(T &x, const T &y) { return (x > y) ? (x = y, true) : false; } template int flg(T x, int i) { return (x >> i) & 1; } template void print(const vector &v, T x = 0) { int n = v.size(); for (int i = 0; i < n; i++) cout << v[i] + x << (i == n - 1 ? '\n' : ' '); if (v.empty()) cout << '\n'; } template void printn(const vector &v, T x = 0) { int n = v.size(); for (int i = 0; i < n; i++) cout << v[i] + x << '\n'; } template int lb(const vector &v, T x) { return lower_bound(begin(v), end(v), x) - begin(v); } template int ub(const vector &v, T x) { return upper_bound(begin(v), end(v), x) - begin(v); } template void rearrange(vector &v) { sort(begin(v), end(v)); v.erase(unique(begin(v), end(v)), end(v)); } template vector id_sort(const vector &v, bool greater = false) { int n = v.size(); vector ret(n); iota(begin(ret), end(ret), 0); sort(begin(ret), end(ret), [&](int i, int j) { return greater ? v[i] > v[j] : v[i] < v[j]; }); return ret; } template pair operator+(const pair &p, const pair &q) { return make_pair(p.first + q.first, p.second + q.second); } template pair operator-(const pair &p, const pair &q) { return make_pair(p.first - q.first, p.second - q.second); } template istream &operator>>(istream &is, pair &p) { S a; T b; is >> a >> b; p = make_pair(a, b); return is; } template ostream &operator<<(ostream &os, const pair &p) { return os << p.first << ' ' << p.second; } struct io_setup { io_setup() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout << fixed << setprecision(15); } } io_setup; const int inf = (1 << 30) - 1; const ll INF = (1LL << 60) - 1; const int MOD = 1000000007; // const int MOD = 998244353; template struct Binary_Indexed_Tree { vector bit; const int n; Binary_Indexed_Tree(const vector &v) : n((int)v.size()) { bit.resize(n + 1); copy(begin(v), end(v), begin(bit) + 1); for (int a = 2; a <= n; a <<= 1) { for (int b = a; b <= n; b += a) bit[b] += bit[b - a / 2]; } } Binary_Indexed_Tree(int n, const T &x) : n(n) { bit.resize(n + 1); vector v(n, x); copy(begin(v), end(v), begin(bit) + 1); for (int a = 2; a <= n; a <<= 1) { for (int b = a; b <= n; b += a) bit[b] += bit[b - a / 2]; } } void add(int i, const T &x) { for (i++; i <= n; i += (i & -i)) bit[i] += x; } void change(int i, const T &x) { add(i, x - query(i, i + 1)); } T sum(int i) const { T ret = 0; for (; i > 0; i -= (i & -i)) ret += bit[i]; return ret; } T query(int l, int r) const { return sum(r) - sum(l); } T operator[](int i) const { return query(i, i + 1); } int lower_bound(T x) const { int ret = 0; for (int k = 31 - __builtin_clz(n); k >= 0; k--) { if (ret + (1 << k) <= n && bit[ret + (1 << k)] < x) x -= bit[ret += (1 << k)]; } return ret; } int upper_bound(T x) const { int ret = 0; for (int k = 31 - __builtin_clz(n); k >= 0; k--) { if (ret + (1 << k) <= n && bit[ret + (1 << k)] <= x) x -= bit[ret += (1 << k)]; } return ret; } }; template struct Heavy_Light_Decomposition { struct edge { int to, id; edge(int to, int id) : to(to), id(id) {} }; vector> es; vector par, si, depth; vector root; // 属する連結成分の根 vector id_v, id_e; // 各頂点、各辺が一列に並べたときに何番目に相当するか(辺の番号は 1,2,...,n-1 となることに注意) const int n; int m; Heavy_Light_Decomposition(int n) : es(n), par(n), si(n), depth(n), root(n), id_v(n), id_e(n - 1), n(n), m(0) {} void add_edge(int from, int to) { es[from].emplace_back(to, m); if (!directed) es[to].emplace_back(from, m); m++; } int _dfs1(int now, int pre = -1) { par[now] = pre; if (pre == -1) depth[now] = 0; si[now] = 1; for (auto &e : es[now]) { if (e.to != pre) { depth[e.to] = depth[now] + 1; si[now] += _dfs1(e.to, now); } } return si[now]; } void _dfs2(int now, bool st, int &s, int pre = -1) { root[now] = (st ? now : root[pre]); id_v[now] = s++; edge heavy = {-1, -1}; int M = 0; for (auto &e : es[now]) { if (e.to == pre) continue; if (M < si[e.to]) M = si[e.to], heavy = e; } if (heavy.id != -1) { id_e[heavy.id] = s; _dfs2(heavy.to, false, s, now); } for (auto &e : es[now]) { if (e.to != pre && e.id != heavy.id) { id_e[e.id] = s; _dfs2(e.to, true, s, now); } } } void decompose(int root = 0) { _dfs1(root); int s = 0; _dfs2(root, true, s); } vector> get_path(int u, int v, bool use_edge = false) { // パスに対応する区間たちを列挙 vector> ret; while (root[u] != root[v]) { if (depth[root[u]] > depth[root[v]]) swap(u, v); ret.emplace_back(id_v[root[v]], id_v[v] + 1); v = par[root[v]]; } if (depth[u] > depth[v]) swap(u, v); ret.emplace_back(id_v[u] + use_edge, id_v[v] + 1); return ret; } vector> get_path_noncommutative(int u, int v, bool use_edge = false) { // クエリが非可換の場合 vector> l, r; while (root[u] != root[v]) { if (depth[root[u]] > depth[root[v]]) { l.emplace_back(id_v[u] + 1, id_v[root[u]]); u = par[root[u]]; } else { r.emplace_back(id_v[root[v]], id_v[v] + 1); v = par[root[v]]; } } if (depth[u] > depth[v]) { l.emplace_back(id_v[u] + 1, id_v[v] + use_edge); } else { r.emplace_back(id_v[u] + use_edge, id_v[v] + 1); } reverse(begin(r), end(r)); for (auto &e : r) l.push_back(e); return l; } }; int main() { int N; cin >> N; vector S(N); rep(i, N) cin >> S[i]; int Q; cin >> Q; vector t(Q), id(Q); vector c(Q); vector T = S; rep(i, Q) { cin >> t[i] >> id[i]; id[i]--; if (t[i] == 1) { cin >> c[i]; T[id[i]] += c[i]; } } // rep(i, N) cout << T[i] << '\n'; vector> ptr(N); int K = 1; vector> chi(1, vector(26, -1)); rep(i, N) { int now = 0; each(e, S[i]) { int x = e - 'a'; if (chi[now][x] == -1) { chi[now][x] = K; chi.eb(26, -1); K++; } now = chi[now][x]; ptr[i].eb(now); } } vector a(K, 0); rep(i, N) { int L = sz(S[i]); rep(j, L) a[ptr[i][j]]++; } // cout << K << '\n'; Heavy_Light_Decomposition G(K); rep(i, K) { rep(j, 26) { if (chi[i][j] != -1) G.add_edge(i, chi[i][j]); } } G.decompose(); vector b(K, 0); rep(i, K) b[G.id_v[i]] = a[i]; Binary_Indexed_Tree bit(b); vector L(N); rep(i, N) L[i] = sz(S[i]) - 1; rep(i, Q) { int e = id[i]; if (t[i] == 1) { int p = ptr[e][++L[e]]; bit.add(G.id_v[p], 1); } else { int u = 0, v = ptr[e][L[e]]; ll ans = 0; for (auto e : G.get_path(u, v)) ans += bit.query(e.first, e.second); cout << ans << '\n'; } } }