#include <bits/stdc++.h>
using namespace std;
const int MBIT = 30;
struct lowbit_AC {
  int son[300010][26], end[300010], fail[300010];
  int root[MBIT], size[MBIT], bitcnt, alloc;
  string str[300010];
  int strcnt;
  void init() {
    alloc = strcnt = bitcnt = 0;
    memset(root, 0, sizeof(root));
    memset(size, 0, sizeof(size));
  }
  int newnode() {
    ++alloc;
    memset(son[alloc], 0, sizeof(son[alloc]));
    end[alloc] = fail[alloc] = 0;
    return alloc;
  }
  void build(int rt) {
    queue<int> q;
    fail[rt] = rt;
    for (int i = 0; i < 26; ++i) {
      if (!son[rt][i])
        son[rt][i] = rt;
      else
        fail[son[rt][i]] = rt, q.push(son[rt][i]);
    }
    while (!q.empty()) {
      int x = q.front();
      q.pop();
      end[x] += end[fail[x]];
      for (int i = 0; i < 26; ++i) {
        if (!son[x][i])
          son[x][i] = son[fail[x]][i];
        else
          fail[son[x][i]] = son[fail[x]][i], q.push(son[x][i]);
      }
    }
  }
  void rebuild(int l, int r, int &rt) {
    rt = newnode();
    for (int i = l; i <= r; ++i) {
      int now = rt;
      for (int j = 0, len = str[i].length(); j < len; ++j) {
        if (son[now][str[i][j] - 'a'] == 0)
          son[now][str[i][j] - 'a'] = newnode();
        now = son[now][str[i][j] - 'a'];
      }
      end[now]++;
    }
    build(rt);
  }
  void insert(char *s) {
    str[++strcnt] = string(s);
    size[++bitcnt] = 1;
    root[bitcnt] = newnode();
    while (bitcnt >= 2 && size[bitcnt] == size[bitcnt - 1]) size[--bitcnt] *= 2;
    alloc = root[bitcnt] - 1;
    rebuild(strcnt - size[bitcnt] + 1, strcnt, root[bitcnt]);
  }
  long long query(char *s, int rt) {
    int now = rt;
    long long res = 0;
    for (int i = 0; s[i]; ++i) {
      now = son[now][s[i] - 'a'];
      res += end[now];
    }
    return res;
  }
  long long ask(char *s) {
    long long res = 0;
    for (int i = 1; i <= bitcnt; ++i) {
      res += query(s, root[i]);
    }
    return res;
  }
} ac, de;
char s[300010];
int main() {
  int n;
  scanf("%d", &n);
  ac.init();
  de.init();
  for (int i = 0; i < n; ++i) {
    int opt;
    scanf("%d", &opt);
    scanf("%s", s);
    if (opt == 1)
      ac.insert(s);
    else if (opt == 2)
      de.insert(s);
    else
      printf("%lld\n", ac.ask(s) - de.ask(s)), fflush(stdout);
  }
}
