#include <bits/stdc++.h>
using namespace std;
inline void read(int& x) {
  char c11 = getchar();
  x = 0;
  while (!isdigit(c11)) c11 = getchar();
  while (isdigit(c11)) x = x * 10 + c11 - '0', c11 = getchar();
}
const int N = 601000;
struct node {
  int ch[26], fail, end, sum;
  inline void clear() {
    for (int i = 0; i < 26; ++i) ch[i] = 0;
    fail = end = sum = 0;
  }
} p[N];
struct RECYCLE {
  int stk[N], top, tot;
  RECYCLE() { top = tot = 0; }
  inline int newnode() { return top ? stk[top--] : ++tot; }
  inline void del(int x) {
    stk[++top] = x;
    p[x].clear();
  }
} re;
struct AC_Auto {
  int rt;
  void clear() { p[rt = re.newnode()].fail = 0; }
  inline void ins(char* a) {
    int nw = rt;
    for (int i = 0, x; a[i]; ++i) {
      x = a[i] - 'a';
      if (!p[nw].ch[x]) p[nw].ch[x] = re.newnode();
      nw = p[nw].ch[x];
    }
    ++p[nw].end;
  }
  void build() {
    static int q[N], he, ta;
    q[he = ta = 1] = rt;
    while (he <= ta) {
      int x = q[he++];
      for (int i = 0, c, t; i < 26; ++i) {
        c = p[x].ch[i], t = p[x].fail;
        if (!c) continue;
        q[++ta] = c;
        while (t && !p[t].ch[i]) t = p[t].fail;
        if (t && p[t].ch[i] != c)
          p[c].fail = p[t].ch[i];
        else
          p[c].fail = rt;
        p[c].sum = p[c].end + p[p[c].fail].sum;
      }
    }
  }
  long long scan(char* s) {
    int nw = rt;
    long long res = 0;
    for (int i = 0, x; s[i]; ++i) {
      x = s[i] - 'a';
      while (nw != rt && !p[nw].ch[x]) nw = p[nw].fail;
      if (p[nw].ch[x]) nw = p[nw].ch[x];
      res += p[nw].sum;
    }
    return res;
  }
  void merge(int& x, int y) {
    if (!y) return;
    if (!x) x = re.newnode();
    for (int i = 0; i < 26; ++i) merge(p[x].ch[i], p[y].ch[i]);
    p[x].end += p[y].end;
    re.del(y);
  }
};
struct NODE {
  int tot, num[20];
  AC_Auto ac[20];
  NODE() {
    tot = 0;
    memset(num, 0, sizeof num);
  }
  void INS(char* a) {
    num[++tot] = 1;
    ac[tot].clear();
    ac[tot].ins(a);
    while (tot > 1)
      if (num[tot] == num[tot - 1]) {
        ac[tot - 1].merge(ac[tot - 1].rt, ac[tot].rt);
        num[tot - 1] += num[tot];
        num[tot] = 0, --tot;
      } else
        break;
    ac[tot].build();
  }
  long long MAT(char* a) {
    long long res = 0ll;
    for (int i = 1; i <= tot; ++i) res += ac[i].scan(a);
    return res;
  }
} A, B;
char s[N];
int main() {
  int m, op;
  read(m);
  while (m--) {
    read(op);
    scanf("%s", s);
    if (op == 1)
      A.INS(s);
    else if (op == 2)
      B.INS(s);
    else
      printf("%lld\n", A.MAT(s) - B.MAT(s)), fflush(stdout);
  }
  return 0;
}
