#include <bits/stdc++.h>
using namespace std;
int n, m, a[300010][27];
int val[300010], cost[610];
int fail[300010];
string st[610];
char s[300010], p[300010];
int main() {
  int T, op, len, slen, i, j, k, x;
  long long ans;
  for (scanf("%d", &T); T; --T) {
    scanf("%d %s", &op, s);
    len = strlen(s);
    if (op <= 2) {
      if (len <= 500) {
        for (i = 0, j = 0; i < len; ++i) {
          if (!a[j][x = s[i] - 'a']) a[j][x] = ++m;
          j = a[j][x];
        }
        val[j] += op == 1 ? 1 : -1;
      } else {
        st[++n] = s;
        cost[n] = op == 1 ? 1 : -1;
      }
    } else {
      ans = 0;
      for (i = 0; i < len; ++i)
        for (k = i, j = 0; k < len; ++k) {
          if (!a[j][x = s[k] - 'a']) break;
          j = a[j][x];
          ans += val[j];
        }
      for (i = 1; i <= n; ++i) {
        if ((slen = st[i].length()) > len) continue;
        fail[0] = fail[1] = 0;
        for (j = 1; j < slen; ++j) {
          for (k = fail[j]; k && st[i][k] != st[i][j]; k = fail[k])
            ;
          fail[j + 1] = st[i][k] == st[i][j] ? k + 1 : 0;
        }
        for (j = 0, k = 0; j < len; ++j) {
          while (k && st[i][k] != s[j]) k = fail[k];
          if (st[i][k] == s[j]) ++k;
          if (k == slen) {
            ans += cost[i];
            k = fail[k];
          }
        }
      }
      cout << ans << endl;
    }
  }
  return 0;
}
