#include <bits/stdc++.h>
using namespace std;
const int MAXN = 3e5 + 100;
const int CASE_SIZE = 26;
char str[MAXN];
struct Trie {
  int child[MAXN][CASE_SIZE], value[MAXN], trieN, root;
  void init() {
    trieN = root = 0;
    value[root] = 0;
    memset(child[root], -1, sizeof(child[root]));
  }
  int newnode() {
    trieN++;
    value[trieN] = 0;
    memset(child[trieN], -1, sizeof(child[trieN]));
    return trieN;
  }
  void insert(char *s, int val) {
    int x = root;
    for (int i = 0; s[i]; i++) {
      int d = s[i] - 'a';
      if (child[x][d] == -1) child[x][d] = newnode();
      x = child[x][d];
    }
    value[x] += val;
  }
  int search(char *s) {
    int sum = 0, x = root;
    for (int i = 0; s[i]; i++) {
      int d = s[i] - 'a';
      if (child[x][d] == -1) break;
      x = child[x][d];
      sum += value[x];
    }
    return sum;
  }
} trie;
struct KMP {
  int Next[MAXN];
  void getNext(string s, int len) {
    memset(Next, 0, sizeof(Next));
    int i = 0, j = -1;
    Next[0] = -1;
    while (i < len) {
      if (j == -1 || s[i] == s[j]) {
        ++i;
        ++j;
        Next[i] = j;
      } else
        j = Next[j];
    }
  }
  int search(string s, char *str) {
    int sum = 0, j = 0, lens = s.length(), lenstr = strlen(str);
    getNext(s, s.length());
    for (int i = 0; i < lenstr; i++) {
      while (j > 0 && s[j] != str[i]) {
        j = Next[j];
      }
      if (s[j] == str[i]) j++;
      if (j == lens) {
        sum++;
        j = Next[j];
      }
    }
    return sum;
  }
} kmp;
string cun[MAXN];
int flag[MAXN];
int main() {
  int n;
  while (scanf("%d", &n) != EOF) {
    int cnt = 0;
    memset(flag, 0, sizeof(flag));
    trie.init();
    for (int i = 1; i <= n; i++) {
      int d;
      scanf("%d %s", &d, str);
      int len = strlen(str);
      if (d == 1) {
        if (len <= 1000)
          trie.insert(str, 1);
        else
          cun[++cnt] = string(str), flag[cnt] = 1;
      }
      if (d == 2) {
        if (len <= 1000)
          trie.insert(str, -1);
        else
          cun[++cnt] = string(str), flag[cnt] = -1;
      }
      if (d == 3) {
        long long ans = 0;
        for (int i = 0; i < len; i++) ans += trie.search(str + i);
        for (int i = 1; i <= cnt; i++) {
          if (len < cun[i].length()) continue;
          ans += kmp.search(cun[i], str) * flag[i];
        }
        printf("%I64d\n", ans);
        fflush(stdout);
      }
    }
  }
}
