// Sample solution to Amiga problem
// Stein Norheim 2004
//

import java.io.*;
import java.util.*;

class InputSrc {
	StreamTokenizer tokens = null;
	
	public int getInteger() throws IOException {
		tokens.nextToken();
		return Integer.parseInt(tokens.sval);
	}
	
	public String getString() throws IOException {
		tokens.nextToken();
		return tokens.sval;
	}
	
	public InputSrc () throws FileNotFoundException {
		BufferedReader stdin = 
			new BufferedReader(new InputStreamReader(System.in));
		//		new BufferedReader(new FileReader("amiga2.in"));
    
		tokens = new StreamTokenizer(stdin);
		tokens.resetSyntax();
		tokens.whitespaceChars(0,32);
		tokens.wordChars(33,255);
	}
}

class Node {
	public int Id;
	public String Name;
	
	ArrayList sameAs = new ArrayList();
	ArrayList rightOf = new ArrayList();
	ArrayList leftOf = new ArrayList();
	ArrayList nextTo = new ArrayList();
	
	public Node(int id, String name) {
		Id = id;
		Name = name;
	}
	
	public void SetSameAs(Node otherNode) {
		if (!sameAs.contains(otherNode)) {
			sameAs.add(otherNode);
			otherNode.SetSameAs(this);
		}
	}
	
	public void SetLeftOf(Node otherNode) {
		if (!leftOf.contains(otherNode)) {
			leftOf.add(otherNode);
			otherNode.SetRightOf(this);
		}
	}
	
	public void SetRightOf(Node otherNode) {
		if (!rightOf.contains(otherNode)) {
			rightOf.add(otherNode);
			otherNode.SetLeftOf(this);
		}
	}
	
	public void SetNextTo(Node otherNode) {
		if (!nextTo.contains(otherNode)) {
			nextTo.add(otherNode);
			otherNode.SetNextTo(this);
		}
	}
}

class Relations {
	
	Node[] nodes;
	
	String[] namedConstants = {
			"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"};
	
	Map nameMap;
		
	
	public Relations() {
		nameMap = new HashMap();
		nodes = new Node[namedConstants.length];
		for (int i=0; i<namedConstants.length; i++) {
			Node currentNode = new Node(i, namedConstants[i]);
			nameMap.put(currentNode.Name, currentNode);
			nodes[i]=currentNode;
		}
	}
	
	public void ReadRelations(InputSrc input) throws IOException {
		int nRelations = input.getInteger();
		for (int i=0; i<nRelations; i++) {
			Node nodeA = (Node) nameMap.get(input.getString());
			String op = input.getString();
			Node nodeB = (Node) nameMap.get(input.getString());
			
			if (op.equals("same-as")) {
				nodeA.SetSameAs(nodeB);
			} else if (op.equals("left-of")) {
				nodeA.SetLeftOf(nodeB);
			} else if (op.equals("right-of")) {
				nodeA.SetRightOf(nodeB);
			} else if (op.equals("next-to")) {
				nodeA.SetNextTo(nodeB);
			}
		}
	}	
}

class Fact {
	Node FactNode;
	int Position;
	
	public Fact(Node node, int pos) {
		FactNode = node;
		Position = pos;
	}
}

class SolutionCandidate {
	int[] solution;
	
	LinkedList queue;
	
	public SolutionCandidate (SolutionCandidate src) {
		solution = (int[]) src.solution.clone();
			
		queue = src.queue;
	}
	
	public SolutionCandidate(Relations rel) {
		solution = new int[rel.nodes.length];
		queue = new LinkedList();
		
		for (int i=0; i<solution.length; i++)
			solution[i]=0;
		
		SetFact(rel.nodes[0],1);
		SetFact(rel.nodes[1],2);
		SetFact(rel.nodes[2],3);
		SetFact(rel.nodes[3],4);
		SetFact(rel.nodes[4],5);
		BFS();
	}
	
	public boolean SolutionValid() {
		return true;
	}
	
	public int GetAmigaOwner(amiga app) {
		Node amigaNode = (Node) app.rel.nameMap.get("amiga");
		int amigaRoom = solution[amigaNode.Id];
		
		if (amigaRoom>0) {
				
		    for (int cand=10; cand<15; cand++) {
			if (solution[cand]==amigaRoom)
				return cand;
		    }
		}
		return -1;
	}
	
