#include <bits/stdc++.h>
using namespace std;
int q, ans;
char s[300010];
set<int> len, hs[300010];
int p[300010], v[300010];
int main() {
  int i, l, op;
  for (i = 1, p[0] = 1; i <= 300000; ++i)
    p[i] = 1LL * p[i - 1] * 27 % 1000000007;
  scanf("%d", &q);
  while (q--) {
    scanf("%d%s", &op, s + 1);
    l = strlen(s + 1);
    for (i = 1, v[0] = 0; i <= l; ++i)
      v[i] = (1LL * v[i - 1] * 27 + s[i] - 'a' + 1) % 1000000007;
    if (op == 1) {
      len.insert(l);
      hs[l].insert(v[l]);
    } else if (op == 2)
      hs[l].erase(v[l]);
    else {
      ans = 0;
      for (auto it = len.begin(); it != len.end(); it++)
        for (i = *it; i <= l; i++)
          ans += (hs[*it].find((v[i] - 1LL * v[i - *it] * p[*it] % 1000000007 +
                                1000000007) %
                               1000000007) != hs[*it].end());
      printf("%d\n", ans);
      fflush(stdout);
    }
  }
  return 0;
}
