#include <iostream>
using namespace std;
typedef struct TrieNode
{
int count;
TrieNode *child[26];
TrieNode();
}TrieNode;
TrieNode::TrieNode()
{
for(int i = 0; i < 26; i++)
child[i] = NULL;
count = 0;
}
void buildTrieTree(TrieNode *root, char *str)
{
root->count++;
if (*str == '\0') return;
if (root->child[(*str)-'a'] != NULL)
buildTrieTree(root->child[(*str)-'a'], str+1);
else
{
root->child[(*str)-'a'] = new TrieNode[1];
buildTrieTree(root->child[(*str)-'a'], str+1);
}
}
int getBranchNumber(TrieNode *root, char *str)
{
if (*str == '\0')
return root->count;
else if (root->child[(*str)-'a'] == NULL)
return 0;
else
return getBranchNumber(root->child[(*str)-'a'], str+1);
}
void freeTrieTree(TrieNode *root)
{
for(int i = 0; i < 26; i++)
{
if(root->child[i] != NULL)
{
freeTrieTree(root->child[i]);
}
}
delete []root;
}
int main(void)
{
int n, m;
char str[30];
TrieNode *root = new TrieNode[1];
cin>>n;
for (int i = 0; i < n; i++)
{
cin>>str;
buildTrieTree(root, str);
}
cin>>m;
for (int i = 0; i < m; i++)
{
cin>>str;
cout<<getBranchNumber(root, str)<<endl;
}
freeTrieTree(root);
return 0;
}
using namespace std;
typedef struct TrieNode
{
int count;
TrieNode *child[26];
TrieNode();
}TrieNode;
TrieNode::TrieNode()
{
for(int i = 0; i < 26; i++)
child[i] = NULL;
count = 0;
}
void buildTrieTree(TrieNode *root, char *str)
{
root->count++;
if (*str == '\0') return;
if (root->child[(*str)-'a'] != NULL)
buildTrieTree(root->child[(*str)-'a'], str+1);
else
{
root->child[(*str)-'a'] = new TrieNode[1];
buildTrieTree(root->child[(*str)-'a'], str+1);
}
}
int getBranchNumber(TrieNode *root, char *str)
{
if (*str == '\0')
return root->count;
else if (root->child[(*str)-'a'] == NULL)
return 0;
else
return getBranchNumber(root->child[(*str)-'a'], str+1);
}
void freeTrieTree(TrieNode *root)
{
for(int i = 0; i < 26; i++)
{
if(root->child[i] != NULL)
{
freeTrieTree(root->child[i]);
}
}
delete []root;
}
int main(void)
{
int n, m;
char str[30];
TrieNode *root = new TrieNode[1];
cin>>n;
for (int i = 0; i < n; i++)
{
cin>>str;
buildTrieTree(root, str);
}
cin>>m;
for (int i = 0; i < m; i++)
{
cin>>str;
cout<<getBranchNumber(root, str)<<endl;
}
freeTrieTree(root);
return 0;
}