#include <bits/stdc++.h>
using namespace std;
struct Trie_map_node {
  map<char, Trie_map_node *> next;
  Trie_map_node *fail;
  int end, end2;
  Trie_map_node(Trie_map_node *a, bool b) {
    fail = a;
    end2 = 0;
    end = b;
  }
  ~Trie_map_node() {
    next.clear();
    delete fail;
  }
};
struct Trie_map {
  Trie_map_node *root;
  Trie_map() { root = new Trie_map_node(NULL, 0); }
  void insert(string &l) {
    Trie_map_node *found = root;
    for (auto &s : l) {
      if (found->next.find(s) != found->next.end())
        found = found->next[s];
      else {
        found->next[s] = new Trie_map_node(NULL, 0);
        found = found->next[s];
      }
      continue;
    }
    found->end++;
  }
  void clear_fail(Trie_map_node *part) {
    part->fail = NULL;
    part->end2 = part->end;
    for (auto &s : part->next) clear_fail(s.second);
  }
  void compute_fail() {
    queue<Trie_map_node *> part;
    part.push(root);
    while (!part.empty()) {
      Trie_map_node *part2 = NULL;
      Trie_map_node *father = part.front();
      part.pop();
      for (auto s = father->next.begin(); s != father->next.end(); ++s) {
        if (father == root)
          s->second->fail = root;
        else {
          part2 = father->fail;
          while (part2 != NULL) {
            if (part2->next.find(s->first) != part2->next.end()) {
              s->second->fail = part2->next[s->first];
              s->second->end2 += part2->next[s->first]->end2;
              break;
            }
            part2 = part2->fail;
          }
          if (part2 == NULL) s->second->fail = root;
        }
        part.push(s->second);
      }
    }
  }
  void clear() { root = new Trie_map_node(NULL, 0); }
};
struct online_AC {
  Trie_map part[20];
  int group[20], size1[20];
  int p2;
  set<string> part2[20];
  online_AC() {
    for (int i = 0; i < 20; ++i) group[i] = (1 << i);
    p2 = 0;
    memset(size1, 0, sizeof(size1));
  };
  void add(string &s) {
    Trie_map_node *found = part[0].root;
    part2[0].insert(s);
    size1[0] += s.size();
    part[0].insert(s);
    if (size1[0] > group[0])
      rebuild(0);
    else {
      part[0].clear_fail(part[0].root);
      part[0].compute_fail();
    }
  }
  void Delete(string &s) {
    int flag = 1;
    for (int i = 0; i <= p2; ++i) {
      if (part2[i].find(s) != part2[i].end()) {
        part2[i].erase(s);
        Trie_map_node *found = part[i].root;
        for (auto &p : s) {
          if (found->next.find(p) != found->next.end())
            found = found->next[p];
          else {
            flag = 0;
            break;
          }
        }
        if (flag) {
          found->end -= 1;
          part[i].clear_fail(part[i].root);
          part[i].compute_fail();
        }
        break;
      }
    }
  }
  long match(string &s) {
    long ans = 0;
    for (int i = 0; i <= p2; ++i) {
      Trie_map_node *found = part[i].root, *found2 = NULL;
      for (auto &p : s) {
        if (found->next.find(p) != found->next.end())
          found = found->next[p];
        else {
          while (found != part[i].root &&
                 found->next.find(p) == found->next.end())
            found = found->fail;
          if (found->next.find(p) != found->next.end()) found = found->next[p];
        }
        ans += found->end2;
      }
    }
    return ans;
  }
  void rebuild(int s) {
    if (s + 1 > p2) p2 = s + 1;
    for (auto &p : part2[s]) part2[s + 1].insert(p);
    size1[s + 1] += size1[s];
    size1[s] = 0;
    part2[s].clear();
    part[s].clear();
    if (size1[s + 1] > group[s + 1])
      rebuild(s + 1);
    else {
      part[s + 1].clear();
      for (auto p : part2[s + 1]) part[s + 1].insert(p);
      part[s + 1].clear_fail(part[s + 1].root);
      part[s + 1].compute_fail();
    }
  }
};
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  online_AC part;
  int a, b;
  string str;
  cin >> a;
  while (a--) {
    cin >> b;
    if (b == 1) {
      cin >> str;
      part.add(str);
    } else if (b == 2) {
      cin >> str;
      part.Delete(str);
    } else {
      cin >> str;
      cout << part.match(str) << endl;
    }
  }
  return 0;
}

