#include <bits/stdc++.h> // NeOWami
using namespace std;
#define ft first
#define sc second
#define int long long
using pii = pair<int, int>;
const int N = 1e6 + 5;
const int A = 2e6 +6;
int q;
struct query {
int t, id;
string s;
} Q[N];
namespace sub1 {
using HASH = unsigned long long;
using pih = pair<int, HASH>;
const HASH BASE = 3;
HASH T[A], POW[A];
HASH getHash(int l, int r) {
return T[r] - T[l - 1] * POW[r - l + 1];
}
bool checksub() {
int cnt1 = 0, cnt2 = 0;
for (int i = 1; i <= q; i++) {
if (Q[i].t == 0) cnt1++;
else cnt2 += (int)Q[i].s.size();
}
return (long long) cnt1 * cnt2 <= 200000000;
}
void solve() {
vector<pih> S;
POW[0] =1;
for (int i = 1; i < A; i++) POW[i] = POW[i - 1] * BASE;
for (int id = 1; id <= q; id++) {
if (Q[id].t == 0) {
int n = Q[id].s.size();
HASH h = 0;
for (char c: Q[id].s) {
h = h * BASE + (c - '0' + 1);
}
S.push_back({n, h});
}
else {
int n = Q[id].s.size();
for (int i = 1; i <= n; i++) T[i] = T[i - 1] * BASE + (Q[id].s[i - 1] -'0' + 1);
int ans = 0;
for (pih item: S) {
for (int i = 1; i + item.ft - 1 <= n; i++) {
if (getHash(i, i + item.ft - 1) == item.sc) ans++;
}
}
cout << ans << "\n";
}
}
}
};
namespace sub2{
//Chep Code Aho
struct AhoCorasick {
static const int SIGMA = 2;
int base;
struct Node {
array<int, SIGMA> next{};
int link = 0;
int out = 0;
Node() { next.fill(-1); }
};
vector<Node> t;
vector<int> term_node, bfs_order;
AhoCorasick(int baseChar = 'a') : base(baseChar) {
t.emplace_back();
t[0].link = 0;
}
int add(const string& s) {
int v = 0;
for (char ch : s) {
int c = ch - base;
if (c < 0 || c >= SIGMA) {
continue;
}
if (t[v].next[c] == -1) {
t[v].next[c] = (int)t.size();
t.emplace_back();
}
v = t[v].next[c];
}
t[v].out += 1;
term_node.push_back(v);
return (int)term_node.size() - 1;
}
void build() {
queue<int> q;
for (int c = 0; c < SIGMA; ++c) {
int u = t[0].next[c];
if (u != -1) {
t[u].link = 0;
q.push(u);
} else {
t[0].next[c] = 0;
}
}
bfs_order.clear();
while (!q.empty()) {
int v = q.front(); q.pop();
bfs_order.push_back(v);
int link = t[v].link;
t[v].out += t[link].out;
for (int c = 0; c < SIGMA; ++c) {
int u = t[v].next[c];
if (u != -1) {
t[u].link = t[link].next[c];
q.push(u);
} else {
t[v].next[c] = t[link].next[c];
}
}
}
}
long long count_total(const string& s) const {
long long ans = 0;
int v = 0;
for (char ch : s) {
int c = ch - base;
if (c < 0 || c >= SIGMA) { v = 0; continue; }
v = t[v].next[c];
ans += t[v].out;
}
return ans;
}
};
int ans[N];
void DnC(int l, int r){
if(l >= r) return;
int mid = l + r >> 1;
AhoCorasick ac('0');
vector<int> updates, queries;
for(int i = l; i <= mid; ++i){
if(Q[i].t == 0) ac.add(Q[i].s);
}
ac.build();
for(int i = mid + 1; i <= r; ++i){
if(Q[i].t == 1) ans[i] += ac.count_total(Q[i].s);
}
DnC(l, mid);
DnC(mid + 1, r);
}
void solve() {
DnC(1, q);
for (int i = 1; i <= q; i++) if (Q[i].t == 1) cout << ans[i] << '\n';
}
};
signed main() {
cin.tie(NULL)->sync_with_stdio(false); cout.tie(NULL);
if(ifstream("VIRUS.inp")) {
freopen("VIRUS.inp", "r", stdin);
freopen("VIRUS.out", "w", stdout);
}
cin >> q;
for (int i = 1; i <= q; i++) {
cin >> Q[i].t >> Q[i].s;
Q[i].id = i;
}
// if (sub1::checksub()) return sub1::solve(), 0;
return sub2::solve(), 0;
return 0;
}