#include <bits/stdc++.h>
using namespace std;
const int N = 300005;
const int BZ = sqrt(N * 1.0);
vector<string> vs;
int ch[N][26], val[N], top = 1;
int nnext[N];
int main(int argc, const char *argv[]) {
  int q;
  cin >> q;
  while (q--) {
    int op;
    string s;
    cin >> op >> s;
    int len = (int)s.size();
    switch (op) {
      case 1:
        if (len <= BZ) {
          int now = 0;
          for (auto x : s) {
            int c = x - 'a';
            if (!ch[now][c]) {
              ch[now][c] = top++;
            }
            now = ch[now][c];
          }
          val[now]++;
        } else {
          vs.push_back(s);
        }
        break;
      case 2:
        if (len <= BZ) {
          int now = 0;
          for (auto x : s) {
            int c = x - 'a';
            now = ch[now][c];
          }
          val[now]--;
        } else {
          for (auto it = vs.begin(); it != vs.end(); it++) {
            if (*it == s) {
              vs.erase(it);
              break;
            }
          }
        }
        break;
      case 3:
        long long ans = 0;
        for (int i = 0; i < (int)s.size(); i++) {
          int now = 0;
          for (int j = i; j < min(i + BZ, (int)s.size()); j++) {
            int c = s[j] - 'a';
            if (!ch[now][c]) {
              now = 0;
              break;
            }
            now = ch[now][c];
            ans += val[now];
          }
        }
        for (auto t : vs) {
          if (t.size() > s.size()) {
            continue;
          }
          nnext[0] = -1;
          int fail = -1;
          for (int i = 0; i < (int)t.size(); i++) {
            while (fail != -1 && t[i] != t[fail]) {
              fail = nnext[fail];
            }
            nnext[i + 1] = ++fail;
          }
          fail = 0;
          for (int i = 0; i < (int)s.size(); i++) {
            while (fail != -1 && s[i] != t[fail]) {
              fail = nnext[fail];
            }
            fail++;
            if (fail >= (int)t.size()) {
              fail = nnext[fail];
              ans++;
            }
          }
        }
        cout << ans << endl;
        fflush(stdout);
        break;
    }
  }
  return 0;
}
