#include <bits/stdc++.h>

using namespace std;

int main()
{
    int t = 0;
    cin >> t;
    int n, m = 0;
    int l[300][300];
    int mo[300][300];
    int u[300][300];
    int najveci_l_red[300];
    int najmanja_u_kolona[300];
    int najveca_mo_kolona[300];
    int maks = -1;
    bool moguce = false;

    for (int i = 0; i < 300; i++)
    {
        najveci_l_red[i] = INT_MIN;
        najmanja_u_kolona[i] = INT_MAX;
        najveca_mo_kolona[i] = INT_MIN;
    }

    for (int i = 0; i < t; i++)
    {
        cin >> n >> m;
        for (int j = 0; j < n; j++)
        {
            for (int k = 0; k < m; k++)
            {
                cin >> l[j][k];
                najveci_l_red[j] = max(najveci_l_red[j], l[j][k]);
            }
        }
        for (int j = 0; j < n; j++)
        {
            for (int k = 0; k < m; k++)
            {
                cin >> u[j][k];
                najmanja_u_kolona[k] = min(najmanja_u_kolona[k], u[j][k]);
            }
        }

        for (int j = 0; j < n; j++)
        {
            for (int k = 0; k < m; k++)
            {
                mo[j][k] = najveci_l_red[j];
                najveca_mo_kolona[k] = max(najveca_mo_kolona[k], mo[j][k]);
            }
        }

        for (int j = 0; j < m; j++)
        {
            if (najveca_mo_kolona[j] > najmanja_u_kolona[j])
            {
                for (int k = 0; k < n; k++)
                {
                    mo[k][j] -= abs(najmanja_u_kolona[j] - najveca_mo_kolona[j]);
                }
            }
        }

        for (int j = 0; j < n; j++)
        {
            for (int k = 0; k < m; k++)
            {
                if (mo[j][k] >= l[j][k] && mo[j][k] <= u[j][k])
                {
                    moguce = true;
                }
                else
                {
                    moguce = false;
                    break;
                }
            }
        }
        if (moguce)
        {
            cout << "DA" << endl;
            for (int j = 0; j < n; j++)
            {
                for (int k = 0; k < m; k++)
                {
                    cout << mo[j][k] << " ";
                }
                cout << endl;
            }
        }
        else
        {
            cout << "NE" << endl;
        }
    }



    return 0;
}