#include <bits/stdc++.h>
using namespace std;
const int TRIE_LEN = 300000;
const int LOG_TRIE_LEN = 20;
int N, t;
string S;
struct Automata {
  map<int, map<int, int>> trie;
  int fail_out[TRIE_LEN];
  bool end_word[TRIE_LEN];
  vector<string> dict;
  int nodes = 1;
  bool built = false;
  void clear() {
    nodes = 1;
    built = false;
    memset(fail_out, 0, sizeof(fail_out));
    memset(end_word, 0, sizeof(end_word));
    trie.clear();
  }
  void build_bfs() {
    queue<int> q;
    q.push(0);
    int failure[TRIE_LEN] = {0};
    while (!q.empty()) {
      int u = q.front();
      q.pop();
      for (int i = 0; i < 26; i++) {
        int v = trie[u][i];
        if (!v)
          trie[u][i] = trie[failure[u]][i];
        else
          q.push(v);
        if (!u || !v) continue;
        failure[v] = trie[failure[u]][i];
        fail_out[v] = end_word[failure[v]] ? failure[v] : fail_out[failure[v]];
      }
    }
  }
  void build() {
    clear();
    built = true;
    for (string &s : dict) {
      int cur = 0;
      for (char c : s) {
        int cc = c - 'a';
        if (!trie[cur][cc]) trie[cur][cc] = nodes++;
        cur = trie[cur][cc];
      }
      end_word[cur] = true;
    }
    build_bfs();
  }
  int check(string &s) {
    int cur = 0, ans = 0;
    for (char c : s) {
      int cc = c - 'a';
      cur = trie[cur][cc];
      int f = cur;
      while (f) {
        if (end_word[f]) ans++;
        f = fail_out[f];
      }
    }
    return ans;
  }
};
Automata insertions[LOG_TRIE_LEN], deletions[LOG_TRIE_LEN];
void process(Automata da[]) {
  vector<string> dict;
  dict.push_back(S);
  for (int i = 0; i < LOG_TRIE_LEN; i++) {
    if (!da[i].dict.size()) {
      std::move(dict.begin(), dict.end(), std::back_inserter(da[i].dict));
      break;
    } else {
      std::move(da[i].dict.begin(), da[i].dict.end(), std::back_inserter(dict));
      da[i].dict.clear();
      da[i].built = false;
    }
  }
}
int count(Automata da[]) {
  int ans = 0;
  for (int i = 0; i < LOG_TRIE_LEN; i++) {
    if (da[i].dict.size()) {
      if (!da[i].built) da[i].build();
      ans += da[i].check(S);
    }
  }
  return ans;
}
int main() {
  std::ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  ;
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> t >> S;
    if (t == 1)
      process(insertions);
    else if (t == 2)
      process(deletions);
    else {
      cout << count(insertions) - count(deletions) << "\n";
      cout.flush();
    }
  }
  return 0;
}
