#include <bits/stdc++.h>
using namespace std;
template <typename T1, typename T2>
inline T1 max(T1 a, T2 b) {
  return a < b ? b : a;
}
template <typename T1, typename T2>
inline T1 min(T1 a, T2 b) {
  return a < b ? a : b;
}
const char lf = '\n';
const int _ = 300007, alp = 26;
struct acmata {
  int fa[_], ch[_][alp], th[_][alp], tcnt;
  long long val[_], sval[_];
  int merges(int a, int b) {
    if (!a || !b) return a | b;
    val[a] += val[b];
    for (int i = 0; i < alp; i++) ch[a][i] = merges(ch[a][i], ch[b][i]);
    return a;
  }
  int rt[_], sz[_], rtcnt;
  void make(int now) {
    queue<int> q;
    fa[now] = now, sval[now] = val[now];
    for (int i = 0; i < alp; i++) {
      if (ch[now][i])
        th[now][i] = ch[now][i], fa[ch[now][i]] = now, q.emplace(ch[now][i]);
      else
        th[now][i] = now;
    }
    while (!q.empty()) {
      int a = q.front();
      q.pop();
      sval[a] = val[a] + sval[fa[a]];
      for (int i = 0; i < alp; i++) {
        if (ch[a][i])
          th[a][i] = ch[a][i], fa[ch[a][i]] = th[fa[a]][i], q.emplace(ch[a][i]);
        else
          th[a][i] = th[fa[a]][i];
      }
    }
  }
  void push(int n, char *s) {
    int now = ++tcnt;
    rt[++rtcnt] = now, sz[rtcnt] = 1;
    for (int i = 0; i < n; i++) {
      int c = s[i] - 'a';
      if (!ch[now][c]) ch[now][c] = ++tcnt;
      now = ch[now][c];
    }
    val[now]++;
    while (rtcnt > 1 && sz[rtcnt] == sz[rtcnt - 1])
      sz[rtcnt - 1] += sz[rtcnt],
          rt[rtcnt - 1] = merges(rt[rtcnt - 1], rt[rtcnt]), rtcnt--;
    make(rt[rtcnt]);
  }
  long long finder(int n, char *s) {
    long long ans = 0;
    for (int i = 1; i <= rtcnt; i++) {
      int now = rt[i];
      for (int j = 0; j < n; j++) now = th[now][s[j] - 'a'], ans += sval[now];
    }
    return ans;
  }
  acmata() {
    memset(fa, 0, sizeof(fa));
    memset(ch, 0, sizeof(ch));
    memset(th, 0, sizeof(th));
    memset(val, 0, sizeof(val));
    memset(sval, 0, sizeof(sval));
    memset(sz, 0, sizeof(sz));
    memset(rt, 0, sizeof(rt));
    tcnt = 0, rtcnt = 0;
  }
} t, w;
int qn;
char temp[_];
int main() {
  ios::sync_with_stdio(0), cin.tie(nullptr), cout.tie(nullptr);
  cin >> qn;
  for (int qq = 1; qq <= qn; qq++) {
    int a, b;
    cin >> a >> temp, b = strlen(temp);
    if (a == 1) t.push(b, temp);
    if (a == 2) w.push(b, temp);
    if (a == 3) cout << t.finder(b, temp) - w.finder(b, temp) << endl;
  }
  return 0;
}
