#include <bits/stdc++.h>
using namespace std;
const int sigma = 26;
const int logmax = 20;
string s_to_update;
struct trie {
  int cnt;
  trie *phi;
  trie *son[sigma];
  trie() {
    cnt = 0;
    phi = NULL;
    for (int i = 0; i < sigma; ++i) son[i] = NULL;
  }
  void update(int pos) {
    if (pos == s_to_update.size()) {
      cnt++;
      if (cnt != 1) cerr << "mda";
      return;
    }
    int to = s_to_update[pos] - 'a';
    if (son[to] == NULL) son[to] = new trie;
    son[to]->update(pos + 1);
  }
};
class aho_corasick {
  trie *root = new trie;

 public:
  int sz = 0;
  vector<string> v;
  void add(string s) {
    sz += s.size();
    v.push_back(s);
    s_to_update = s;
    root->update(0);
  }
  void compute_phi() {
    trie **q;
    q = new trie *[sz + 10];
    int dim = 0;
    q[++dim] = root;
    root->phi = root;
    for (int i = 1; i <= dim; ++i) {
      trie *node = q[i];
      node->cnt += node->phi->cnt;
      for (int to = 0; to < sigma; ++to) {
        if (node->son[to] == NULL) continue;
        trie *k = node->phi;
        while (k != root && k->son[to] == NULL) k = k->phi;
        if (k != node && k->son[to] != NULL) k = k->son[to];
        node->son[to]->phi = k;
        q[++dim] = node->son[to];
      }
    }
  }
  int compute_ans(string S) {
    int res = 0;
    trie *k = root;
    for (int i = 0; i < S.size(); ++i) {
      int to = S[i] - 'a';
      while (k != root && k->son[to] == NULL) k = k->phi;
      if (k->son[to] != NULL) k = k->son[to];
      res += k->cnt;
    }
    return res;
  }
  void reset() {
    sz = 0;
    v.clear();
    root = new trie;
  }
};
aho_corasick ac[2][logmax];
void update(string s, int idx) {
  int pos = -1;
  int sum = s.size();
  for (int i = 0; i < logmax && pos == -1; ++i) {
    sum += ac[idx][i].sz;
    if (sum <= (1 << i)) pos = i;
  }
  for (int i = 0; i < pos; ++i) {
    for (auto &it : ac[idx][i].v) ac[idx][pos].add(it);
    ac[idx][i].reset();
  }
  ac[idx][pos].add(s);
  ac[idx][pos].compute_phi();
}
int query(string s, int idx) {
  int res = 0;
  for (int i = 0; i < logmax; ++i) res += ac[idx][i].compute_ans(s);
  return res;
}
int main() {
  ios_base ::sync_with_stdio(false);
  int n;
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    int type;
    string s;
    cin >> type >> s;
    if (type != 3) update(s, type - 1);
    if (type == 3) cout << query(s, 0) - query(s, 1) << '\n';
  }
  return 0;
}
