#include <bits/stdc++.h>
const int A = 26;
struct state {
  state *p, *to[A], *link;
  int x, end, amt;
  state(state *p_ = 0, int x_ = 0) {
    p = p_;
    x = x_;
    amt = -1;
    end = 0;
    for (int i = 0; i < A; ++i) to[i] = 0;
    if (p)
      link = 0;
    else
      link = this;
  }
  state *add(int c);
  void move(state *pr);
  state *to_f(int c);
  state *link_f();
  int amt_f();
};
state *state::add(int c) {
  if (!to[c]) to[c] = new state(this, c);
  return to[c];
}
void state::move(state *pr) {
  end += pr->end;
  for (int i = 0; i < A; ++i)
    if (pr->to[i] && pr->to[i]->p == pr) add(i)->move(pr->to[i]);
  delete pr;
  pr = 0;
}
state *state::to_f(int c) {
  if (to[c]) return to[c];
  if (!p) return to[c] = this;
  return to[c] = link_f()->to_f(c);
}
state *state::link_f() {
  if (link) return link;
  if (!p->p) return link = p;
  return link = p->link_f()->to_f(x);
}
int state::amt_f() {
  if (amt != -1) return amt;
  if (!p) return amt = 0;
  return amt = end + link_f()->amt_f();
}
int count(state *cur, char *s) {
  int res = 0;
  for (int i = 0; s[i]; ++i) {
    cur = cur->to_f(s[i] - 'a');
    res += cur->amt_f();
  }
  return res;
}
const int LG = 19;
class dynamic_ac {
  state *ac[LG];

 public:
  dynamic_ac() {
    for (int i = 0; i < LG; ++i) ac[i] = 0;
  }
  void add_string(char *s) {
    int i;
    for (i = 0; ac[i]; ++i)
      ;
    state *cur = ac[i] = new state;
    for (int j = 0; s[j]; ++j) cur = cur->add(s[j] - 'a');
    ++cur->end;
    for (int j = i - 1; j >= 0; --j) {
      ac[i]->move(ac[j]);
      ac[j] = 0;
    }
  }
  int query(char *s) {
    int ans = 0;
    for (int i = 0; i < LG; ++i)
      if (ac[i]) ans += count(ac[i], s);
    return ans;
  }
};
const int N = 300000;
int m;
char s[N + 1];
int main() {
  scanf("%d", &m);
  dynamic_ac in, out;
  while (m--) {
    int t;
    scanf("%d%s", &t, s);
    if (t == 1)
      in.add_string(s);
    else if (t == 2)
      out.add_string(s);
    else {
      printf("%d\n", in.query(s) - out.query(s));
      fflush(stdout);
    }
  }
  return 0;
}
