#include "bits/stdc++.h"
using namespace std;
typedef long long LL;
const int INF = 0x3f3f3f3f;
struct AVL {
char key[11];
int value;
int height;
AVL* father;
AVL* lson;
AVL* rson;
}*root;
AVL* fafa;
AVL* fa;
AVL* me;
char key[11];
AVL* init(AVL* fa) {
AVL* point = (AVL*)malloc(sizeof(AVL));
strcpy(point->key, key);
point->value = 1;
point->father = fa;
point->height = 1;
point->lson = point->rson = NULL;
return point;
}
void updateHeight(AVL* point) {
int lheight = point->lson == NULL ? 0 : point->lson->height;
int rheight = point->rson == NULL ? 0 : point->rson->height;
point->height = max(lheight, rheight) + 1;
if (point->father != NULL) {
updateHeight(point->father);
}
}
bool unbalance(AVL* point) {
me = point;
fa = point->father;
fafa = fa->father;
if (fafa == NULL) {
return false;
}
int lheight = fafa->lson == NULL ? 0 : fafa->lson->height;
int rheight = fafa->rson == NULL ? 0 : fafa->rson->height;
if (abs(lheight - rheight) > 1) {
return true;
}
return unbalance(fa);
}
void leftRotate(AVL* fa, AVL* me) {
AVL* fafa = fa->father;
me->father = fafa;
if (fafa != NULL) {
if (fafa->lson == fa) {
fafa->lson = me;
} else {
fafa->rson = me;
}
}
fa->rson = me->lson;
if (me->lson != NULL) {
me->lson->father = fa;
}
fa->father = me;
me->lson = fa;
updateHeight(fa);
}
void rightRotate(AVL* fa, AVL* me) {
AVL* fafa = fa->father;
me->father = fafa;
if (fafa != NULL) {
if (fafa->lson == fa) {
fafa->lson = me;
} else {
fafa->rson = me;
}
}
fa->lson = me->rson;
if (me->rson != NULL) {
me->rson->father = fa;
}
fa->father = me;
me->rson = fa;
updateHeight(fa);
}
void rebalance() {
if (fafa->lson == fa && fa->lson == me) {
rightRotate(fafa, fa);
return;
}
if (fafa->rson == fa && fa->rson == me) {
leftRotate(fafa, fa);
return;
}
if (fafa->lson == fa && fa->rson == me) {
leftRotate(fa, me);
rightRotate(fafa, me);
return;
}
rightRotate(fa, me);
leftRotate(fafa, me);
}
void insert() {
AVL* father = NULL;
AVL* now = root;
int cmp;
while (now != NULL) {
if (strcmp(now->key, key) == 0) {
now->value++;
return;
}
father = now;
cmp = strcmp(now->key, key);
if (cmp == -1) {
now = now->rson;
} else {
now = now->lson;
}
}
if (father == NULL) {
root = init(NULL);
return;
} else if (cmp == -1) {
father->rson = init(father);
updateHeight(father);
if (unbalance(father->rson)) {
rebalance();
}
} else {
father->lson = init(father);
updateHeight(father);
if (unbalance(father->lson)) {
rebalance();
}
}
}
int query() {
AVL* now = root;
while (now != NULL) {
int cmp = strcmp(now->key, key);
if (cmp == 0) {
return now->value;
} else if (cmp == -1) {
now = now->rson;
} else {
now = now->lson;
}
}
return 0;
}
int main() {
while (gets(key) && key[0]) {
int len = strlen(key);
while (len != 0) {
key[len--] = '\0';
insert();
if (root->father != NULL) {
root = root->father;
}
}
}
while (~scanf("%s", key)) {
printf("%d\n", query());
}
return 0;
}