#include <bits/stdc++.h>
using namespace std;
int trie[300000][26];
int fail[300000];
int out[300000];
set<int> mark;
int findNextState(int currentState, char nextInput) {
  int answer = currentState;
  int ch = nextInput - 'a';
  while (trie[answer][ch] == -1) answer = fail[answer];
  return trie[answer][ch];
}
int main() {
  int m;
  scanf("%d", &m);
  memset(trie, -1, sizeof trie);
  memset(fail, -1, sizeof fail);
  int t, states = 1, s = 0;
  char line[300000 + 5];
  bool ok = true;
  for (int i = 0; i < 26; i++)
    if (trie[0][i] == -1) trie[0][i] = 0;
  while (m--) {
    scanf("%d%s", &t, line);
    if (t == 1) {
      for (int i = 0; i < 26; i++)
        if (trie[0][i] == 0) trie[0][i] = -1;
      int idx = 0;
      for (int i = 0; line[i]; i++) {
        int c = line[i] - 'a';
        if (trie[idx][c] == -1) trie[idx][c] = states++;
        idx = trie[idx][c];
      }
      for (int i = 0; i < 26; i++)
        if (trie[0][i] == -1) trie[0][i] = 0;
      mark.insert(idx);
      ok = false;
    } else if (t == 2) {
      int idx = 0;
      for (int i = 0; line[i]; i++) {
        int c = line[i] - 'a';
        idx = trie[idx][c];
      }
      mark.erase(idx);
      ok = false;
    } else {
      if (!ok) {
        ok = true;
        memset(out, 0, sizeof out);
        for (set<int>::iterator it = mark.begin(); it != mark.end(); ++it)
          out[*it]++;
        queue<int> q;
        for (int i = 0; i < 26; i++) {
          if (trie[0][i] != 0) {
            q.push(trie[0][i]);
            fail[trie[0][i]] = 0;
          }
        }
        while (q.size()) {
          int state = q.front();
          q.pop();
          for (int c = 0; c < 26; c++) {
            if (trie[state][c] != -1) {
              int failure = fail[state];
              while (trie[failure][c] == -1) failure = fail[failure];
              failure = trie[failure][c];
              fail[trie[state][c]] = failure;
              out[trie[state][c]] += out[failure];
              q.push(trie[state][c]);
            }
          }
        }
      }
      int nd = 0;
      int cnt = 0;
      for (int i = 0; line[i]; i++) {
        nd = findNextState(nd, line[i]);
        cnt += out[nd];
      }
      printf("%d\n", cnt);
      fflush(stdout);
    }
  }
  return 0;
}
