#include <bits/stdc++.h>
using namespace std;
const int MAXN = 3e5 + 20;
const int p_pow = 31;
const int mod = 1e9 + 9;
int q, op, n;
char s[MAXN];
set<long long> hashes[MAXN];
long long has[MAXN];
long long p[MAXN];
int len[MAXN];
inline long long convert(char ch) { return (long long)(ch - 'a' + 1); }
int main() {
  p[0] = 1;
  for (int i = 0; i < (int)MAXN; ++i) p[i] = (1LL * p[i - 1] * p_pow) % mod;
  scanf("%d", &q);
  while (q--) {
    scanf("%d %s", &op, s + 1);
    n = strlen(s + 1);
    if (op == 1) {
      long long ha = 0;
      for (int i = 1; i <= n; i++)
        ha = 1ll * (1ll * ha * p_pow % mod + convert(s[i])) % mod;
      hashes[n].insert(ha);
      ++len[n];
    }
    if (op == 2) {
      long long ha = 0;
      for (int i = 1; i <= n; i++)
        ha = 1ll * (1ll * ha * p_pow % mod + convert(s[i])) % mod;
      hashes[n].erase(ha);
      --len[n];
    }
    if (op == 3) {
      has[0] = 0;
      for (int i = 1; i <= n; i++)
        has[i] = 1ll * (1ll * has[i - 1] * p_pow % mod + convert(s[i])) % mod;
      int k = 1;
      long long ans = 0;
      for (int l = 1; l <= n; ++l) {
        k = 1ll * k * p_pow % mod;
        if (!len[l]) continue;
        for (int j = 0; j <= n - l; j++) {
          long long ha = 1ll * (has[l + j] - 1ll * has[j] * k % mod) % mod;
          ha = (ha % mod + mod) % mod;
          ans += (hashes[l].find(ha) != hashes[l].end());
        }
      }
      printf("%lld\n", ans);
      fflush(stdout);
    }
  }
}
