Back and Forth

No attempts yetTime limit3sMemory limit128 MB

Problem

The neighborhood kids invented another odd game. Everybody runs back and forth inside a marked field, and nobody may turn at will: every step goes straight along one of the four compass directions.

The field is a grid of MM columns and NN rows. A position is a pair (x,y)(x, y) with 0x<M0 \le x < M and 0y<N0 \le y < N, and (0,0)(0, 0) is the bottom-left corner. A step N raises yy by one, a step S lowers yy by one, a step E raises xx by one, and a step W lowers xx by one.

The rules are as follows.

  • Before the game starts, one kid is chosen to be "it".
  • The game runs for a fixed number of rounds. In each round every player takes exactly one step in the direction they are facing, and all players step at the same time.
  • A player whose next step would leave the field reverses direction before taking that step. N and S are opposites, and so are E and W.
  • Once everybody has stepped, look at the squares holding more than one player. If three or more players share a square, all of them reverse direction. If exactly two share a square, they trade directions like this: the player with the longer name turns to face the direction the other one just stepped in, and the player with the shorter name turns to face the reverse of the direction the longer-named player just stepped in. Both new directions are read off the step that just finished. The rule works because no two kids in the neighborhood have names of the same length.
  • After the last round the winner is the player other than "it" whose straight-line distance to "it" is smallest. If two players are equally close, the one with the shorter name wins.

John finds the game silly, because the starting layout already settles the outcome. Write the program that works out the winner for him, so he can show the other kids and talk them into playing something better.

The figure below shows the starting layout of the first sample game.

Input

The first line holds the number of games TT. Each game is given as follows.

The first line of a game holds three integers MM, NN, and PP: the width of the field, its height (both counted in squares), and the number of players.

Each of the next PP lines holds a name, the xx and yy coordinates of that player's starting square, and the direction the player faces at the start, separated by spaces. The direction is one of N, S, E, and W. No two players start on the same square, and the player listed first is "it".

The last line of a game holds the number of rounds RR.

Constraints:

  • 1T1001 \le T \le 100
  • 2M92 \le M \le 9, 2N92 \le N \le 9
  • 2P92 \le P \le 9 and PM×NP \le M \times N
  • 0x<M0 \le x < M, 0y<N0 \le y < N
  • 1R10001 \le R \le 1000
  • Every name is 1 to 20 English letters, and within one game no two names have the same length.

Output

For each game print one line reading Case x: name, where xx is the number of the game counting from 1 and name is the winner's name.