#include <iostream>
#include <queue>
#include <stack>
using namespace std;
struct state{
state* parent;
char word[16];
};
void result_show(state* tmp){
if(tmp == NULL)
return;
result_show(tmp->parent);
cout << "use recursive : " << tmp->word << endl;
}
bool doublet(char* a, char* b){
if(strlen(a) != strlen(b))
return false;
int cnt = 0;
for(int i = 0; i < (int)strlen(a); i++)
if(a[i] != b[i])
cnt++;
if(cnt == 1)
return true;
return false;
}
int main(){
int dic_cnt;
int testcase_cnt, testcase;
int i;
cin >> dic_cnt >> testcase_cnt;
char** dic = (char**)malloc(sizeof(char*) * dic_cnt);
for(i = 0; i < dic_cnt; i++){
dic[i] = (char*)malloc(sizeof(char) * 16);
cin >> dic[i];
}
/*for(i = 0; i < dic_cnt; i++)
cout << dic[i] << endl;*/
for(testcase = 0; testcase < testcase_cnt; testcase++){
queue<state> openQ, closeQ;
stack<state> sequence_stack;
char start_word[16], end_word[16];
cin >> start_word >> end_word;
// start state generate
state start_state;
start_state.parent = NULL;
strcpy_s(start_state.word, start_word);
openQ.push(start_state);
while(true){
if(openQ.empty()){
cout << "No Solution" << endl;
break;
}
state current_state = openQ.front();
openQ.pop();
if(!strcmp(current_state.word, end_word)){// goal check
//cout << "goal : " << current_state.word << endl;
result_show(¤t_state);
sequence_stack.push(current_state);
state* tmp = current_state.parent;
while(tmp){
sequence_stack.push(*tmp);
tmp = tmp->parent;
}
cout << "goal 1: " << endl;
while(!sequence_stack.empty()){
cout << sequence_stack.top().word << endl;
sequence_stack.pop();
}
break;
}
// insert current state to close Q
closeQ.push(current_state);
state* current_state_ptr = &closeQ.back();
// make new state
for(i = 0; i < dic_cnt; i++){
if(doublet(current_state.word, dic[i])){ // current state equal dic's
if(current_state.parent != NULL){
if(!strcmp(current_state.parent->word, dic[i]))
continue;
}
state new_state;
new_state.parent = current_state_ptr;
strcpy_s(new_state.word, dic[i]);
openQ.push(new_state);
}
}
}
}
}