#include using namespace std; constexpr int mod[] = {998244353, 1000000007}; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int N; cin >> N; map, int> mp; vector ans(N); vector tmp(N); vector>> hs(N, vector>(2, vector(1, 0))); vector S(N); for (int i = 0; i < N; i++) { cin >> S[i]; for (int j = 0; j < S[i].size(); j++) { for (int k = 0; k < 2; k++) { hs[i][k].push_back((27ll * hs[i][k][j] + S[i][j] - 'a' + 1) % mod[k]); } mp[{hs[i][0][j+1], hs[i][1][j+1]}]++; } } for (int i = 0; i < N; i++) { for (int j = 0; j < S[i].size(); j++) { ans[i] += mp[{hs[i][0][j+1], hs[i][1][j+1]}]; } } vector> res; int Q; cin >> Q; while (Q--) { int f; cin >> f; if (f == 1) { int x; char c; cin >> x >> c; x--; S[x] += c; res.push_back({x,hs[x][0].size()}); for (int j = 0; j < 2; j++) { hs[x][j].push_back((27ll * hs[x][j][hs[x][j].size()-1] + c - 'a' + 1) % mod[j]); } mp[{hs[x][0][hs[x][0].size() - 1], hs[x][1][hs[x][1].size() - 1]}]++; if (res.size() % 500 == 0) { for (int i = 0; i < N; i++) { ans[i] = 0; for (int j = 0; j < S[i].size(); j++) { ans[i] += mp[{hs[i][0][j+1], hs[i][1][j+1]}]; } } } } else { int x; cin >> x; x--; long long nans = ans[x]; for(int i = res.size()/500*500; i < res.size(); i++) { if(hs[x][0].size() <= res[i].second) { continue; } nans += (hs[x][0][res[i].second] == hs[res[i].first][0][res[i].second] && hs[x][1][res[i].second] == hs[res[i].first][1][res[i].second]); } cout << nans << '\n'; } } }