#include <bits/stdc++.h>
using namespace std;
const int MAX = 3e5 + 5;
int n;
int qu[MAX];
char b[MAX];
struct AC {
  int tot, Top, Siz;
  int st[MAX], fail[MAX], val[MAX], Val[MAX], rt[21], siz[21];
  int son[MAX][26], _son[MAX][26];
  AC() { tot = Top = Siz = 0; }
  int BRING_OUT() { return Top ? st[Top--] : ++tot; }
  int GET_POINT() {
    int k = BRING_OUT();
    for (int i = 0; i < 26; ++i) son[k][i] = _son[k][i] = 0;
    return val[k] = fail[k] = Val[k] = 0, k;
  }
  void merge(int &x, int &y) {
    if (!x || !y) return void(x += y);
    st[++Top] = y;
    for (int i = 0; i < 26; ++i)
      merge(son[x][i], son[y][i]), son[y][i] = _son[y][i] = 0;
    Val[x] += Val[y];
  }
  void ins(int k, int _L) {
    int x = rt[k];
    for (int i = 1, d; i <= _L; ++i)
      if (son[x][b[i] - 'a'])
        x = son[x][b[i] - 'a'];
      else
        d = son[x][b[i] - 'a'] = GET_POINT(), x = d;
    ++Val[x];
  }
  void GET_FAIL(int k) {
    int hl = 1, tl = 0;
    for (int i = 0; i < 26; ++i)
      if (son[rt[k]][i])
        _son[rt[k]][i] = qu[++tl] = son[rt[k]][i], fail[son[rt[k]][i]] = rt[k];
      else
        _son[rt[k]][i] = rt[k];
    while (hl <= tl) {
      int tt = qu[hl++];
      for (int i = 0; i < 26; ++i)
        if (son[tt][i])
          fail[_son[tt][i] = son[tt][i]] = _son[fail[tt]][i],
                             qu[++tl] = son[tt][i];
        else
          _son[tt][i] = _son[fail[tt]][i];
      val[tt] = Val[tt] + val[fail[tt]];
    }
  }
  long long ask(int _L) {
    long long ans = 0;
    for (int i = 1; i <= Siz; ++i)
      for (int j = 1, x = rt[i]; j <= _L; ++j)
        x = _son[x][b[j] - 'a'], ans += val[x];
    return ans;
  }
  void Ins(int _L) {
    siz[++Siz] = 1, rt[Siz] = GET_POINT(), ins(Siz, _L);
    while (Siz > 1 && siz[Siz] == siz[Siz - 1])
      merge(rt[Siz - 1], rt[Siz]), siz[Siz - 1] <<= 1, --Siz;
    GET_FAIL(Siz);
  }
} _A, __A;
int main() {
  scanf("%d", &n);
  for (int i = 1, op, _n; i <= n; ++i) {
    scanf("%d%s", &op, b + 1), _n = strlen(b + 1);
    if (op == 1)
      _A.Ins(_n);
    else if (op == 2)
      __A.Ins(_n);
    else if (op == 3)
      printf("%I64d\n", _A.ask(_n) - __A.ask(_n)), fflush(stdout);
  }
  return 0;
}
