#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 5;
int n, type;
string s;
struct Dynamic_Aho {
  struct AhoCorasick {
    struct node {
      int child[26], nex[26], link, sz, val;
    } ept;
    vector<node> trie;
    void reset() {
      trie.clear();
      trie.push_back(ept);
    }
    void add(string s) {
      int now = 0;
      for (int i = 0; i < s.length(); i++) {
        int x = s[i] - 97;
        if (trie[now].child[x] == 0) {
          trie.push_back(ept);
          trie[now].child[x] = trie.size() - 1;
        }
        now = trie[now].child[x];
      }
      trie[now].sz++;
    }
    void bfs() {
      queue<int> q;
      q.push(0);
      while (q.size() > 0) {
        int u = q.front();
        q.pop();
        trie[u].val = trie[trie[u].link].val + trie[u].sz;
        for (int i = 0; i < 26; i++) {
          int v = trie[u].child[i];
          trie[u].nex[i] = trie[trie[u].link].nex[i];
          if (v != 0) {
            assert(v < trie.size());
            trie[v].link = trie[u].nex[i];
            trie[u].nex[i] = v;
            q.push(v);
          }
        }
      }
    }
  } Aho[20];
  vector<string> block[20];
  void add_str(string s) {
    int idx = -1;
    for (int i = 0; i <= 19; i++) {
      if (block[i].size() == 0) {
        idx = i;
        break;
      }
    }
    Aho[idx].reset();
    Aho[idx].add(s);
    block[idx].push_back(s);
    for (int i = 0; i < idx; i++) {
      for (auto tmp : block[i]) {
        Aho[idx].add(tmp);
        block[idx].push_back(tmp);
      }
      block[i].clear();
      Aho[i].reset();
    }
    Aho[idx].bfs();
  }
} Ins, Del;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> type >> s;
    if (type == 1) {
      Ins.add_str(s);
    }
    if (type == 2) {
      Del.add_str(s);
    }
    if (type == 3) {
      int ans = 0;
      for (int i = 0; i <= 19; i++) {
        if (Ins.Aho[i].trie.size() <= 1) continue;
        int now = 0;
        for (int j = 0; j < s.length(); j++) {
          int x = s[j] - 97;
          now = Ins.Aho[i].trie[now].nex[x];
          ans += Ins.Aho[i].trie[now].val;
        }
      }
      for (int i = 0; i <= 19; i++) {
        if (Del.Aho[i].trie.size() <= 1) continue;
        int now = 0;
        for (int j = 0; j < s.length(); j++) {
          int x = s[j] - 97;
          now = Del.Aho[i].trie[now].nex[x];
          ans -= Del.Aho[i].trie[now].val;
        }
      }
      cout << ans << endl;
    }
  }
}
