#include<bits/stdc++.h> #define endl '\n' using i64 = long long; using namespace std; signed main(){ //std::ios::sync_with_stdio(false);cin.tie(0); int n, m; cin >> n >> m; getchar(); vector<string> str(n + 1); map<string, int> idx; // 变量 -> 编号 for (int i = 1; i <= n;i++){ getline(cin, str[i]); int num = str[i].size(); string name = ""; for (int j = num - 2; j >= 0;j--){ if(str[i][j]==' ') break; name = str[i][j] + name; } idx[name] = i; } while(m--){ string name, s, opt = ""; getline(cin, name); int num = name.size(); for (int i = 0; i < num - 1; i++) if (name[i] == '.' && name[i + 1] == 'f') opt = "1" + opt; else if (name[i] == '.' && name[i + 1] == 's') opt = "2" + opt; for (int i = 0; i < num;i++) if(name[i]=='.'){ name = name.substr(0, i); break; } int cnt = idx[name]; s = str[cnt]; num = s.size(); while(opt!=""){ num = s.size(); stack<char> stk; int end1, end2; for (int i = 4; i < num;i++){ if(s[i]=='<') stk.push(s[i]); if(s[i]=='>') stk.pop(); if (s[i] == ','&&stk.size()==1){ end1 = i; break; } } for (int i = num - 1; i >= 0;i--) if(s[i]=='>'){ end2 = i; break; } if (opt.back() == '1') s = s.substr(5, end1 - 5); else s = s.substr(end1 + 1, end2 - end1 - 1); opt.pop_back(); } //cout << s << ' ' << opt << endl; num = s.size(); for (int i = 0; i < num;i++) if(s[i]==' '){ s = s.substr(0, i); break; } cout << s << endl; } return 0; }