#include<iostream>
#include<malloc.h>
#include<queue>
#define MAXV 10
using namespace std;
typedef char E;
typedef queue<int>* Queue;
typedef struct Node{
int nextVex;
struct Node *next;
}*node;
queue<int> Q;
struct HeadNode{
E element;
struct Node *next;
};
typedef struct GraphTable{
int vex,edge;
struct HeadNode vertex[MAXV];
}*Graph;
Graph Create(){
Graph g = (Graph)malloc(sizeof(GraphTable));
g->edge=g->vex=0;
return g;
}
void addVex(Graph g,E e){
g->vertex[g->vex].element=e;
g->vertex[g->vex].next=NULL;
g->vex++;
}
int getIndexByElem(Graph g,E v){
int i=0;
while(g){
if(g->vertex[i].element==v)
return i;
i++;
}
}
void addEdge(Graph g,E v1,E v2){
int a = getIndexByElem(g,v1);
int b = getIndexByElem(g,v2);
node nd = g->vertex[a].next;
node newNode=(node)malloc(sizeof(Node));
newNode->next=NULL;
newNode->nextVex=b;
if(!nd) g->vertex[a].next = newNode;
else{
while(nd->next){
if(nd->nextVex==b) return;
nd =nd->next;
}
nd->next=newNode;
}
g->edge++;
}
char res[100];
void BFS(Graph g,int startVex,int targerVex,int *visited){
Q.push(startVex);
visited[startVex]=1;
int i=0;
while(!Q.empty()){
int next=Q.front();
Q.pop();
if (i!=0)cout<<" "<<g->vertex[next].element;
else cout<<g->vertex[next].element;
node nd =g->vertex[next].next;
while(nd){
if(!visited[nd->nextVex]){
Q.push(nd->nextVex);
visited[nd->nextVex]=1;
}
nd=nd->next;
}
i++;
}
cout<<endl;
}
int main(){
Graph graph= Create();
int n;
cin>>n;
char x;
for (int c=0;c<n;c++){
cin>>x;
addVex(graph,x);
}
char A,B;
while(cin>>A>>B)
addEdge(graph,A,B);
int arr[graph->vex];
for(int i=0;i<=graph->vex;i++) arr[i]=0;
BFS(graph,0,graph->vex,arr);
return 0;
}