#include <iostream>
#include <string>
#include <map>
using namespace std;

int main() {
	
	int T, N, i, count=0, test=1;
	string input1, input2, start, end1;
	map<string, string> fl;
	map<string,string>::iterator it, it1;

	cin>>T;
	while(T--)
	{
		cin>>N;
		
		for(i=0; i<N; i++)
		{
			cin>>input1>>input2;
			fl.insert(std::pair<string,string>(input1,input2));
		}

	
		for(it=fl.begin(); it != fl.end(); ++it)
		{
			start = it->first;
			end1 = it->second;
			count = 0;

			while(fl.find(end1) != fl.end())
			{
				it1 = fl.find(end1);
				end1 = it1->second;
				count += 1;
			}
		
			if(count == N-1)
				break;
		}

		it1 = fl.find(start);

		cout<<"Case #"<<test<<": ";
		while(1)
		{
			start = it1->first;
			end1 = it1->second;
			cout<<start<<"-"<<end1<<" ";
		
			if(fl.find(end1) == fl.end())
				break;
			else
				it1 = fl.find(end1);
		}
		cout<<"\n";
		test += 1;
		fl.clear();
	}
	return 0;
}