Under construction

Still being built. Things may change

Who owns the Amiga?

Time limit1sMemory limit128 MB

Summary
Given constraint lines over five rooms, decide whether the Amiga owner is uniquely determined and print that student, or state it cannot be found.
Level

Medium4 of 10

Topics
Brute force, Implementation, Backtracking, Simulation
Solved
No attempts yet

Problem

In a corridor of a student dormitory there are five rooms numbered 1, 2, 3, 4 and 5; room 1 is the left-most room. Each room has a door of a different colour — blue, green, red, white and yellow — but not necessarily in that order.

In these rooms live five students — Anna, Bernhard, Chris, David and Ellen — of five different nationalities — Danish, Finnish, Icelandic, Norwegian and Swedish. (Both the names and the nationalities are listed in alphabetical order, so it does not follow that Anna is Danish.)

Each student owns one computer, and the computers are of five different kinds — Amiga, Atari, Linux, Mac and Windows (again in alphabetical order). Each student also has a favourite programming language — C, C++, Java, Pascal and Perl (also alphabetical).

Given some facts about the students, determine who owns the Amiga computer.

Input

The first line contains an integer TT (1≤T≤10001 \le T \le 1000), the number of scenarios.

Each scenario begins with a line containing an integer FF (1≤F≤20001 \le F \le 2000), the number of fact lines that follow. Each fact line contains three words separated by one or more spaces.

The first and third word are each one of the following (all lower-case, no capital letters):

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

The second word is a relationship, one of:

same-as left-of right-of next-to

  • same-as — the first and third words describe the same room. For example, blue same-as bernhard says Bernhard lives in the room with the blue door.
  • left-of — the first word's room is immediately to the left of the third word's room. For example, chris left-of perl says Chris lives in the room immediately to the left of the Perl programmer.
  • right-of — the first word's room is immediately to the right of the third word's room.
  • next-to — the two words describe rooms next to each other. For example, swedish next-to linux says the Swedish student lives immediately to the left or right of the Linux owner.

The input for each scenario is consistent: there is always at least one arrangement that satisfies all of its facts.

Output

For each scenario, print one line beginning with

scenario #n:

where n is the scenario number (starting at 1). If the facts determine a unique owner of the Amiga, continue the line with

xxxx owns the amiga.

where xxxx is that student's name (anna, bernhard, chris, david or ellen). If the owner cannot be determined uniquely, instead print

cannot identify the amiga owner.

Examples4

  1. Example 1

    Input
    2
    8
    red same-as 1
    danish same-as 1
    perl same-as 5
    atari same-as 2
    linux same-as 3
    mac same-as 4
    windows same-as 5
    anna same-as 1
    8
    chris left-of amiga
    chris left-of 4
    c same-as 1
    danish same-as 1
    red same-as 1
    linux same-as red
    windows next-to linux
    mac left-of swedish
    
    Expected output
    scenario #1: anna owns the amiga.
    scenario #2: cannot identify the amiga owner.
    
  2. Example 2

    Input
    1
    2
    amiga same-as 3
    bernhard same-as 3
    
    Expected output
    scenario #1: bernhard owns the amiga.
    
  3. Example 3

    Input
    1
    1
    amiga same-as 2
    
    Expected output
    scenario #1: cannot identify the amiga owner.
    
  4. Example 4

    Input
    1
    2
    amiga right-of 4
    ellen same-as 5
    
    Expected output
    scenario #1: ellen owns the amiga.