#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
int n, k = 0;
cin >> n >> k;
vector <string> variates;
vector <int> values;
while (n--) {
string variate;
cin >> variate;
variates.push_back(variate);
int value = 0;
cin >> value;
values.push_back(value);
}
while (getchar() != '\n');
while (k--) {
string sentence;
getline(cin, sentence);
int position_left = sentence.find("{");
while (sentence.find("{") != string::npos) {
int position_right = sentence.find("}");
string get_variate = sentence.substr(position_left+1, position_right-position_left-1);
int i = 0;
for (; i < (int)sizeof(variates); i++) {
if (variates[i] == get_variate)
break;
}
int length = position_right-position_left+1;
sentence.replace(position_left, length, to_string(values[i]));
position_left = sentence.find("{", position_left+1);
}
cout << sentence << endl;
}
return 0;
}