#include <vector>
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
int main() {
vector<vector<string>> alph_start = vector<vector<string>>(26);
vector<vector<string>> alph_end = vector<vector<string>>(26);
string s;
vector<string> mas;
while (getline(cin, s), s != "") {
transform(s.begin(), s.end(), s.begin(), toupper);
mas.push_back(s);
}
int count;
cin >> count;
getline(cin, s);
for (int i = 0; i < count; i++) {
getline(cin, s);
if (s[0] >= 'a')
alph_start[s[0] - 'a'].push_back(s);
else
alph_start[s[0] - 'A'].push_back(s);
if (s[s.size() - 1] >= 'a')
alph_end[s[s.size() - 1] - 'a'].push_back(s);
else
alph_end[s[s.size() - 1] - 'A'].push_back(s);
}
for (int i = 0; i < mas.size(); i++) {
string output = mas[i] + "#";
for (auto word : alph_start[mas[i][0] - 'A'])
output += word + ",";
for (auto word : alph_end[mas[i][mas[i].size() - 1] - 'A'])
output += word + ",";
if (output.size() != mas[i].size() + 1) {
output.erase(output.size() - 1);
cout << output << endl;
}
}
}