#include<bits/stdc++.h>
using namespace std;
#define MAX 100+10
map< int ,string> ms;
map< string,int > sm;
vector< int > edgelist[ MAX] ;
string s;
int n,i,m;
int main( ) {
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int cases= 0 ;
while ( scanf ( "%d" ,& n) ! = EOF ) {
for ( i= 1 ; i<= n; i++ ) {
cin >> s;
ms[ i] = s;
sm[ s] = i;
}
scanf ( "%d" ,& m) ;
vector< int > in( n+ 1 ,0 ) ;
for ( i= 1 ; i<= m; i++ ) {
string s1,s2;
cin >> s1;
cin >> s2;
edgelist[ sm[ s1] ] .push_back ( sm[ s2] ) ;
in[ sm[ s2] ] ++ ;
}
/*for(i=1;i<=n;i++){
for(int j=0;j<edgelist[i].size();j++)in[edgelist[i][j]]++;
}*/
//for(i=1;i<=n;i++)cout<<ms[i]<<" : "<<in[i]<<endl;
queue< int > q;
for ( i= 1 ; i<= n; i++ ) {
if ( in[ i] == 0 ) q.push ( i) ;
}
vector< int > top_sort;
while ( ! q.empty ( ) ) {
int u= q.front ( ) ;
q.pop ( ) ;
top_sort.push_back ( u) ;
for ( i= 0 ; i< edgelist[ u] .size ( ) ; i++ ) {
int v= edgelist[ u] [ i] ;
-- in[ v] ;
if ( in[ v] == 0 ) q.push ( v) ;
}
}
printf ( "Case #%d: Dilbert should drink beverages in this order: " ,++ cases) ;
//sort(top_sort.begin(),top_sort.end());
for ( i= 0 ; i< top_sort.size ( ) ; i++ ) {
cout << ms[ top_sort[ i] ] ;
if ( i! = top_sort.size ( ) - 1 ) cout << " " ;
}
printf ( ".\n \n " ) ;
for ( i= 1 ; i<= n; i++ ) edgelist[ i] .clear ( ) ;
ms.clear ( ) ,sm.clear ( ) ;
}
return 0 ;
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKI2RlZmluZSBNQVggMTAwKzEwCm1hcDxpbnQsc3RyaW5nPm1zOwptYXA8c3RyaW5nLGludD5zbTsKdmVjdG9yPGludD5lZGdlbGlzdFtNQVhdOwpzdHJpbmcgczsKaW50IG4saSxtOwppbnQgbWFpbigpewogICAgLy9mcmVvcGVuKCJpbi50eHQiLCJyIixzdGRpbik7CiAgICAvL2ZyZW9wZW4oIm91dC50eHQiLCJ3IixzdGRvdXQpOwogICAgaW50IGNhc2VzPTA7CiAgICB3aGlsZShzY2FuZigiJWQiLCZuKSE9RU9GKXsKICAgICAgICBmb3IoaT0xO2k8PW47aSsrKXsKICAgICAgICAgICAgY2luPj5zOwogICAgICAgICAgICBtc1tpXT1zOwogICAgICAgICAgICBzbVtzXT1pOwogICAgICAgIH0KICAgICAgICBzY2FuZigiJWQiLCZtKTsKICAgICAgICB2ZWN0b3I8aW50PmluKG4rMSwwKTsKICAgICAgICBmb3IoaT0xO2k8PW07aSsrKXsKICAgICAgICAgICAgc3RyaW5nIHMxLHMyOwogICAgICAgICAgICBjaW4+PnMxOwogICAgICAgICAgICBjaW4+PnMyOwogICAgICAgICAgICBlZGdlbGlzdFtzbVtzMV1dLnB1c2hfYmFjayhzbVtzMl0pOwogICAgICAgICAgICBpbltzbVtzMl1dKys7CiAgICAgICAgfQogICAgICAgIC8qZm9yKGk9MTtpPD1uO2krKyl7CiAgICAgICAgICAgIGZvcihpbnQgaj0wO2o8ZWRnZWxpc3RbaV0uc2l6ZSgpO2orKylpbltlZGdlbGlzdFtpXVtqXV0rKzsKICAgICAgICB9Ki8KICAgICAgICAvL2ZvcihpPTE7aTw9bjtpKyspY291dDw8bXNbaV08PCIgOiAiPDxpbltpXTw8ZW5kbDsKICAgICAgICBxdWV1ZTxpbnQ+cTsKICAgICAgICBmb3IoaT0xO2k8PW47aSsrKXsKICAgICAgICAgICAgaWYoaW5baV09PTApcS5wdXNoKGkpOwogICAgICAgIH0KICAgICAgICB2ZWN0b3I8aW50PnRvcF9zb3J0OwogICAgICAgIHdoaWxlKCFxLmVtcHR5KCkpewogICAgICAgICAgICBpbnQgdT1xLmZyb250KCk7CiAgICAgICAgICAgIHEucG9wKCk7CiAgICAgICAgICAgIHRvcF9zb3J0LnB1c2hfYmFjayh1KTsKICAgICAgICAgICAgZm9yKGk9MDtpPGVkZ2VsaXN0W3VdLnNpemUoKTtpKyspewogICAgICAgICAgICAgICAgaW50IHY9ZWRnZWxpc3RbdV1baV07CiAgICAgICAgICAgICAgICAtLWluW3ZdOwogICAgICAgICAgICAgICAgaWYoaW5bdl09PTApcS5wdXNoKHYpOwogICAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIHByaW50ZigiQ2FzZSAjJWQ6IERpbGJlcnQgc2hvdWxkIGRyaW5rIGJldmVyYWdlcyBpbiB0aGlzIG9yZGVyOiAiLCsrY2FzZXMpOwogICAgICAgIC8vc29ydCh0b3Bfc29ydC5iZWdpbigpLHRvcF9zb3J0LmVuZCgpKTsKICAgICAgICBmb3IoaT0wO2k8dG9wX3NvcnQuc2l6ZSgpO2krKyl7CiAgICAgICAgICAgIGNvdXQ8PG1zW3RvcF9zb3J0W2ldXTsKICAgICAgICAgICAgaWYoaSE9dG9wX3NvcnQuc2l6ZSgpLTEpY291dDw8IiAiOwogICAgICAgIH0KICAgICAgICBwcmludGYoIi5cblxuIik7CiAgICAgICAgZm9yKGk9MTtpPD1uO2krKyllZGdlbGlzdFtpXS5jbGVhcigpOwogICAgICAgIG1zLmNsZWFyKCksc20uY2xlYXIoKTsKICAgIH0KICAgIHJldHVybiAwOwp9Cg==
stdin
OAphCmIKYwpkCmUKZgpzCnQKNgphIGUKYSBiCmMgZAplIGYKcyB0CmEgdAoKOApzCmEKYgplCmYKYwpkCnQKNgphIGUKYSBiCmMgZAplIGYKcyB0CmEgdAoKOAphCmIKYwpzCnQKZQpmCmQKNgphIGIKYSBlCmMgZAplIGYKcyB0CmEgdAo=
8
a
b
c
d
e
f
s
t
6
a e
a b
c d
e f
s t
a t
8
s
a
b
e
f
c
d
t
6
a e
a b
c d
e f
s t
a t
8
a
b
c
s
t
e
f
d
6
a b
a e
c d
e f
s t
a t