#include <bits/stdc++.h>
using namespace std;
const int M = 20;
int to_ind(char ch) { return ch - 'a'; }
struct Node {
  Node *child[26], *parent, *failure_link;
  int suffix_matches;
  char ch;
  Node(Node *p, char c) {
    suffix_matches = 0;
    ch = c;
    failure_link = NULL;
    parent = p;
    for (char ch = 'a'; ch <= 'z'; ++ch) extend(ch, NULL);
  }
  void extend(char ch, Node *node) { child[to_ind(ch)] = node; }
  Node *get_child(char ch) {
    if (to_ind(ch) < 0) return NULL;
    return child[to_ind(ch)];
  }
  int has_child(char ch) { return this->get_child(ch) != NULL; }
};
struct AhoCorasickTree {
  vector<string> strs;
  Node *root;
  AhoCorasickTree() {
    this->root = new Node(NULL, 0);
    this->root->failure_link = this->root;
  }
  AhoCorasickTree(vector<string> &ss) {
    this->root = new Node(NULL, 0);
    for (int i = 0; i < ss.size(); ++i) this->insert(ss[i]);
    this->build_failure_links();
  }
  ~AhoCorasickTree() { free_node(this->root); }
  void insert(string &s) {
    this->strs.push_back(s);
    Node *tmp, *current = this->root;
    for (int i = 0; i < s.size(); ++i) {
      if (!current->has_child(s[i]))
        current->extend(s[i], new Node(current, s[i]));
      current = current->get_child(s[i]);
    }
    current->suffix_matches++;
  }
  void build_failure_links(void) {
    deque<pair<Node *, char> > q;
    q.push_back(make_pair(this->root, 0));
    while (!q.empty()) {
      Node *node = q.front().first;
      char last_ch = q.front().second;
      q.pop_front();
      node->failure_link = this->root;
      if (node->parent != NULL && node->parent->parent != NULL) {
        Node *tmp = node->parent->failure_link;
        while (tmp != this->root && !tmp->has_child(last_ch))
          tmp = tmp->failure_link;
        if (tmp->has_child(last_ch)) {
          node->failure_link = tmp->get_child(last_ch);
          node->suffix_matches += node->failure_link->suffix_matches;
        }
      }
      for (char c = 'a'; c <= 'z'; ++c)
        if (node->has_child(c)) q.push_back(make_pair(node->get_child(c), c));
    }
  }
  long long query(string &s) {
    Node *tmp, *current = this->root;
    long long result = 0;
    for (int i = 0; i < s.size(); ++i) {
      while (current != this->root && !current->has_child(s[i]))
        current = current->failure_link;
      if (current->has_child(s[i])) current = current->get_child(s[i]);
      result += current->suffix_matches;
    }
    return result;
  }
  int count(void) { return this->strs.size(); }
  void free_node(Node *node) {
    for (char c = 'a'; c <= 'z'; ++c)
      if (node->has_child(c)) free_node(node->get_child(c));
    delete node;
  }
};
struct DynamicAhoCorasick {
  AhoCorasickTree *tree[M];
  DynamicAhoCorasick() {
    for (int i = 0; i < M; ++i) tree[i] = new AhoCorasickTree();
  }
  ~DynamicAhoCorasick() {
    for (int i = 0; i < M; ++i) delete tree[i];
  }
  void insert(string &s) {
    tree[0]->insert(s);
    tree[0]->build_failure_links();
    int tind = 0;
    while (tree[tind]->count() > (1 << tind)) {
      vector<string> A = this->tree[tind]->strs;
      vector<string> B = this->tree[tind + 1]->strs;
      vector<string> C(A.size() + B.size());
      merge(A.begin(), A.end(), B.begin(), B.end(), C.begin());
      delete tree[tind + 1];
      delete tree[tind];
      tree[tind + 1] = new AhoCorasickTree(C);
      tree[tind] = new AhoCorasickTree();
      tind++;
    }
  }
  long long query(string &s) {
    long long result = 0;
    for (int i = 0; i < M; ++i) result += this->tree[i]->query(s);
    return result;
  }
} added, removed;
char st[300005];
int m, t;
int main(void) {
  scanf("%d", &m);
  for (int i = 0; i < m; ++i) {
    scanf("%d %s", &t, st);
    string s = string(st);
    switch (t) {
      case 1:
        added.insert(s);
        break;
      case 2:
        removed.insert(s);
        break;
      case 3:
        cout << added.query(s) - removed.query(s) << endl;
        fflush(stdout);
        break;
    }
  }
  return 0;
}

### Prompt

Develop a solution in cpp to the problem described below:

You should process m queries over a set D of strings. Each query is one of three kinds:

  1. Add a string s to the set D. It is guaranteed that the string s was not added before. 
  2. Delete a string s from the set D. It is guaranteed that the string s is in the set D. 
  3. For the given string s find the number of occurrences of the strings from the set D. If some string p from D has several occurrences in s you should count all of them. 



Note that you should solve the problem in online mode. It means that you can't read the whole input at once. You can read each query only after writing the answer for the last query of the third type. Use functions fflush in C++ and BufferedWriter.flush in Java languages after each writing in your program.

Input

