Problem: cf710_f
Difficulty: easy
Categories: data structures, search
Time limit: 3000 ms, memory limit: 768 MB

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
```

Non-adaptive interactor (C++):
#include "testlib.h"
#include <csignal>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

static int queries = 0;
static long long query_limit = 0;

void log_queries() {
    tout << "queries=" << queries << "\n";
    tout << "query_limit=" << query_limit << "\n";
}

void quit_with(TResult verdict, const string& msg) {
    log_queries();
    quitf(verdict, "%s", msg.c_str());
}

int main(int argc, char* argv[]) {
    registerInteraction(argc, argv);
    signal(SIGPIPE, SIG_IGN);
    atexit(log_queries);

    
    
    
    
    

    const int m = inf.readInt();
    cout << m << "\n" << flush;

    for (int i = 0; i < m; i++) {
        const int t = inf.readInt();
        const string s = inf.readToken();
        cout << t << " " << s << "\n" << flush;
        if (t == 3) {
            long long out;
            if (!(cin >> out)) {
                quit_with(_wa, "Missing outputs");
            }
            queries++;
        }
    }
    query_limit = queries;
    quit_with(_ok, "Best-effort accepted");
}


Adaptive interactor (C++):

Test generator (C++):
#include "testlib.h"
#include <bits/stdc++.h>

using namespace std;

int main(int argc, char* argv[]) {
    
    registerGen(argc, argv, 1);
    
    
    string mode = opt<string>("mode", "non");
    int seed = atoi(argv[1]);

    
    if (seed == 1) {
        cout << "5\n";
        cout << "1 abc\n";
        cout << "3 abcabc\n";
        cout << "2 abc\n";
        cout << "1 aba\n";
        cout << "3 abababc\n";
        return 0;
    }
    if (seed == 2) {
        cout << "6\n";
        cout << "1 a\n";
        cout << "1 b\n";
        cout << "3 ab\n";
        cout << "2 a\n";
        cout << "3 ab\n";
        cout << "2 b\n";
        return 0;
    }

    
    
    const int MAX_M = 300000;
    const int MAX_TOTAL_LEN = 300000;

    
    int alphabet_size = 26;
    if (mode == "adp") {
        
        alphabet_size = 2;
    } else {
        
        int r = rnd.next(10);
        if (r < 2) alphabet_size = 2;       
        else if (r < 4) alphabet_size = 4;  
        else alphabet_size = 26;            
    }

    
    vector<string> active;      
    vector<string> history;     
    set<string> seen_adds;      
    
    struct Op {
        int type;
        string s;
    };
    vector<Op> ops;
    
    long long current_total_len = 0;
    
    
    while (ops.size() < MAX_M && current_total_len < MAX_TOTAL_LEN - 5) {
        
        int w_add = 40;
        int w_del = 20;
        int w_qry = 40;
        
        
        if (active.empty()) w_del = 0;
        
        
        if (history.size() < 10) {
            w_add = 80; w_del = 0; w_qry = 20;
        }

        
        long long rem_len = MAX_TOTAL_LEN - current_total_len;
        if (rem_len < 100) {
            if (rem_len < 5) break; 
            w_add = 1; w_del = 1; w_qry = 10; 
        }

        int dice = rnd.next(w_add + w_del + w_qry);
        int type = 1;
        if (dice < w_add) type = 1;
        else if (dice < w_add + w_del) type = 2;
        else type = 3;

        string s = "";

        if (type == 1) { 
            int len = rnd.next(1, 10);
            
            if (rnd.next(20) == 0 && rem_len >= 10) len = rnd.next(10, min((int)rem_len, 300));
            if (len > rem_len) len = (int)rem_len;
            if (len < 1) len = 1;

            
            bool unique_found = false;
            for (int k = 0; k < 20; ++k) {
                string tmp = "";
                for (int i = 0; i < len; ++i) tmp += (char)('a' + rnd.next(alphabet_size));
                if (seen_adds.find(tmp) == seen_adds.end()) {
                    s = tmp;
                    unique_found = true;
                    break;
                }
            }
            if (!unique_found) continue; 

            seen_adds.insert(s);
            active.push_back(s);
            history.push_back(s);

        } else if (type == 2) { 
            if (active.empty()) continue;
            int idx = rnd.next(active.size());
            s = active[idx];
            
            active[idx] = active.back();
            active.pop_back();

        } else { 
            int len = rnd.next(1, 50);
            if (rnd.next(20) == 0 && rem_len >= 50) len = rnd.next(50, min((int)rem_len, 1000));
            if (len > rem_len) len = (int)rem_len;
            if (len < 1) len = 1;

            
            if (history.empty() || rnd.next(5) == 0) {
                
                for (int i = 0; i < len; ++i) s += (char)('a' + rnd.next(alphabet_size));
            } else {
                
                while (s.length() < (size_t)len) {
                    string part = history[rnd.next(history.size())];
                    if (s.length() + part.length() > (size_t)len) {
                        
                        while(s.length() < (size_t)len) s += (char)('a' + rnd.next(alphabet_size));
                        break;
                    }
                    s += part;
                }
            }
        }

        
        if (current_total_len + s.length() > MAX_TOTAL_LEN) break;

        ops.push_back({type, s});
        current_total_len += s.length();
    }

    
    cout << ops.size() << "\n";
    for (const auto& op : ops) {
        cout << op.type << " " << op.s << "\n";
    }

    return 0;
}