Where's Waldorf?

No attempts yetTime limit1sMemory limit128 MB

Problem

You are given a grid of letters with $m$ rows and $n$ columns, together with a list of words. For each word, find where it appears in the grid.

A word appears in the grid if its letters lie along a straight, uninterrupted line of grid cells. Matching is case-insensitive. The line may run in any of the eight directions: horizontally (left or right), vertically (up or down), or along either diagonal.

Input

The first line contains a single integer $T$, the number of test cases.

Each test case has the following form:

  • A line with two integers $m$ and $n$ ($1 \le m \le 50$, $1 \le n \le 50$).
  • $m$ lines follow, each containing exactly $n$ letters; this is the grid of letters. Letters may be upper- or lower-case.
  • A line with a single integer $k$ ($1 \le k \le 20$), the number of words to search for.
  • $k$ lines follow, one word per line. Each word consists of upper- and lower-case letters only (no spaces, hyphens, or other characters) and has length at least $1$.

Output

For each word, output two integers separated by a single space: the row and column of the cell that holds the first letter of the word.

Rows are numbered from $1$ (top) to $m$ (bottom); columns from $1$ (left) to $n$ (right).

If a word occurs more than once, report the occurrence whose first letter is closest to the top of the grid; if several are equally high, report the leftmost among them. In other words, choose the first-letter cell that comes earliest in row-major order (top to bottom, then left to right).

If a word does not appear at all, output $m$ and $n$ instead.

Print the results of one test case on consecutive lines, and separate the outputs of different test cases with a single blank line.