#include<bits/stdc++.h>
#define MAX 103
using namespace std;
vector<int>graph[MAX];
map<string,int>mp;
map<int,string>mp1;
int indeg[MAX];
int main()
{
int node,m,i,j,v,tc=0;
string s,s1;
while(scanf("%d",&node)!=EOF)
{
for(i=1; i<=node; i++)
{
cin>>s;
mp[s] = i;
mp1[i] = s;
}
scanf("%d",&m);
for(i=1; i<=m; i++)
{
cin>>s>>s1;
indeg[mp[s1]]++;
graph[mp[s]].push_back(mp[s1]);
}
printf("Case #%d: Dilbert should drink beverages in this order:",++tc);
for(i=1; i<=node; i++)
{
if(indeg[i]==0)
{
cout<<" "<<mp1[i];
indeg[i]--;
int sz = graph[i].size();
for(j=0;j<sz;j++)
{
v = graph[i][j];
indeg[v]--;
}
i = 0;
}
}
puts(".");
puts("");
for(i=0;i<=node;i++)
{
graph[i].clear();
}
memset(indeg,0,sizeof(indeg));
mp.clear();
mp1.clear();
}
return 0;
}