#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <stack>
usingnamespace std;
vector<bool> visited(30, false);
stack<int> list;
//The dfs algorithm
void dfs(vector<vector<int> > adj, int c)
{
visited[c] = true;
int size = adj[c].size();
for (int i = size - 1; i > 0; i--)
{
int j = adj[c][i];
if (!visited[j])
{
dfs(adj, j);
}
}
list.push(c);
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
//Input number of tasks
int N;
cin >> N;
cin.ignore();
//Input the instructions
vector<string> s(N);
for (int i = 0; i < N; i++)
{
getline(cin, s[i]);
}
//Input the vertexes for graph and put into adjacency list
vector<int> temp;
vector<vector<int> > adj(N, vector<int>(1));
int a, b;
while (cin >> a >> b)
{
adj[a].push_back(b);
temp.push_back(b);
}
//Sort each adjacency list
for (int i = 0; i < N; i++)
{
if (!adj[i].empty())
{
sort(adj[i].begin(), adj[i].end());
}
}
//Find the first in-degree vertex and do dfs on it
sort(temp.begin(), temp.end());
temp.erase(unique(temp.begin(), temp.end()), temp.end());
int count = 0;
for (int i = N - 1; i > 0; i--)
{
if (temp[count] != i)
{
dfs(adj, i);
}
else
{
count++;
}
}
//Print the resulting stack from dfs
while (!list.empty())
{
cout << s[list.top()] << "\n";
list.pop();
}
return 0;
}