#include<iostream>
using namespace std;
#define N 100
typedef char OtherInfo;
int visited[N]={0};
typedef struct ArcNode{
int adjvex;
OtherInfo info;
struct ArcNode* next;
}ArcNode;
typedef struct VNode{
char vex;
ArcNode* first;
}VNode,AdjList[N];
typedef struct{
AdjList vertices;
int vexnum,arcnum;
}ALGraph;
int LocateVex(ALGraph G,char v)
{
for(int i=0;i<G.vexnum;++i){
if(G.vertices[i].vex==v)return i;
}
return -1;
}
void CreatALGraph(ALGraph &G,int &err)
{
cin>>G.vexnum>>G.arcnum;
for(int i=0;i<G.vexnum;++i)
{
cin>>G.vertices[i].vex;
G.vertices[i].first=NULL;
}
for(int k=0;k<G.arcnum;++k){
char v1,v2;
cin>>v1>>v2;
int i,j;
i=LocateVex(G,v1);j=LocateVex(G,v2);
if(i==-1||j==-1)err=1;
else{
ArcNode* p1=new ArcNode;
p1->adjvex=j;
p1->next=G.vertices[i].first;
G.vertices[i].first=p1;
ArcNode *p2=new ArcNode;
p2->adjvex=i;
p2->next=G.vertices[j].first;
G.vertices[j].first=p2;
}
}
}
void DFS_ALGraph(ALGraph G,int adj)
{
cout<<G.vertices[adj].vex<<" ";
visited[adj]=1;
ArcNode* p=G.vertices[adj].first;
while(p!=NULL){
if(visited[p->adjvex]!=1)DFS_ALGraph(G,p->adjvex);
p=p->next;
}
}
int main()
{
ALGraph G;
int err=0;
char star;
CreatALGraph(G,err);
cin>>star;
int adj=LocateVex(G,star);
if(adj==-1||err==1)cout<<"error";
else{
DFS_ALGraph(G,adj);
}
return 0;
}