#include<bits/stdc++.h>
using namespace std;
int numNode, numEdge;
map<char, int> node;
vector<char> id;
vector<vector<int>> adj;
void bfs(char start, char finish) {
queue<int> q;
vector<int> pre(numNode, -2);
pre[node[start]] = -1;
q.push(node[start]);
bool hasPath = false;
while(!q.empty()) {
int u = q.front();
q.pop();
if(u == node[finish]) {
hasPath = true;
break;
}
for(int v : adj[u]) if(pre[v] == -2) {
pre[v] = u;
q.push(v);
}
}
if(hasPath == false) {
cout << "no_path\n";
return;
}
vector<char> path;
int u = node[finish];
while(u != -1) {
path.push_back(id[u]);
u = pre[u];
}
reverse(path.begin(), path.end());
for(char x : path) cout << x << " ";
cout << '\n';
}
int main() {
cin >> numNode >> numEdge;
id.resize(numNode);
adj.resize(numNode);
for(int i = 0; i < numNode; i++) {
char x;
cin >> x;
node[x] = i;
id[i] = x;
}
for(int i = 0; i < numEdge; i++) {
char x, y;
cin >> x >> y;
adj[node[x]].push_back(node[y]);
}
int numQuery;
cin >> numQuery;
for(int i = 0; i < numQuery; i++) {
char start, finish;
cin >> start >> finish;
bfs(start, finish);
}
return 0;
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCmludCBudW1Ob2RlLCBudW1FZGdlOwptYXA8Y2hhciwgaW50PiBub2RlOwp2ZWN0b3I8Y2hhcj4gaWQ7CnZlY3Rvcjx2ZWN0b3I8aW50Pj4gYWRqOwp2b2lkIGJmcyhjaGFyIHN0YXJ0LCBjaGFyIGZpbmlzaCkgewogICAgcXVldWU8aW50PiBxOwogICAgdmVjdG9yPGludD4gcHJlKG51bU5vZGUsIC0yKTsKICAgIHByZVtub2RlW3N0YXJ0XV0gPSAtMTsKICAgIHEucHVzaChub2RlW3N0YXJ0XSk7CiAgICBib29sIGhhc1BhdGggPSBmYWxzZTsKICAgIHdoaWxlKCFxLmVtcHR5KCkpIHsKICAgICAgICBpbnQgdSA9IHEuZnJvbnQoKTsKICAgICAgICBxLnBvcCgpOwogICAgICAgIGlmKHUgPT0gbm9kZVtmaW5pc2hdKSB7CiAgICAgICAgICAgIGhhc1BhdGggPSB0cnVlOwogICAgICAgICAgICBicmVhazsKICAgICAgICB9CiAgICAgICAgZm9yKGludCB2IDogYWRqW3VdKSBpZihwcmVbdl0gPT0gLTIpIHsKICAgICAgICAgICAgcHJlW3ZdID0gdTsKICAgICAgICAgICAgcS5wdXNoKHYpOwogICAgICAgIH0KICAgIH0KICAgIGlmKGhhc1BhdGggPT0gZmFsc2UpIHsKICAgICAgICBjb3V0IDw8ICJub19wYXRoXG4iOwogICAgICAgIHJldHVybjsKICAgIH0KICAgIHZlY3RvcjxjaGFyPiBwYXRoOwogICAgaW50IHUgPSBub2RlW2ZpbmlzaF07CiAgICB3aGlsZSh1ICE9IC0xKSB7CiAgICAgICAgcGF0aC5wdXNoX2JhY2soaWRbdV0pOwogICAgICAgIHUgPSBwcmVbdV07CiAgICB9CiAgICByZXZlcnNlKHBhdGguYmVnaW4oKSwgcGF0aC5lbmQoKSk7CiAgICBmb3IoY2hhciB4IDogcGF0aCkgY291dCA8PCB4IDw8ICIgIjsKICAgIGNvdXQgPDwgJ1xuJzsKfQppbnQgbWFpbigpIHsKICAgIGNpbiA+PiBudW1Ob2RlID4+IG51bUVkZ2U7CiAgICBpZC5yZXNpemUobnVtTm9kZSk7CiAgICBhZGoucmVzaXplKG51bU5vZGUpOwogICAgZm9yKGludCBpID0gMDsgaSA8IG51bU5vZGU7IGkrKykgewogICAgICAgIGNoYXIgeDsKICAgICAgICBjaW4gPj4geDsKICAgICAgICBub2RlW3hdID0gaTsKICAgICAgICBpZFtpXSA9IHg7CiAgICB9CiAgICBmb3IoaW50IGkgPSAwOyBpIDwgbnVtRWRnZTsgaSsrKSB7CiAgICAgICAgY2hhciB4LCB5OwogICAgICAgIGNpbiA+PiB4ID4+IHk7CiAgICAgICAgYWRqW25vZGVbeF1dLnB1c2hfYmFjayhub2RlW3ldKTsKICAgIH0KICAgIGludCBudW1RdWVyeTsKICAgIGNpbiA+PiBudW1RdWVyeTsKICAgIGZvcihpbnQgaSA9IDA7IGkgPCBudW1RdWVyeTsgaSsrKSB7CiAgICAgICAgY2hhciBzdGFydCwgZmluaXNoOwogICAgICAgIGNpbiA+PiBzdGFydCA+PiBmaW5pc2g7CiAgICAgICAgYmZzKHN0YXJ0LCBmaW5pc2gpOwogICAgfQogICAgcmV0dXJuIDA7Cn0=