#include <bits/stdc++.h>
using namespace std;

pair<string, vector<vector<int>>> solve(const vector<vector<int>>& l, const vector<vector<int>>& u, int n, int M) {

    vector<vector<int>> m(n, vector<int>(M));
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < M; j++) {
            m[i][j] = min(l[i][j], u[i][j]);
        }
    }


    for (int i = 0; i < n; i++) {
        for (int j = 0; j < M; j++) {

            if (m[i][j] < *min_element(m[i].begin(), m[i].end())) {

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

            else if (m[i][j] > *max_element(m[i].begin(), m[i].end())) {

                for (int k = 0; k < n; k++) {
                    m[k][j]--;
                }
            }
        }
    }

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < M; j++) {
            if (l[i][j] > m[i][j] || m[i][j] > u[i][j]) {
                return { "NO", {} };
            }
        }
    }

    return { "DA", m };
}

int main() {
    int T;
    cin >> T;  


    for (int t = 1; t <= T; t++) {
        int N, M;
        cin >> N >> M;


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


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


        pair<string, vector<vector<int>>> result = solve(l, u, N, M);
        string result_str = result.first;
        vector<vector<int>> m_out = result.second;
        cout << result_str << endl;
        for (auto row : m_out) {
            for (auto element : row) {
                cout << element << " ";
            }
            cout << endl;
        }
    }

    return 0;
}