#include <bits/stdc++.h>
#define pi acos(-1)
#define fastio ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
const int INF = 0x3f3f3f3f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const int maxn = 400000 + 10;
const int mod = 1e9 + 7;
char word[20];
int sz=1;
struct node
{
int child[30];
int f;
int hcnt;
int acnt;
node(){
f = 0;
hcnt = 0;
acnt = 0;
memset(child, 0, sizeof(child));
}
}Trie[maxn];
void _insert(char *s)
{
int u=0, len = strlen(s), pos;
for(int i=0; i<len ; i++){
pos = s[i] - 'a';
if(Trie[u].child[pos] == 0) { Trie[u].child[pos] = sz++; }
u = Trie[u].child[pos];
Trie[u].hcnt++;
}
Trie[u].f = 1;
Trie[u].acnt++;
}
int _find(char *s)
{
int u=0, len = strlen(s), pos;
for(int i=0; i<len; i++){
pos = s[i] - 'a';
if(Trie[u].child[pos] == 0){ return 0; }
u = Trie[u].child[pos];
}
return Trie[u].hcnt;
}
int main()
{
while(cin.getline(word, 15)){
if(strlen(word) == 0) break;
else _insert(word);
}
while(cin.getline(word, 15)){
printf("%d\n", _find(word));
}
return 0;
}