#include <string>
#include <iostream>
#include <vector>
#include <stdlib.h>
#include <iomanip>

//#define DEBUG

using namespace std;

struct Fact
{
	int prop1type,prop1;
	int prop2type,prop2;
	int relation;
	Fact() { }
};

int amigaOwner;
int house[5][6];     // house[houseNo][propType] => property (-1 == dunno)
int whereProp[6][5]; // whereProp[propType][prop] => house (-1 == dunno)

vector<Fact*> propFacts[6][5]; // propFacts[type][prop] => which facts concerns this prop

char *props[]={
	"1","2","3","4","5",
	"blue","green","red","white","yellow",
	"anna","bernhard","chris","david","ellen",
	"danish","finnish","icelandic","norwegian","swedish",
	"amiga","atari","linux","mac","windows",
	"c","c++","java","pascal","perl"};

int totVerifications=0; // Performance counter

bool verify(vector<Fact*> v)
{
	for(int i=0;i<v.size();i++) {
		totVerifications++;
		int h1=whereProp[v[i]->prop1type][v[i]->prop1];
		int h2=whereProp[v[i]->prop2type][v[i]->prop2];		
		if (h1>=0 && h2>=0) {
			switch (v[i]->relation) {
				case 0 : if (h1!=h2) return false; break;
				case 1 : if (h2-h1!=1) return false; break;
				case 2 : if (h1-h2!=1) return false; break;
				case 3 : if (h1-h2!=1 && h2-h1!=1) return false; break;
			}
		}
	}
	return true;
}

bool go()
{
	if (amigaOwner>=0 && whereProp[4][0]>=0 && house[whereProp[4][0]][2]==amigaOwner)
		return false;
		
	int best=-1,type,prop;
	// Find best property to place...
	for(int i=1;i<6;i++)
		for(int j=0;j<5;j++)
			if (whereProp[i][j]<0) {
				int poss=0;
				for(int k=0;k<5;k++)
					if (house[k][i]<0) {
						whereProp[i][j]=k;
						if (verify(propFacts[i][j])) poss++;
						whereProp[i][j]=-1;
					}
				if (!poss) return false;
				if (best<0 || poss<best) {
					best=poss;
					type=i;
					prop=j;
				}
			}
	if (best<0) {
		int owner=house[whereProp[4][0]][2];
#ifdef DEBUG
		if (owner!=amigaOwner) { // Print out different Solutions
			for(int i=0;i<6;i++) {
				for(int j=0;j<5;j++)
					cout << setw(10) << props[i*5+house[j][i]];
				cout << endl;
			}
			cout << endl;
		}
#endif
		if (amigaOwner>=0 && amigaOwner!=owner) {
			amigaOwner=-1; // Different answers
			return true;
		}
		amigaOwner=owner;
		return false; // Keep looking for multiple solutions
	}
	
	// ...and try all places it fits
	for(int k=0;k<5;k++)
		if (house[k][type]<0) {
			whereProp[type][prop]=k;
			if (verify(propFacts[type][prop])) {
				house[k][type]=prop;
				if (go()) return true;
				house[k][type]=-1;
			}
			whereProp[type][prop]=-1;
		}
		
	return false;
}

int main()
{
	int T,F;
	string s1,s2,s3;
	cin >> T;
		
	for(int scenarioNo=1;scenarioNo<=T;scenarioNo++) {		
		for(int i=0;i<6;i++)
			for(int j=0;j<5;j++) {
				propFacts[i][j].clear();
				house[j][i]=i?-1:j;
				whereProp[i][j]=i?-1:j;
			}
		
		cin >> F;
		vector<Fact*> allFacts(F);
		for(int i=0;i<F;i++) {
			cin >> s1 >> s2 >> s3;			
			Fact *f=new Fact();
			allFacts[i]=f;
			f->prop1=f->prop2=f->relation=-1;
			for(int j=0;j<30;j++) {
				if (s1==props[j])
					propFacts[f->prop1type=j/5][f->prop1=j%5].push_back(f);
				if (s3==props[j])
					propFacts[f->prop2type=j/5][f->prop2=j%5].push_back(f);
			}
			if (s2=="same-as") f->relation=0;
			else if (s2=="left-of") f->relation=1;
			else if (s2=="right-of") f->relation=2;
			else if (s2=="next-to") f->relation=3;
			
			if (f->prop1<0 || f->prop2<0 || f->relation<0) {
				cerr << "Error in input: " << s1 << " " << s2 << " " << s3 << endl;
				exit(-1);
			}			
		}
		
		amigaOwner=-1;
		if (verify(allFacts)) // Make sures "2 left-of 1" conditions are checked
			go();

		cout << "scenario #" << scenarioNo << ": ";
		if (amigaOwner<0)
			cout << "cannot identify the amiga owner." << endl;
		else
			cout << props[amigaOwner+10] << " owns the amiga." << endl;
	}			

#ifdef DEBUG	
	cout << "Number of verifications done: " << totVerifications << endl;
#endif	
	
	return 0;
}