### Prompt

Please provide a Cpp coded solution 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;
struct Trie_map_node {
  map<char, Trie_map_node *> next;
  Trie_map_node *fail;
  int end, end2;
  Trie_map_node(Trie_map_node *a, bool b) {
    fail = a;
    end2 = 0;
    end = b;
  }
  ~Trie_map_node() {
    next.clear();
    delete fail;
  }
};
struct Trie_map {
  Trie_map_node *root;
  Trie_map() { root = new Trie_map_node(NULL, 0); }
  void insert(string &l) {
    Trie_map_node *found = root;
    for (auto &s : l) {
      if (found->next.find(s) != found->next.end())
        found = found->next[s];
      else {
        found->next[s] = new Trie_map_node(NULL, 0);
        found = found->next[s];
      }
      continue;
    }
    found->end++;
  }
  void clear_fail(Trie_map_node *part) {
    part->fail = NULL;
    part->end2 = part->end;
    for (auto &s : part->next) clear_fail(s.second);
  }
  void compute_fail() {
    queue<Trie_map_node *> part;
    part.push(root);
    while (!part.empty()) {
      Trie_map_node *part2 = NULL;
      Trie_map_node *father = part.front();
      part.pop();
      for (auto s = father->next.begin(); s != father->next.end(); ++s) {
        if (father == root)
          s->second->fail = root;
        else {
          part2 = father->fail;
          while (part2 != NULL) {
            if (part2->next.find(s->first) != part2->next.end()) {
              s->second->fail = part2->next[s->first];
              s->second->end2 += part2->next[s->first]->end2;
              break;
            }
            part2 = part2->fail;
          }
          if (part2 == NULL) s->second->fail = root;
        }
        part.push(s->second);
      }
    }
  }
  void clear() { root = new Trie_map_node(NULL, 0); }
};
struct online_AC {
  Trie_map part[20];
  int group[20], size1[20];
  int p2;
  set<string> part2[20];
  online_AC() {
    for (int i = 0; i < 20; ++i) group[i] = (1 << i);
    p2 = 0;
    memset(size1, 0, sizeof(size1));
  };
  void add(string &s) {
    Trie_map_node *found = part[0].root;
    part2[0].insert(s);
    size1[0] += s.size();
    part[0].insert(s);
    if (size1[0] > group[0])
      rebuild(0);
    else {
      part[0].clear_fail(part[0].root);
      part[0].compute_fail();
    }
  }
  void Delete(string &s) {
    int flag = 1;
    for (int i = 0; i <= p2; ++i) {
      if (part2[i].find(s) != part2[i].end()) {
        part2[i].erase(s);
        Trie_map_node *found = part[i].root;
        for (auto &p : s) {
          if (found->next.find(p) != found->next.end())
            found = found->next[p];
          else {
            flag = 0;
            break;
          }
        }
        if (flag) {
          found->end -= 1;
          part[i].clear_fail(part[i].root);
          part[i].compute_fail();
        }
        break;
      }
    }
  }
  long match(string &s) {
    long ans = 0;
    for (int i = 0; i <= p2; ++i) {
      Trie_map_node *found = part[i].root, *found2 = NULL;
      for (auto &p : s) {
        if (found->next.find(p) != found->next.end())
          found = found->next[p];
        else {
          while (found != part[i].root &&
                 found->next.find(p) == found->next.end())
            found = found->fail;
          if (found->next.find(p) != found->next.end()) found = found->next[p];
        }
        ans += found->end2;
      }
    }
    return ans;
  }
  void rebuild(int s) {
    if (s + 1 > p2) p2 = s + 1;
    for (auto &p : part2[s]) part2[s + 1].insert(p);
    size1[s + 1] += size1[s];
    size1[s] = 0;
    part2[s].clear();
    part[s].clear();
    if (size1[s + 1] > group[s + 1])
      rebuild(s + 1);
    else {
      part[s + 1].clear();
      for (auto p : part2[s + 1]) part[s + 1].insert(p);
      part[s + 1].clear_fail(part[s + 1].root);
      part[s + 1].compute_fail();
    }
  }
};
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  online_AC part;
  int a, b;
  string str;
  cin >> a;
  while (a--) {
    cin >> b;
    if (b == 1) {
      cin >> str;
      part.add(str);
    } else if (b == 2) {
      cin >> str;
      part.Delete(str);
    } else {
      cin >> str;
      cout << part.match(str) << endl;
    }
  }
  return 0;
}

```