#include <bits/stdc++.h>

using namespace std;

void solve()
{
    int n , m , a , b ;
    cin>>n>>m>>a>>b ;
    char arr[n][m] , arr2[a][b] ;
    for(int i = 0 ; i < n ; ++i)
        for(int j = 0 ; j < m ; ++j)
            cin>>arr[i][j] ;
    for(int i = 0 ; i < a ; ++i)
        for(int j = 0 ; j < b ; ++j)
            cin>>arr2[i][j] ;
    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 ;
}