The first line contains integer m (1 ≤ m ≤ 3·105) — the number of queries.

Each of the next m lines contains integer t (1 ≤ t ≤ 3) and nonempty string s — the kind of the query and the string to process. All strings consist of only lowercase English letters.

The sum of lengths of all strings in the input will not exceed 3·105.

Output

For each query of the third kind print the only integer c — the desired number of occurrences in the string s.

Examples

Input

5
1 abc
3 abcabc
2 abc
1 aba
3 abababc


Output

2
2


Input

10
1 abc
1 bcd
1 abcd
3 abcd
2 abcd
3 abcd
2 bcd
3 abcd
2 abc
3 abcd


Output

3
2
1
0

### Response

```cpp
#include <bits/stdc++.h>
using namespace std;
const int M = 20;
int to_ind(char ch) { return ch - 'a'; }
struct Node {
  Node *child[26], *parent, *failure_link;
  int suffix_matches;
  char ch;
  Node(Node *p, char c) {
    suffix_matches = 0;
    ch = c;
    failure_link = NULL;
    parent = p;
    for (char ch = 'a'; ch <= 'z'; ++ch) extend(ch, NULL);
  }
  void extend(char ch, Node *node) { child[to_ind(ch)] = node; }
  Node *get_child(char ch) {
    if (to_ind(ch) < 0) return NULL;
    return child[to_ind(ch)];
  }
  int has_child(char ch) { return this->get_child(ch) != NULL; }
};
struct AhoCorasickTree {
  vector<string> strs;
  Node *root;
  AhoCorasickTree() {
    this->root = new Node(NULL, 0);
    this->root->failure_link = this->root;
  }
  AhoCorasickTree(vector<string> &ss) {
    this->root = new Node(NULL, 0);
    for (int i = 0; i < ss.size(); ++i) this->insert(ss[i]);
    this->build_failure_links();
  }
  ~AhoCorasickTree() { free_node(this->root); }
  void insert(string &s) {
    this->strs.push_back(s);
    Node *tmp, *current = this->root;
    for (int i = 0; i < s.size(); ++i) {
      if (!current->has_child(s[i]))
        current->extend(s[i], new Node(current, s[i]));
      current = current->get_child(s[i]);
    }
    current->suffix_matches++;
  }
  void build_failure_links(void) {
    deque<pair<Node *, char> > q;
    q.push_back(make_pair(this->root, 0));
    while (!q.empty()) {
      Node *node = q.front().first;
      char last_ch = q.front().second;
      q.pop_front();
      node->failure_link = this->root;
      if (node->parent != NULL && node->parent->parent != NULL) {
        Node *tmp = node->parent->failure_link;
        while (tmp != this->root && !tmp->has_child(last_ch))
          tmp = tmp->failure_link;
        if (tmp->has_child(last_ch)) {
          node->failure_link = tmp->get_child(last_ch);
          node->suffix_matches += node->failure_link->suffix_matches;
        }
      }
      for (char c = 'a'; c <= 'z'; ++c)
        if (node->has_child(c)) q.push_back(make_pair(node->get_child(c), c));
    }
  }
  long long query(string &s) {
    Node *tmp, *current = this->root;
    long long result = 0;
    for (int i = 0; i < s.size(); ++i) {
      while (current != this->root && !current->has_child(s[i]))
        current = current->failure_link;
      if (current->has_child(s[i])) current = current->get_child(s[i]);
      result += current->suffix_matches;
    }
    return result;
  }
  int count(void) { return this->strs.size(); }
  void free_node(Node *node) {
    for (char c = 'a'; c <= 'z'; ++c)
      if (node->has_child(c)) free_node(node->get_child(c));
    delete node;
  }
};
struct DynamicAhoCorasick {
  AhoCorasickTree *tree[M];
  DynamicAhoCorasick() {
    for (int i = 0; i < M; ++i) tree[i] = new AhoCorasickTree();
  }
  ~DynamicAhoCorasick() {
    for (int i = 0; i < M; ++i) delete tree[i];
  }
  void insert(string &s) {
    tree[0]->insert(s);
    tree[0]->build_failure_links();
    int tind = 0;
    while (tree[tind]->count() > (1 << tind)) {
      vector<string> A = this->tree[tind]->strs;
      vector<string> B = this->tree[tind + 1]->strs;
      vector<string> C(A.size() + B.size());
      merge(A.begin(), A.end(), B.begin(), B.end(), C.begin());
      delete tree[tind + 1];
      delete tree[tind];
      tree[tind + 1] = new AhoCorasickTree(C);
      tree[tind] = new AhoCorasickTree();
      tind++;
    }
  }
  long long query(string &s) {
    long long result = 0;
    for (int i = 0; i < M; ++i) result += this->tree[i]->query(s);
    return result;
  }
} added, removed;
char st[300005];
int m, t;
int main(void) {
  scanf("%d", &m);
  for (int i = 0; i < m; ++i) {
    scanf("%d %s", &t, st);
    string s = string(st);
    switch (t) {
      case 1:
        added.insert(s);
        break;
      case 2:
        removed.insert(s);
        break;
      case 3:
        cout << added.query(s) - removed.query(s) << endl;
        fflush(stdout);
        break;
    }
  }
  return 0;
}

```