#define Local
#include <iostream>
#include <iomanip>
#include <string>
#include <cstring>
#include <cstdio>
#include <queue>
#include <stack>
#include <algorithm>
#include <cmath>
//方向顺序无所谓,因为不论在哪个方向上找到都输出这个位置
//判断函数中应该小于等于m或者n,而不是m-1、n-1,因为加上长度时候当前位置还占一个位置,验证即可。
using namespace std;
#define MAX 50+5
char grid[MAX][MAX], tofind[20][MAX];//储存输入
char temp[MAX];//截取单词
int m ,n, p, pos_x, pos_y;
void strlwr_(char *s)
{
int i = 0;
for (i = 0; i < strlen(s); i++)
if (s[i] >= 'A' && s[i] <= 'Z')
s[i] = s[i] - 'A' + 'a';
}
void Input()
{
int i = 0;
cin >> m >> n;
getchar();
for (i = 0; i < m; i++)
{
gets(grid[i]);
strlwr_(grid[i]);
}
cin >> p;
getchar();
for (i = 0; i < p; i++)
{
gets(tofind[i]);
strlwr_(tofind[i]);
}
}
int Dirction_1(int x, int y, char *s)//正上
{
int i = 0, j = 0;
if (x - strlen(s) >= 0)
for (i = 0; i < strlen(s); i++)
temp[j++] = grid[x-i][y];
temp[j] = '\0';
if (!strcmp(temp, s))
return 1;
else
return 0;
}
int Dirction_2(int x, int y, char *s)//右上
{
int i = 0, j = 0;
if (x - strlen(s) >= 0 && y + strlen(s) <= n)
for (i = 0; i < strlen(s); i++)
temp[j++] = grid[x-i][y+i];
temp[j] = '\0';
if (!strcmp(temp, s))
return 1;
else
return 0;
}
int Dirction_3(int x, int y, char *s)//正右
{
int i = 0, j = 0;
if (y + strlen(s) <= n)
for (i = 0; i < strlen(s); i++)
temp[j++] = grid[x][y+i];
temp[j] = '\0';
if (!strcmp(temp, s))
return 1;
else
return 0;
}
int Dirction_4(int x, int y, char *s)//右下
{
int i = 0, j = 0;
if (x + strlen(s) <= m && y + strlen(s) <= n)
for (i = 0; i < strlen(s); i++)
temp[j++] = grid[x+i][y+i];
temp[j] = '\0';
if (!strcmp(temp, s))
return 1;
else
return 0;
}
int Dirction_5(int x, int y, char *s)//正下
{
int i = 0, j = 0;
if (x + strlen(s) <= m)
for (i = 0; i < strlen(s); i++)
temp[j++] = grid[x+i][y];
temp[j] = '\0';
if (!strcmp(temp, s))
return 1;
else
return 0;
}
int Dirction_6(int x, int y, char *s)//左上
{
int i = 0, j = 0;
if (x + strlen(s) <= m && y - strlen(s) >= 0)
for (i = 0; i < strlen(s); i++)
temp[j++] = grid[x+i][y-i];
temp[j] = '\0';
if (!strcmp(temp, s))
return 1;
else
return 0;
}
int Dirction_7(int x, int y, char *s)//正左
{
int i = 0, j = 0;
if (y + strlen(s) >= 0)
for (i = 0; i < strlen(s); i++)
temp[j++] = grid[x][y-i];
temp[j] = '\0';
if (!strcmp(temp, s))
return 1;
else
return 0;
}
int Dirction_8(int x, int y, char *s)//左上
{
int i = 0, j = 0;
if (x - strlen(s) >= 0 && y - strlen(s) >= 0)
for (i = 0; i < strlen(s); i++)
temp[j++] = grid[x-i][y-i];
temp[j] = '\0';
if (!strcmp(temp, s))
return 1;
else
return 0;
}
int Search(int x, int y, char *s)
{
if (Dirction_1(x, y, s)) {pos_x = x, pos_y = y; return 1;}
else if (Dirction_2(x, y, s)) {pos_x = x, pos_y = y; return 1;}
else if (Dirction_3(x, y, s)) {pos_x = x, pos_y = y; return 1;}
else if (Dirction_4(x, y, s)) {pos_x = x, pos_y = y; return 1;}
else if (Dirction_5(x, y, s)) {pos_x = x, pos_y = y; return 1;}
else if (Dirction_6(x, y, s)) {pos_x = x, pos_y = y; return 1;}
else if (Dirction_7(x, y, s)) {pos_x = x, pos_y = y; return 1;}
else if (Dirction_8(x, y, s)) {pos_x = x, pos_y = y; return 1;}
return 0;
}
int main()
{
#ifdef Local
freopen("a.in", "r", stdin);
freopen("a.out", "w", stdout);
#endif
int t = 0, i = 0, j = 0, k = 0;
cin >> t;
getchar();
while (t--)
{
Input();
for (k = 0; k < p; k++)
{
int flag = 0;
for (i = 0; i < m; i++)
{
if (1 == flag)
break;
for (j = 0; j < n; j++)
{
if (Search(i, j, tofind[k]))
{
cout << pos_x+1 << ' '<< pos_y+1 << endl;
flag = 1;
break;
}
}
}
}
if (t)
cout << endl;
}
}