#include<bits/stdc++.h>
using namespace std;
struct node{
int cnt_trees;
bool endmark;
node *next[26];
node(){
cnt_trees=0;
endmark=false;
for(int i=0;i<26;i++) next[i]=NULL;
}
}*root;
void insert_into_Trie(char* str,int len){
node *cur=root;
for(int i=0;i<len;i++){
int id=str[i]-'a';
if(cur->next[id]==NULL)
cur->next[id]=new node();
cur=cur->next[id];
}
cur->endmark=true;
}
node* search_into_Trie(char* str,int len){
node *cur=root;
for(int i=0;i<len;i++){
int id=str[i]-'a';
if(cur->next[id]==NULL) return NULL;
cur=cur->next[id];
}
return cur;
}
void lexicograpPrint(node *cur,char* prefix,vector<char>print_word){
if(cur->endmark and print_word.size()!=0){
printf("%s",prefix);
for(auto x:print_word) printf("%c",x);
printf("\n");
}
for(int i=0;i<26;i++){
if(cur->next[i]!=NULL){
print_word.push_back(i+'a'); //push converting integer int character
lexicograpPrint(cur->next[i],prefix,print_word);
print_word.pop_back();
}
}
print_word.pop_back();
}
int main()
{
int n,q;
char str[21];
root=new node();
scanf("%d",&n);
while(n--){
scanf(" %s",str);
insert_into_Trie(str,strlen(str));
}
scanf("%d",&q);
int tc=0;
while(q--){
scanf(" %s",str);
cout<<"Case #"<<++tc<<":"<<endl;
bool childrenThere=false;
node *temp=search_into_Trie(str,strlen(str));
if(temp!=NULL){ //that means the whole prefix remains in the Trie
for(int i=0;i<26;i++){
if(temp->next[i]!=NULL){childrenThere=true;break;} //prefix has children.so needed to print
}
if(childrenThere){
vector<char>print_word;
lexicograpPrint(temp,str,print_word); //printing Trie Lexicographically
}
else cout<<"No match."<<endl;
}
else cout<<"No match."<<endl;
}
return 0;
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKc3RydWN0IG5vZGV7CiAgICBpbnQgY250X3RyZWVzOwogICAgYm9vbCBlbmRtYXJrOwogICAgbm9kZSAqbmV4dFsyNl07CiAgICBub2RlKCl7CiAgICAgICAgY250X3RyZWVzPTA7CiAgICAgICAgZW5kbWFyaz1mYWxzZTsKICAgICAgICBmb3IoaW50IGk9MDtpPDI2O2krKykgbmV4dFtpXT1OVUxMOwogICAgfQp9KnJvb3Q7Cgp2b2lkIGluc2VydF9pbnRvX1RyaWUoY2hhciogc3RyLGludCBsZW4pewogICAgbm9kZSAqY3VyPXJvb3Q7CiAgICBmb3IoaW50IGk9MDtpPGxlbjtpKyspewogICAgICAgIGludCBpZD1zdHJbaV0tJ2EnOwogICAgICAgIGlmKGN1ci0+bmV4dFtpZF09PU5VTEwpCiAgICAgICAgICAgIGN1ci0+bmV4dFtpZF09bmV3IG5vZGUoKTsKICAgICAgICAgICAgY3VyPWN1ci0+bmV4dFtpZF07CiAgICB9CiAgICBjdXItPmVuZG1hcms9dHJ1ZTsKfQoKbm9kZSogc2VhcmNoX2ludG9fVHJpZShjaGFyKiBzdHIsaW50IGxlbil7CiAgICBub2RlICpjdXI9cm9vdDsKICAgIGZvcihpbnQgaT0wO2k8bGVuO2krKyl7CiAgICAgICAgaW50IGlkPXN0cltpXS0nYSc7CiAgICAgICAgaWYoY3VyLT5uZXh0W2lkXT09TlVMTCkgcmV0dXJuIE5VTEw7CiAgICAgICAgY3VyPWN1ci0+bmV4dFtpZF07CiAgICB9CiAgICByZXR1cm4gY3VyOwp9Cgp2b2lkIGxleGljb2dyYXBQcmludChub2RlICpjdXIsY2hhciogcHJlZml4LHZlY3RvcjxjaGFyPnByaW50X3dvcmQpewoKICAgICAgICBpZihjdXItPmVuZG1hcmsgYW5kIHByaW50X3dvcmQuc2l6ZSgpIT0wKXsKICAgICAgICAgICAgcHJpbnRmKCIlcyIscHJlZml4KTsKICAgICAgICAgICAgZm9yKGF1dG8geDpwcmludF93b3JkKSBwcmludGYoIiVjIix4KTsKICAgICAgICAgICAgcHJpbnRmKCJcbiIpOwogICAgICAgIH0KCiAgICBmb3IoaW50IGk9MDtpPDI2O2krKyl7CiAgICAgICAgaWYoY3VyLT5uZXh0W2ldIT1OVUxMKXsKICAgICAgICAgICAgcHJpbnRfd29yZC5wdXNoX2JhY2soaSsnYScpOyAgLy9wdXNoIGNvbnZlcnRpbmcgaW50ZWdlciBpbnQgY2hhcmFjdGVyCiAgICAgICAgICAgIGxleGljb2dyYXBQcmludChjdXItPm5leHRbaV0scHJlZml4LHByaW50X3dvcmQpOwogICAgICAgICAgICBwcmludF93b3JkLnBvcF9iYWNrKCk7CiAgICAgICAgfQogICAgfQogICAgcHJpbnRfd29yZC5wb3BfYmFjaygpOwp9CgppbnQgbWFpbigpCnsKCiAgICBpbnQgbixxOwogICAgY2hhciBzdHJbMjFdOwogICAgcm9vdD1uZXcgbm9kZSgpOwogICAgc2NhbmYoIiVkIiwmbik7CiAgICB3aGlsZShuLS0pewogICAgICAgIHNjYW5mKCIgJXMiLHN0cik7CiAgICAgICAgaW5zZXJ0X2ludG9fVHJpZShzdHIsc3RybGVuKHN0cikpOwogICAgfQogICAgc2NhbmYoIiVkIiwmcSk7CiAgICBpbnQgdGM9MDsKICAgIHdoaWxlKHEtLSl7CiAgICAgICAgc2NhbmYoIiAlcyIsc3RyKTsKICAgICAgICBjb3V0PDwiQ2FzZSAjIjw8Kyt0Yzw8IjoiPDxlbmRsOwogICAgICAgIGJvb2wgY2hpbGRyZW5UaGVyZT1mYWxzZTsKICAgICAgICBub2RlICp0ZW1wPXNlYXJjaF9pbnRvX1RyaWUoc3RyLHN0cmxlbihzdHIpKTsKICAgICAgICBpZih0ZW1wIT1OVUxMKXsgICAgICAgICAgICAvL3RoYXQgbWVhbnMgdGhlIHdob2xlIHByZWZpeCByZW1haW5zIGluIHRoZSBUcmllCiAgICAgICAgICAgIGZvcihpbnQgaT0wO2k8MjY7aSsrKXsKICAgICAgICAgICAgICAgIGlmKHRlbXAtPm5leHRbaV0hPU5VTEwpe2NoaWxkcmVuVGhlcmU9dHJ1ZTticmVhazt9IC8vcHJlZml4IGhhcyBjaGlsZHJlbi5zbyBuZWVkZWQgdG8gcHJpbnQKICAgICAgICAgICAgfQogICAgICAgICAgICBpZihjaGlsZHJlblRoZXJlKXsKICAgICAgICAgICAgICAgIHZlY3RvcjxjaGFyPnByaW50X3dvcmQ7CiAgICAgICAgICAgICAgICBsZXhpY29ncmFwUHJpbnQodGVtcCxzdHIscHJpbnRfd29yZCk7ICAgLy9wcmludGluZyBUcmllIExleGljb2dyYXBoaWNhbGx5CiAgICAgICAgICAgIH0KICAgICAgICAgICAgZWxzZSBjb3V0PDwiTm8gbWF0Y2guIjw8ZW5kbDsKICAgICAgICB9CiAgICAgICAgZWxzZSBjb3V0PDwiTm8gbWF0Y2guIjw8ZW5kbDsKICAgIH0KICAgIHJldHVybiAwOwp9Cg==