//刚开始写只有50分 因为读入变量的值的时候可能存在空格,用流读的话,会将空格忽略,
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <cstdio>
#include <sstream>
using namespace std;
int main() {
int n, m;
vector<string> v;
map<string, string> dict;
int pre, nxt;
cin >> n >> m;
cin.get();
string tmp, s1, s2;
for(int i=1; i<=n; i++) {
getline(cin, tmp);
v.push_back(tmp);
}
for(int i=1; i<=m; i++) {
/*string s;
getline(cin, s);
stringstream ss(s);
ss >> s1;
ss >> s2;//s2可能存在“ ”
dict[s1] = s2.substr(1, s2.length()-2);//去掉“”*/
cin >> s1;
getline(cin, s2);
dict[s1] = s2.substr(2, s2.length()-3);//因为s2读入“ ”
}
for(int i=0; i<n; i++) {
pre = 0;
while((pre = v[i].find("{{ ", pre)) != string::npos &&(nxt = v[i].find(" }}", pre)) != string::npos) {
s1 = v[i].substr(pre + 3, nxt - pre - 3);
v[i].replace(pre, nxt - pre + 3, dict[s1]);
pre += dict[s1].length(); //继续向后查询
}
cout << v[i] << endl;
}
return 0;
}