#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <set>
using namespace std;
int main()
{
int n;
cin >> n;
string str1, str2, str3;
multimap<string, string> sub;
multimap<string, string> ins;
for (int i = 0; i < n; i++)
{
cin >> str1 >> str2 >> str3;
if (str2 == "subClassOf")
sub.insert(pair<string, string>(str3, str1));
else if (str2 == "instanceOf")
ins.insert(pair<string,string>(str3,str1));
}
string recv;
cin >> recv;
set<string> res;
for (auto i : sub)
{
if (i.first == recv)
{
for (auto j : ins)
{
if (j.first == i.second)
res.insert(j.second);
}
}
}
for (auto i : ins)
{
if (i.first == recv)
res.insert(i.second);
}
if (res.empty())
cout << "empty" << endl;
for (auto i : res)
{
cout << i << " ";
}
cout << endl;
}