#include <bits/stdc++.h>
using namespace std;
const int N = 300005, B = 31, P = 199912221;
int n;
char s[N];
long long b[N];
set<int> L;
set<long long> H[N];
set<int>::iterator it;
int main() {
  int i, j, k, ans;
  long long x;
  scanf("%d", &n);
  b[0] = 1;
  for (i = 1; i < N; i++) b[i] = b[i - 1] * B % P;
  while (n--) {
    scanf("%d %s", &j, s + 1);
    k = strlen(s + 1);
    if (j == 1 || j == 2) {
      x = 0;
      for (i = 1; i <= k; i++) x = (x * B + s[i]) % P;
      if (j == 1)
        L.insert(k), H[k].insert(x);
      else
        H[k].erase(x);
    } else {
      ans = 0;
      for (it = L.begin(); it != L.end(); it++) {
        j = *it;
        if (j > k) break;
        if (H[j].empty()) continue;
        for (x = 0, i = 1; i <= j; i++) x = (x * B + s[i]) % P;
        if (H[j].find(x) != H[j].end()) ans++;
        for (i = j + 1; i <= k; i++) {
          x += P - b[j - 1] * s[i - j] % P, x = (x * B + s[i]) % P;
          if (H[j].find(x) != H[j].end()) ans++;
        }
      }
      printf("%d\n", ans);
      fflush(stdout);
    }
  }
  return 0;
}
