We will now try another problem inspired by one of Sam Loyd's puzzles.
A group of children made a lovely patch quilt that they gave as a gift to their teacher. Knowing their teacher is a puzzle aficionado, the kids created the quilt with a very particular feature: the names of everyone who contributed to the gift are hidden in the patchwork.
Figure 1: A present from her students
The teacher was happily surprised by the thoughtful gift and kept it in the classroom, where it is displayed and continues to pose a challenge to anyone who gazes upon it. The rules of the puzzle are simple:
- To find any name, you can start at any position, and at each step you may move in any direction, including diagonals.
- Any position in the puzzle can be used more than once. You can even choose not to move at some steps, to use the same letter in succession.
- For every name that is hidden in the puzzle, there is only one valid way to find it (there are no multiple solutions for any of the names).
You know the list of names of all the students. Your task is to identify their locations inside the quilt.
Input
Input starts with an integer T (≤ 200), denoting the number of test cases.
Each case starts with a line containing two integers R and C (1 ≤ R, C ≤ 30) denoting the number of rows and columns of the quild respectively. The following R lines contain C letters each, representing the contents of the puzzle. All letters are uppercase.
The next line contains an integer N (1 ≤ N ≤ 20), denoting the number of students. Each of the next N lines contains a name, containing uppercase letters. The length of each name will be at least 2, and at most 15. You can safely assume that if a certain name is found in the puzzle, there will only be one possible way to discover it.
Output
For each case, print the case number in a single line. Then, for each name given in the input, print if it is found in the puzzle or not.
If a name is found, print the message "NAME found:" followed by a description of its location, given in the following way:
- First print the coordinates of the first letter in the form (r,c) where 1 ≤ r ≤ R and 1 ≤ c ≤ C.
- Then, for each additional letter, print its location relative to the previous letter. Use the letters U, D, L, R for up, down, left and right respectively. For diagonals, use the following combinations: UL for up-left, UR for up-right, DL for down-left, and DR for down-right. To indicate no movement at all, use an asterisk (*).
- Make sure to use a comma and a single space between elements in the list. Notice, however, that there must be no spaces after the last move.
If a name is not found, simply print NAME not found
. For more details on the format of the output, please refer to the sample below.
Sample
Sample Input | Sample Output |
---|---|
1 4 5 KHPMT FNEOA RAHSJ YRTES 4 JAMES JOHN HANNAH MARIE | Case 1: JAMES found: (3,5), U, UL, DL, DR JOHN found: (3,5), UL, DL, UL HANNAH found: (3,3), L, U, *, D, R MARIE not found |