#include <bits/stdc++.h>

using namespace std;

int main()
{
    int T,N,M;
    cin >> T;
    while (T--){
        cin >> N >> M;
        vector<vector<int>> l(N, vector<int>(M));
        vector<vector<int>> u(N, vector<int>(M));
        vector<vector<int>> m(N, vector<int>(M, 0));

        for (int i = 0; i < N; i++){
            for (int j = 0; j < M; j++)
                cin >> l[i][j];
        }
        for (int i = 0; i < N; i++){
            for (int j = 0; j < M; j++)
                cin >> u[i][j];
        }


        for (int i = 0; i < N ; i++){
            for (int j = 0; j < M ; j++){
                while (m[i][j] < l[i][j]){
                    for (int k = 0; k < M; k++)
                        m[i][k]++;
                }
            }
        }

        for (int i = 0; i < N ; i++){
            for (int j = 0; j < M ; j++){
                while (m[i][j] > u[i][j]){
                    for (int k = 0; k < N; k++)
                        m[k][j]--;
                }
            }
        }

        bool possible = true;
        for (int i = 0; i < N && possible; i++){
            for (int j = 0; j < M && possible; j++){

                if (m[i][j] < l[i][j] || m[i][j] > u[i][j])
                    possible = false;
            }
        }

        if (possible){
            cout << "DA" << endl;
            for (int i = 0; i < N; i++){
                for (int j = 0; j < M; j++)
                    cout << m[i][j] << " ";
                cout << endl;
            }
        }
        else cout << "NE" << endl;
    }

    return 0;
}