#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
#include <iomanip>
#define vt vector
#define pb push_back
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define pii pair <int, int>
#define fr first
#define sc second
using namespace std;
string filename = "trie";
ifstream fin(filename + ".in");
ofstream fout(filename + ".out");
////////////////////////////////////////////////////////////
const int SIGMA = 26;
struct Trie{
int f, cnt;
Trie *nxt[SIGMA];
Trie(){
f = cnt = 0;
for(int i = 0; i < SIGMA; i++){
nxt[i] = NULL;
}
}
};
Trie *root = new Trie;
void Insert(Trie *node, string s, int ind){
node->f++;
if(ind == sz(s)){
node->cnt++;
return;
}
if(node->nxt[s[ind] - 'a'] == NULL){
node->nxt[s[ind] - 'a'] = new Trie;
}
Insert(node->nxt[s[ind] - 'a'], s, ind + 1);
}
void Erase(Trie *node, string s, int ind){
node->f--;
if(ind == sz(s)){
node->cnt--;
return;
}
Erase(node->nxt[s[ind] - 'a'], s, ind + 1);
if(node->nxt[s[ind] - 'a']->f == 0){
node->nxt[s[ind] - 'a'] = NULL;
}
}
int Count(Trie *node, string s, int ind){
if(ind == sz(s)){
return node->cnt;
}
if(node->nxt[s[ind] - 'a'] == NULL){
return 0;
}
return Count(node->nxt[s[ind] - 'a'], s, ind + 1);
}
int LCP(Trie *node, string s, int ind){
if(ind == sz(s)){
return sz(s);
}
if(node->nxt[s[ind] - 'a'] == NULL){
return ind;
}
return LCP(node->nxt[s[ind] - 'a'], s, ind + 1);
}
void solve(){
int type;
string s;
while(fin >> type >> s){
if(type == 0){
Insert(root, s, 0);
}else if(type == 1){
Erase(root, s, 0);
}else if(type == 2){
fout << Count(root, s, 0) << '\n';
}else{
fout << LCP(root, s, 0) << '\n';
}
}
}
int main(){
int T;
//fin >> T;
T = 1;
while(T--){
solve();
}
return 0;
}