#include <bits/stdc++.h>
using namespace std;
struct node {
int word;
node* edges[26];
};
node* get() {
node* x = new node;
x->word = -1;
for (int i = 0; i < 26; i++) x->edges[i] = NULL;
return x;
}
node* trie = get();
string in[25010];
void add(node* c, string s, int ci, int i) {
if (ci == s.size()) { c->word = i; return; }
int k = s[ci] - 'a';
if (c->edges[k] == NULL) c->edges[k] = get();
add(c->edges[k], s, ci + 1, i);
}
void print(node* c) {
if (c == NULL) return;
if (c->word != -1) cout << in[c->word] << '\n';
for (int i = 0; i < 26; i++) print(c->edges[i]);
}
void get_pref(node* c, string s, int ci) {
if (ci == s.size()) { print(c); return; }
int k = s[ci] - 'a';
if (c->edges[k] == NULL) { cout << "No match.\n"; return; }
get_pref(c->edges[k], s, ci + 1);
}
int main() {
int n; cin >> n;
for (int i = 0; i < n; i++) {
cin >> in[i];
add(trie, in[i], 0, i);
}
int m; cin >> m; string qs;
for (int tc = 1; tc <= m; tc++) {
cin >> qs;
cout << "Case #" << tc << ": \n";
get_pref(trie, qs, 0);
}
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+Cgp1c2luZyBuYW1lc3BhY2Ugc3RkOwpzdHJ1Y3Qgbm9kZSB7CiAgICBpbnQgd29yZDsKICAgIG5vZGUqIGVkZ2VzWzI2XTsKfTsKbm9kZSogZ2V0KCkgewogICAgbm9kZSogeCA9IG5ldyBub2RlOwogICAgeC0+d29yZCA9IC0xOwogICAgZm9yIChpbnQgaSA9IDA7IGkgPCAyNjsgaSsrKSB4LT5lZGdlc1tpXSA9IE5VTEw7CiAgICByZXR1cm4geDsKfQpub2RlKiB0cmllID0gZ2V0KCk7CnN0cmluZyBpblsyNTAxMF07CnZvaWQgYWRkKG5vZGUqIGMsIHN0cmluZyBzLCBpbnQgY2ksIGludCBpKSB7CiAgICBpZiAoY2kgPT0gcy5zaXplKCkpIHsgYy0+d29yZCA9IGk7IHJldHVybjsgfQogICAgaW50IGsgPSBzW2NpXSAtICdhJzsKICAgIGlmIChjLT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