#include <bits/stdc++.h>
using namespace std;
void solve()
{
int n , m , a , b ;
cin>>n>>m>>a>>b ;
vector<string>arr(n) ;
vector<string>arr2(a);
for(int i = 0 ; i < n ; ++i)
cin>>arr[i] ;
for(int i = 0 ; i < a ; ++i)
cin>>arr2[i];
vector< pair<int , int> >vp ;
for(int col = 0 ; col < b ; ++col)
{
for(int row = 0 ; row < a ; ++row)
{
if(arr2[row][col] == 'x')
{
for(int r = 0 ; r < a ; ++r)
{
for(int c = 0 ; c < b ; ++c)
{
if(arr2[r][c] == 'x')
vp.push_back({r - row , c - col});
}
}
col = b ;
break;
}
}
}
for(int col = 0 ; col < m ; ++col)
{
for(int row = 0 ; row < n ; ++row)
{
if(arr[row][col] == 'x')
{
for(int i = 0 ; i < vp.size() ; ++i)
{
int x = row + vp[i].first , y = col + vp[i].second ;
if(x < 0 || x >= n || y < 0 || y >= m)
{
cout<<"NIE\n";
return ;
}
if(arr[x][y] == '.')
{
cout<<"NIE\n";
return ;
}
arr[x][y] = '.' ;
}
}
}
}
cout<<"TAK\n";
return ;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int t ;
cin>>t ;
while(t--)
{
solve();
}
return 0 ;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+Cgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKdm9pZCBzb2x2ZSgpCnsKICAgIGludCBuICwgbSAsIGEgLCBiIDsKICAgIGNpbj4+bj4+bT4+YT4+YiA7CiAgICB2ZWN0b3I8c3RyaW5nPmFycihuKSA7CiAgICB2ZWN0b3I8c3RyaW5nPmFycjIoYSk7CiAgICBmb3IoaW50IGkgPSAwIDsgaSA8IG4gOyArK2kpCiAgICAgICAgY2luPj5hcnJbaV0gOwogICAgZm9yKGludCBpID0gMCA7IGkgPCBhIDsgKytpKQogICAgICAgIGNpbj4+YXJyMltpXTsKICAgIHZlY3RvcjwgcGFpcjxpbnQgLCBpbnQ+ID52cCA7CiAgICBmb3IoaW50IGNvbCA9IDAgOyBjb2wgPCBiIDsgKytjb2wpCiAgICB7CiAgICAgICAgZm9yKGludCByb3cgPSAwIDsgcm93IDwgYSA7ICsrcm93KQogICAgICAgIHsKICAgICAgICAgICAgaWYoYXJyMltyb3ddW2NvbF0gPT0gJ3gnKQogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICBmb3IoaW50IHIgPSAwIDsgciA8IGEgOyArK3IpCiAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgZm9yKGludCBjID0gMCA7IGMgPCBiIDsgKytjKQogICAgICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICAgICAgaWYoYXJyMltyXVtjXSA9PSAneCcpCiAgICAgICAgICAgICAgICAgICAgICAgICAgICB2cC5wdXNoX2JhY2soe3IgLSByb3cgLCBjIC0gY29sfSk7CiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgY29sID0gYiA7CiAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgfQogICAgICAgIH0KICAgIH0KICAgIGZvcihpbnQgY29sID0gMCA7IGNvbCA8IG0gOyArK2NvbCkKICAgIHsKICAgICAgICBmb3IoaW50IHJvdyA9IDAgOyByb3cgPCBuIDsgKytyb3cpCiAgICAgICAgewogICAgICAgICAgICBpZihhcnJbcm93XVtjb2xdID09ICd4JykKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgZm9yKGludCBpID0gMCA7IGkgPCB2cC5zaXplKCkgOyArK2kpCiAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgaW50IHggPSByb3cgKyB2cFtpXS5maXJzdCAsIHkgPSBjb2wgKyB2cFtpXS5zZWNvbmQgOwogICAgICAgICAgICAgICAgICAgIGlmKHggPCAwIHx8IHggPj0gbiB8fCB5IDwgMCB8fCB5ID49IG0pCiAgICAgICAgICAgICAgICAgICAgewogICAgICAgICAgICAgICAgICAgICAgICBjb3V0PDwiTklFXG4iOwogICAgICAgICAgICAgICAgICAgICAgICByZXR1cm4gOwogICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICBpZihhcnJbeF1beV0gPT0gJy4nKQogICAgICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICAgICAgY291dDw8Ik5JRVxuIjsKICAgICAgICAgICAgICAgICAgICAgICAgcmV0dXJuIDsKICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgYXJyW3hdW3ldID0gJy4nIDsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgfQogICAgICAgIH0KICAgIH0KICAgIGNvdXQ8PCJUQUtcbiI7CiAgICByZXR1cm4gOwp9CgppbnQgbWFpbigpCnsKICAgIGlvczo6c3luY193aXRoX3N0ZGlvKDApOwogICAgY2luLnRpZSgwKTsKICAgIGludCB0IDsKICAgIGNpbj4+dCA7CiAgICB3aGlsZSh0LS0pCiAgICB7CiAgICAgICAgc29sdmUoKTsKICAgIH0KICAgIHJldHVybiAwIDsKfQ==