Under construction

Still being built. Things may change

More Fun in Bicol

Time limit5sMemory limit128 MB

Summary
Given unit compass offsets between named places, answer each query with the direction from one place to the other or report it as unknown.
Level

Medium4 of 10

Topics
Union-find, Graph
Solved
No attempts yet

Problem

The Bicol region (Region 5) has six provinces: Albay, Camarines Norte, Camarines Sur, Sorsogon, and the island provinces of Catanduanes and Masbate. Its cities are Iriga City, Legazpi City, Ligao City, Masbate City, Naga City, Sorsogon City, and Tabaco City.

Figure H-1: map of the Bicol region (the positions are drawn for illustration only)

Most of the contestants could not wait for the competition to end and started planning trips around the region. They keep asking each other where a place is, and they are writing down the relative location of one place to another.

Every place sits at a point on the plane, with east along +x+x and north along +y+y. Each of the eight relative positions is one unit step.

relative positionoffset (dx,dy)(dx, dy)
north(0,1)(0, 1)
south(0,−1)(0, -1)
east(1,0)(1, 0)
west(−1,0)(-1, 0)
northeast(1,1)(1, 1)
northwest(−1,1)(-1, 1)
southeast(1,−1)(1, -1)
southwest(−1,−1)(-1, -1)

Saying that place AA is north of place BB means the coordinate of AA minus the coordinate of BB equals (0,1)(0, 1). A relative position counts one unit on every axis it names, so northeast is one north and one east, and two pairs of places joined by the same relative position are the same distance apart.

Offsets add along a chain. If AA is northeast of BB and BB is west of CC, then the coordinate of AA minus the coordinate of CC is (1,1)+(−1,0)=(0,1)(1, 1) + (-1, 0) = (0, 1), so AA is north of CC. Answer every query from the signs of the accumulated offset.

Input

The input has several test cases, and each test case holds two data sets. The first data set describes the relative location of one place to another. The second data set asks queries on the relative location of one place to another.

The first line of a test case has two integers mm and nn, the number of entries in the first and the second data set, where 1≤m≤1001 \le m \le 100 and 1≤n≤501 \le n \le 50.

The next mm lines are position descriptions written as place 1,place 2,relative position. Such an entry says that place 1 is that relative position of place 2. The reverse entry never appears, so an input that carries (Naga City, City of Iriga, Northwest) never carries (City of Iriga, Naga City, Southeast) as well.

The next nn lines are queries written as place 1,place 2, and each one asks for the relative location of place 1 to place 2.

The fields are comma delimited, may carry spaces around them, and may mix upper and lower case. Two names mean the same place when they match once the surrounding spaces are dropped and case is ignored. A relative position is one of north, south, east, west, northeast, northwest, southeast, southwest. The entries of one test case never contradict each other.

A blank line separates two test cases, and a line with the single integer 0 follows the last test case.

Output

For each test case, print Case k on a line of its own first, where kk counts the test cases from 1. Then print one line per query, in the order the queries appear.

When the entries connect the two queried places and the accumulated offset is not (0,0)(0, 0), print <place 1> is <relative position> of <place 2>. Write both names the way the query writes them once the surrounding spaces are dropped, and write the relative position in lower case.

When the entries carry too little information to connect the two places, or when they put both names on the same point so that none of the eight relative positions fits, print Relative location cannot be determined instead.

The output has no blank lines.

Examples1

  1. Example 1

    Input
    5 4
    Naga City,City of Iriga,Northwest
    Masbate City,Naga City,Southeast
    Tabaco City,City of Ligao,Northeast
    Sorsogon City,Legazpi City,Southeast
    City of Ligao,Legazpi City,West
    Naga City,City of Iriga
    City of Iriga, Naga City
    Sorsogon City, Naga City
    Sorsogon City, City of Ligao
    
    0
    
    Expected output
    Case 1
    Naga City is northwest of City of Iriga
    City of Iriga is southeast of Naga City
    Relative location cannot be determined
    Sorsogon City is southeast of City of Ligao