#include <bits/stdc++.h>
using namespace std;
int cnt, value[310];
string st[310];
char str[300010];
struct Trie {
  int ch[300010][26], value[300010], siz, root;
  void init() {
    siz = root = 0;
    value[root] = 0;
    memset(ch[root], -1, sizeof(ch[root]));
  }
  int newnode() {
    siz++;
    value[siz] = 0;
    memset(ch[siz], -1, sizeof(ch[siz]));
    return siz;
  }
  void insert(char *s, int val) {
    int x = root;
    for (int i = 0; s[i]; i++) {
      int d = s[i] - 'a';
      if (ch[x][d] == -1) {
        ch[x][d] = newnode();
      }
      x = ch[x][d];
    }
    value[x] += val;
  }
  int get(char *s) {
    int sum = 0, x = root;
    for (int i = 0; s[i]; i++) {
      int d = s[i] - 'a';
      if (ch[x][d] == -1) {
        break;
      }
      x = ch[x][d];
      sum += value[x];
    }
    return sum;
  }
} trie;
struct kmp {
  int next[300010];
  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 get(string s, char *str) {
    int sum = 0, j = 0;
    int lens = s.length();
    int 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;
int main() {
  int t, m, len, val;
  while (~scanf("%d", &m)) {
    trie.init();
    cnt = 0;
    memset(value, 0, sizeof(value));
    for (int i = 1; i <= m; i++) {
      scanf("%d%s", &t, str);
      len = strlen(str);
      if (t == 1 || t == 2) {
        val = (t == 1 ? 1 : -1);
        if (len <= 1000) {
          trie.insert(str, val);
        } else {
          st[cnt] = string(str);
          value[cnt++] = val;
        }
      } else {
        long long int ans = 0;
        for (int i = 0; i < len; i++) {
          ans += trie.get(str + i);
        }
        for (int i = 0; i < cnt; i++) {
          if (st[i].length() > len) {
            continue;
          }
          ans += kmp.get(st[i], str) * value[i];
        }
        printf("%I64d\n", ans);
        fflush(stdout);
      }
    }
  }
  return 0;
}
