#include<bits/stdc++.h>
using namespace std;
const int N = 2e6 + 10;
int idx = 0, cnt[N], tr[N][26] ;
string s;
void update()
{
int p = 0;
for(int i(0);i < s.length(); ++ i)
{
int to = s[i] - 'a';
if(!tr[p][to])
tr[p][to] = ++ idx;
p = tr[p][to];
}
++ cnt[p];
}
int query()
{
int p = 0;
for(int i(0);i < s.length(); ++ i)
{
int to = s[i] - 'a';
if(!tr[p][to])
return 0;
p = tr[p][to];
}
return cnt[p];
}
int main()
{
int Q;
scanf("%d", &Q);
while(Q -- )
{
char op;
cin >> op >> s;
if(op == 'I')
update();
else
printf("%d\n", query());
}
return 0;
}
qwq谢谢