#include <bits/stdc++.h>
using namespace std;
const int maxm = 1e6 + 9;
const int maxn = 1e6 + 9;
const int N = 1e5;
const long long inf = 1e18;
const int base = 131;
const int mod = 1e9 + 7;
const double eps = 1e-7;
int T, opt, n;
char s[maxn];
struct AC {
  int ch[maxn][26], fail[maxn], val[maxn], tot;
  bitset<maxn> ed;
  queue<int> q;
  int sum[32], rt[32], h;
  void clear() {
    memset(fail, 0, sizeof(fail));
    tot = h = 0;
    memset(val, 0, sizeof(val));
    ed.reset();
    memset(rt, 0, sizeof(rt));
  }
  void get_fail(int u) {
    while (!q.empty()) q.pop();
    q.push(u);
    while (!q.empty()) {
      int now = q.front();
      q.pop();
      for (int i = (0); i <= (25); ++i) {
        if (!ch[now][i]) continue;
        int t = fail[now];
        while (t && !ch[t][i]) t = fail[t];
        fail[ch[now][i]] = t ? ch[t][i] : u;
        q.push(ch[now][i]);
        val[ch[now][i]] = val[fail[ch[now][i]]] + ed[ch[now][i]];
      }
    }
  }
  int merge(int u, int v) {
    if (!u || !v) return u + v;
    ed[u] = (ed[u] | ed[v]);
    for (int i = (0); i <= (25); ++i) ch[u][i] = merge(ch[u][i], ch[v][i]);
    return u;
  }
  void insert() {
    rt[++h] = ++tot;
    int u = tot;
    for (int i = (1); i <= (n); ++i) {
      if (!ch[u][s[i] - 'a']) ch[u][s[i] - 'a'] = ++tot;
      u = ch[u][s[i] - 'a'];
    }
    sum[h] = ed[u] = 1;
    get_fail(rt[h]);
    while (sum[h - 1] == sum[h] && h) {
      merge(rt[h - 1], rt[h]);
      get_fail(rt[h - 1]);
      sum[h - 1] += sum[h];
      h--;
    }
  }
  int query() {
    int ret = 0;
    for (int i = (1); i <= (h); ++i) {
      for (int now = rt[i], t, j = 1; j <= n; ++j) {
        t = s[j] - 'a';
        while (now && !ch[now][t]) now = fail[now];
        now = now ? ch[now][t] : rt[i];
        ret += val[now];
      }
    }
    return ret;
  }
} A, B;
int main() {
  scanf("%d", &T);
  A.clear();
  B.clear();
  while (T--) {
    scanf("%d %s", &opt, s + 1);
    n = strlen(s + 1);
    if (opt == 1)
      A.insert();
    else if (opt == 2)
      B.insert();
    else if (opt == 3)
      printf("%d", A.query() - B.query()), putchar('\n'), fflush(stdout);
  }
  return 0;
}
