#include <bits/stdc++.h>
using namespace std;
ifstream f("trie.in");
ofstream g("trie.out");
struct nod
{
nod *s[26];
int pass,cuvinte;
nod()
{
pass=cuvinte=0;
for(int i=0;i<26;i++)
s[i]=NULL;
}
};
nod *root,*st[30];
char w[25];
inline int decode(char ch){return (int)(ch-'a');}
void insertie(),stergere(),numara(),prefix();
int cod;
int main()
{
root=new nod;root->pass++;
while(f>>cod)
{
f>>w;
if(cod==0)insertie();else
if(cod==1)stergere();else
if(cod==2)numara();else
prefix();
}
return 0;
}
void insertie()
{
int p=0;
nod *q=root;
for(;w[p];p++)
{
int c=decode(w[p]);
if(!q->s[c])q->s[c]=new nod;
q=q->s[c];
q->pass++;
}
q->cuvinte++;
}
void stergere()
{
int p=0,c;
nod *q=root;
for(;w[p];p++)
{
st[p]=q;
c=decode(w[p]);
q=q->s[c];
q->pass--;
}
q->cuvinte--;
st[p]=q;
for(int i=p-1,j=p;;i--,j--)
{
if(st[j]->pass+st[j]->cuvinte)break;
c=decode(w[i]);
nod *aux=st[j];
st[j]=0;
st[i]->s[c]=0;
delete aux;
}
}
void numara()
{
int p=0;
nod *q=root;
for(;w[p];p++)
{
int c=decode(w[p]);
if(!q->s[c]){g<<"0\n";return;}
q=q->s[c];
}
g<<(q->cuvinte)<<'\n';
}
void prefix()
{
int p=0,Lg=0;
nod *q=root;
for(;w[p];p++)
{
int c=decode(w[p]);
if(!q->s[c])break;
q=q->s[c];
Lg++;
}
g<<Lg<<'\n';
}