#include<fstream>
#include<cstdio>
#include<vector>
#include<string>
using namespace std;
ifstream fin("trie.in");
ofstream fout("trie.out");
int n,m,i,j,a,b,c,cer;
struct nod
{
int cnt=0;
int lvs=0;
int fr[26]={};
};
vector<nod>trie(1);
string s;
void insert(string &str)
{
int node=0;
for(char chr:str)
{
if(!trie[node].fr[chr-'a'])
{
trie[node].fr[chr-'a']=trie.size();
trie.emplace_back();
}
node=trie[node].fr[chr-'a'];
trie[node].lvs++;
}
trie[node].cnt++;
}
void erase(string& str)
{
int node=0;
for(char chr: str)
{
node=trie[node].fr[chr-'a'];
trie[node].lvs--;
}
trie[node].cnt--;
node=0;
for(char chr: str)
{
if(trie[trie[node].fr[chr-'a']].lvs==0)
{
trie[node].fr[chr-'a']=0;
return;
}
node=trie[node].fr[chr-'a'];
}
}
int count(string &str)
{
int node=0;
for(char chr: str)
{
if(trie[node].fr[chr-'a']==0)
return 0;
node=trie[node].fr[chr-'a'];
}
return trie[node].cnt;
}
int lcp(string &str)
{
int node=0, len=0;
for(char chr:str)
{
if(trie[node].fr[chr-'a']==0)
return len;
node=trie[node].fr[chr-'a'];
len++;
}
return len;
}
int main()
{
ios::sync_with_stdio(false);
fin.tie(0);
while(fin>>cer>>s)
{
if(cer==0)
insert(s);
if(cer==1)
erase(s);
if(cer==2)
{
int x=count(s);
fout<<x<<'\n';
}
if(cer==3)
{
int x=lcp(s);
fout<<x<<'\n';
}
}
return 0;
}