	public void BackTrack(int base, amiga app) {
		int newOwner = GetAmigaOwner(app);

		if (base<solution.length) {
			
			if (newOwner>0 && app.amigaOwner==newOwner)
				return;
			
			for (int j=0; j<120 && app.amigaOwner != -1; j++) {
				boolean mightwork = true;
				for (int k=0; k<5; k++) {
					if (!(solution[base+k]==0 || solution[base+k]==app.permutations[j][k])) {
						mightwork = false;
						break;
					}
				}
				
				if (mightwork) {
					SolutionCandidate testSolution = new SolutionCandidate(this);
					boolean conflict = false;
					queue.clear();
					
					for (int k=0; k<5; k++) { 
						if (testSolution.SetFact(app.rel.nodes[base+k],app.permutations[j][k])) {
							conflict=true;
							break;
						}
							
					}
					if (!conflict) 
					  conflict = testSolution.BFS();
					
					if (!conflict) testSolution.BackTrack(base+5, app);
					
				}
			} 
	    }
	    else {
			if (newOwner >0) {
			    if (app.amigaOwner==-2 || app.amigaOwner == newOwner)
				app.amigaOwner=newOwner;
			    else
				app.amigaOwner=-1;
			}
    	
	    }
			
	}
	
	
	public boolean SetFact(Node node, int position) {
		if (solution[node.Id] == 0) {
		  solution[node.Id]=position;
		  queue.addLast(new Fact(node,position));
		} else {
			if (solution[node.Id] != position)
				return true;
		}
		return false;
	}
	
	public boolean BFS() {
		while (!queue.isEmpty()) {
			Fact currentFact = (Fact) queue.getFirst();
			queue.removeFirst();
			int i;
			
			Node node = currentFact.FactNode;
			
			for (i=0; i<node.sameAs.size(); i++) {
				Node otherNode = (Node) node.sameAs.get(i);
				
				if (SetFact(otherNode, currentFact.Position))
					return true;
			}
			
			for (i=0; i<node.leftOf.size(); i++) {
				Node otherNode = (Node) node.leftOf.get(i);
				
				if (SetFact(otherNode, currentFact.Position+1))
					return true;
			}
			for (i=0; i<node.rightOf.size(); i++) {
				Node otherNode = (Node) node.rightOf.get(i);
				
				if (SetFact(otherNode, currentFact.Position-1))
					return true;
			}
			
			for (i=0; i<node.nextTo.size(); i++) {
				Node otherNode = (Node) node.nextTo.get(i);
				int myPos = currentFact.Position;
				int otherPos = solution[otherNode.Id];
				
				if (otherPos != 0 && (myPos-otherPos != 1) && (myPos-otherPos != -1))
				  return true;
				if (otherPos == 0) {
					if (myPos==1) {
						if (SetFact(otherNode, 2))
							return true;
					}
						
					if (myPos==5) {
						if (SetFact(otherNode, 4))
							return true;
					}
				}
									
			}
		}
		return false;
	}
	
}

public class amiga{
	public int amigaOwner;

	int row=0;
	int currentRow[] = new int[5];
	public int permutations[][];
	Relations rel = null;

	void PermRow(int pos, int used) {
		if (pos<5) {
			for (int c=0; c<5; c++) {
				if ((used & (1 << c)) == 0) {
					currentRow[pos]=c+1;
					PermRow(pos+1, used | (1<<c));
				}
			}
		} else {
			for (int i=0; i<5; i++) {
			  permutations[row][i]=currentRow[i];
			}
			row++;
		}
	}

	void CreatePermutations() {
		permutations=new int[120][5];
		row = 0;
		PermRow(0,0);		
	
	}
	
	
	Map NameMap = new HashMap();
	public void realMain() throws IOException {
		CreatePermutations();
        
		InputSrc input = new InputSrc();
		
        int nCases=input.getInteger();
        
        for (int i=0; i<nCases; i++) {
        	System.out.print("scenario #"+(i+1)+": ");
        	amigaOwner = -2;
        	
        	rel = new Relations();
        	rel.ReadRelations(input);
        	
        	SolutionCandidate cand = new SolutionCandidate(rel);
        	cand.BackTrack(5, this);

        	if (amigaOwner > 0) {
        		System.out.println(rel.nodes[amigaOwner].Name+" owns the amiga.");
        	} else {
        		System.out.println("cannot identify the amiga owner.");
        	}
        }
	}
	
	
	public static void main(String[] args) throws IOException{
        amiga a = new amiga();
        a.realMain();
	}
}
