#include <bits/stdc++.h>
using namespace std;
const long long bas = 999983;
const int mod = 998244353;
char s[300100];
long long has[300100], p[300100];
long long ans;
int n, m, is[300100], st[2000];
set<long long> se[300100];
long long get(int l, int r) {
  return (has[r] - (has[l - 1] * p[r - l + 1]) % mod + mod) % mod;
}
int main() {
  int i, j, k, len, op, top = 0;
  long long ha, vl;
  scanf("%d", &m);
  p[0] = 1;
  for (i = 1; i <= 300000; i++) p[i] = (p[i - 1] * bas) % mod;
  while (m--) {
    scanf("%d%s", &op, s + 1);
    n = strlen(s + 1);
    ha = 0;
    if (op == 1) {
      if (!is[n]) st[++top] = n;
      is[n]++;
      for (i = 1; i <= n; i++) ha = (ha * bas + (long long)s[i]) % mod;
      se[n].insert(ha);
    } else if (op == 2) {
      for (i = 1; i <= n; i++) ha = (ha * bas + (long long)s[i]) % mod;
      se[n].erase(ha);
    } else {
      ans = 0;
      for (i = 1; i <= n; i++)
        has[i] = (has[i - 1] * bas + (long long)s[i]) % mod;
      for (i = 1; i <= top; i++) {
        len = st[i];
        if (len > n) break;
        for (j = len; j <= n; j++) {
          vl = get(j - len + 1, j);
          if (se[len].find(vl) != se[len].end()) ans++;
        }
      }
      printf("%I64d\n", ans);
      fflush(stdout);
    }
  }
  return 0;
}
