#include <iostream>
#include <string.h>
using namespace std;
#define MAX_CHAR_IN_LINE 50
static bool EastFind(char input[MAX_CHAR_IN_LINE][MAX_CHAR_IN_LINE], int rows, int cols, char* word, int wordLen, int curRow, int curCol)
{
if ((curCol + wordLen) > cols)
return false;
for (int i = 0; i < wordLen; ++i)
{
if (word[i] != input[curRow][curCol + i])
return false;
}
return true;
}
static bool WestFind(char input[MAX_CHAR_IN_LINE][MAX_CHAR_IN_LINE], int rows, int cols, char* word, int wordLen, int curRow, int curCol)
{
if (curCol < (wordLen - 1))
return false;
for (int i = 0; i < wordLen; ++i)
{
if (word[i] != input[curRow][curCol - i])
return false;
}
return true;
}
static bool SouthFind(char input[MAX_CHAR_IN_LINE][MAX_CHAR_IN_LINE], int rows, int cols, char* word, int wordLen, int curRow, int curCol)
{
if ((curRow + wordLen) > rows)
return false;
for (int i = 0; i < wordLen; ++i)
{
if (word[i] != input[curRow + i][curCol])
return false;
}
return true;
}
static bool NorthFind(char input[MAX_CHAR_IN_LINE][MAX_CHAR_IN_LINE], int rows, int cols, char* word, int wordLen, int curRow, int curCol)
{
if (curRow < (wordLen - 1))
return false;
for (int i = 0; i < wordLen; ++i)
{
if (word[i] != input[curRow - i][curCol])
return false;
}
return true;
}
static bool SouthEastFind(char input[MAX_CHAR_IN_LINE][MAX_CHAR_IN_LINE], int rows, int cols, char* word, int wordLen, int curRow, int curCol)
{
if ((curCol + wordLen) > cols)
return false;
if ((curRow + wordLen) > rows)
return false;
for (int i = 0; i < wordLen; ++i)
{
if (word[i] != input[curRow + i][curCol + i])
return false;
}
return true;
}
static bool NorthEastFind(char input[MAX_CHAR_IN_LINE][MAX_CHAR_IN_LINE], int rows, int cols, char* word, int wordLen, int curRow, int curCol)
{
if ((curCol + wordLen) > cols)
return false;
if (curRow < (wordLen - 1))
return false;
for (int i = 0; i < wordLen; ++i)
{
if (word[i] != input[curRow - i][curCol + i])
return false;
}
return true;
}
static bool SouthWestFind(char input[MAX_CHAR_IN_LINE][MAX_CHAR_IN_LINE], int rows, int cols, char* word, int wordLen, int curRow, int curCol)
{
if (curCol < (wordLen - 1))
return false;
if ((curRow + wordLen) > rows)
return false;
for (int i = 0; i < wordLen; ++i)
{
if (word[i] != input[curRow + i][curCol - i])
return false;
}
return true;
}
static bool NorthWestFind(char input[MAX_CHAR_IN_LINE][MAX_CHAR_IN_LINE], int rows, int cols, char* word, int wordLen, int curRow, int curCol)
{
if (curCol < (wordLen - 1))
return false;
if (curRow < (wordLen - 1))
return false;
for (int i = 0; i < wordLen; ++i)
{
if (word[i] != input[curRow - i][curCol - i])
return false;
}
return true;
}
typedef bool (*PFIND_FUNC)(char input[MAX_CHAR_IN_LINE][MAX_CHAR_IN_LINE], int rows, int cols, char* word, int wordLen, int curRow, int curCol);
static PFIND_FUNC s_FindFuncArray[] =
{
EastFind, WestFind, SouthFind, NorthFind, SouthEastFind, NorthEastFind, SouthWestFind, NorthWestFind
};
static bool FindWord(char input[MAX_CHAR_IN_LINE][MAX_CHAR_IN_LINE], int rows, int cols, char* word, int wordLen, int curRow, int curCol)
{
for (int i = 0; i < (sizeof(s_FindFuncArray) / sizeof(PFIND_FUNC)); ++i)
{
if (s_FindFuncArray[i](input, rows, cols, word, wordLen, curRow, curCol))
return true;
}
return false;
}
static void FindWord(char input[MAX_CHAR_IN_LINE][MAX_CHAR_IN_LINE], int rows, int cols, char* word)
{
int len = strlen(word);
for (int i = 0; i < len; ++i)
{
if (word[i] >= 'a')
word[i] -= ('a' - 'A');
}
for (int i = 0; i < rows; ++i)
{
for (int j = 0; j < cols; ++j)
{
if (FindWord(input, rows, cols, word, len, i, j))
{
cout << i + 1 << ' ' << j + 1 << endl;
return;
}
}
}
}
static void DoTest(char input[MAX_CHAR_IN_LINE][MAX_CHAR_IN_LINE])
{
int rows, cols;
cin >> rows >> cols;
for (int i = 0; i < rows; ++i)
{
for (int j = 0; j < cols; ++j)
{
cin >> input[i][j];
if (input[i][j] >= 'a')
input[i][j] -= ('a' - 'A');
}
}
static char s_Word[MAX_CHAR_IN_LINE + 1];
int wordsCnt;
cin >> wordsCnt;
for (int i = 0; i < wordsCnt; ++i)
{
cin >> s_Word;
FindWord(input, rows, cols, s_Word);
}
}
static void TestAll()
{
static char s_Input[MAX_CHAR_IN_LINE][MAX_CHAR_IN_LINE];
int cnt;
cin >> cnt;
for (int i = 0; i < cnt; ++i)
{
DoTest(s_Input);
if (i < (cnt - 1))
cout << endl;
}
}
int main(int argc, char* argv[])
{
TestAll();
return 0;
}