#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#include<queue>
#include<map>
using namespace std;
int n,m;
const int maxn=1e2+;
string rule[maxn],value[maxn];
string s,t;
int lens,lent;
bool legal(){
for(int i=;i<lent;i++){
if(t[i]>='a'&&t[i]<='z') continue;
if(t[i]>='A'&&t[i]<='Z') continue;
if(t[i]>='.'||t[i]=='-'&&t[i]=='_') continue;
return false;
}
return true;
}
bool check(){
lens=s.length();
int i=,j=;
while(i<lens&&j<lent){
if(s[i]==t[j]){
i++;
j++;
continue;
}
if(s[i]!='<') return false;
if(s[i+]=='i'){
i+=;
while(j<lent){
if(t[j]>=''&&t[j]<='') j++;
else if(t[j]=='/') break;
else return false;
}
}else if(s[i+]=='s'){
i+=;
while(j<lent){
if(t[j]!='/') j++;
else break;
}
}else if(s[i+]=='p'){
return true;
}
}
if(i!=lens||j!=lent) return false;
return true;
}
void print(int id){
cout<<value[id];
int i=,j=;
while(i<lens&&j<lent){
if(s[i]==t[j]){
i++;
j++;
continue;
}
if(s[i+]=='i'){
cout<<" ";
i+=;
int flag=;
while(j<lent){
if(t[j]=='/') break;
if(flag&&t[j]==''){
j++;
continue;
}
cout<<t[j];
flag=;
j++;
}
}else if(s[i+]=='s'){
i+=;
cout<<" ";
while(j<lent){
if(t[j]=='/') break;
cout<<t[j];
j++;
}
}else if(s[i+]=='p'){
cout<<" ";
while(j<lent){
cout<<t[j];
j++;
}
}
}
cout<<endl;
}
void work(){
lent=t.length();
if(!legal()){
printf("404\n");
return;
}
for(int i=;i<=n;i++){
s=rule[i];
if(check()){
print(i);
return;
}
}
printf("404\n");
return;
}
int main(){
while(~scanf("%d%d",&n,&m)){
for(int i=;i<=n;i++){
cin>>rule[i]>>value[i];
}
for(int i=;i<=m;i++){
cin>>t;
work();
}
}
return ;
}