#include <fstream>
#include <map>
using namespace std;
ifstream cin ("trie.in");
ofstream cout ("trie.out");
const int N = 1e4;
class Trie
{
int f;
Trie *child[30];
public:
Trie() : f(0), child{} {}
void add (char *s)
{
if (*s == '\0')
return;
const int index = *s - 'a';
if (index < 0 || index >= 30)
return;
if (child[index] == nullptr)
child[index] = new Trie;
child[index]->f++;
child[index]->add(s + 1);
}
void sterge (char *s)
{
if (*s == '\0')
return;
const int index = *s - 'a';
child[index]->f--;
child[index]->sterge(s + 1);
if (child[index]->f == 0)
{
delete child[index];
child[index] = nullptr;
}
}
int prefix (char *s)
{
if (*s == '\0')
return 0;
const int index = *s - 'a';
if (child[index] != nullptr)
return 1 + child[index]->prefix(s + 1);
return 0;
}
};
Trie *t = new Trie;
char s[N + 1];
map <string, int> f;
int x;
int main ()
{
while (cin >> x >> s)
{
switch (x)
{
case 0:
f[s]++;
t->add(s);
break;
case 1:
f[s]--;
t->sterge(s);
break;
case 2:
cout << f[s] << '\n';
break;
case 3:
cout << t->prefix(s) << '\n';
break;
}
}
return 0;
}