#include <bits/stdc++.h>
using namespace std;
#define UNVISITED -1
struct caller_t: set< int >
{
string name; bool visited; int least, rank;
caller_t( const string& s ) :
name( s ), visited( false ), least( UNVISITED ), rank( UNVISITED ) {}
void update_least( int least_v )
{
least = min( least, least_v );
}
bool calling_circle() const
{
return least == rank;
}
void visit( int u )
{
least = rank = u, visited = true;
}
void write()
{
cout << name, visited = false;
}
};
typedef vector< caller_t > callers_t;
struct used_names_t: map< string, int >
{
int read( callers_t& callers )
{
string name; cin >> name;
auto it = find( name ); int index = size();
if ( it != end() )
index = it->second;
else
emplace( name, index ), callers.emplace_back( name );
return index;
}
};
struct calling_circle_t: stack< int >
{
void write( callers_t& s, int u )
{
for( int v; ( s[ v = top() ].write(), pop(), v ) != u; cout << ", " );
cout << '\n';
}
};
struct data_set: callers_t
{
used_names_t used_names; int visited; calling_circle_t calling_circle;
data_set() : visited( 0 ) {}
void phone_call()
{
int caller = used_names.read( *this ),
called = used_names.read( *this );
at( caller ).insert( called );
}
void dfs( int u )
{
at( u ).visit( visited++ ), calling_circle.push( u );
for( auto v: at( u ) )
{
if ( at( v ).rank == UNVISITED )
dfs( v );
if ( at( v ).visited )
at( u ).update_least( at( v ).least );
}
if ( at( u ).calling_circle() )
calling_circle.write( *this, u );
}
};
int main()
{
ios_base::sync_with_stdio( false ), cin.tie( nullptr ), cout.tie( nullptr );
for( int test = 1; ; test++ )
{
int n, m; cin >> n >> m;
if ( n == 0 and m == 0 )
return 0;
data_set s;
for( int i = 0; i < m; i++ )
s.phone_call();
if ( test > 1 )
cout << '\n';
cout << "Calling circles for data set " << test << ':' << '\n';
for( int i = 0; i < n; i++ )
if ( s[ i ].rank == UNVISITED )
s.dfs( i );
}
}