//  ? ? ? ? ? ? ?
//  ? @roadfromroi ?
//  ? ? ? ? ? ? ?
#include <iostream>
#include "vector"
#include "algorithm"
#include "numeric"
#include "climits"
#include "iomanip"
#include "bitset"
#include "cmath"
#include "map"
#include "deque"
#include "array"
#include "set"
#include "random"
#define all(x) x.begin(), x.end()
using namespace std;
#define int long long
mt19937 rnd;
vector<vector<int>> chk(vector<vector<pair<int, int>>> pole) {
    int n = pole.size(), m = pole[0].size();
    vector<int> vs(n);
    for (int i = 0; i < n; ++i) {
        vs[i] = rnd() % (pole[i][0].second - pole[i][0].first + 1) + pole[i][0].first;
    }
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            pole[i][j].first -= vs[i];
            pole[i][j].second -= vs[i];
        }
    }
    vector<pair<int, int>> segs(m, {-1e18, 1e18});
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            segs[j].first = max(segs[j].first, pole[i][j].first);
            segs[j].second = min(segs[j].second, pole[i][j].second);
        }
    }
    for (int i = 0; i < m; ++i) {
        if (segs[i].first > segs[i].second)
            return {};
    }
    vector<vector<int>> res(n, vector<int>(m));
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            res[i][j] = segs[j].first + vs[i];
        }
    }

    return res;
}
template<class T>
vector<vector<T>> rota(vector<vector<T>> ms) {
    if (ms.empty())
        return {};
    vector<vector<T>> sm(ms[0].size(), vector<T>(ms.size()));
    for (int i = 0; i < ms.size(); ++i) {
        for (int j = 0; j < ms[0].size(); ++j) {
            sm[j][i] = ms[i][j];
        }
    }
    return sm;
}
vector<vector<int>> rndchk(vector<vector<pair<int, int>>> pole) {
    if (rnd()%2) {
        return rota(chk(rota(pole)));
    }
    return chk(pole);
}
void solve() {
    int n, m;
    double tm = clock() * 1. / CLOCKS_PER_SEC;
    cin >> n >> m;
    vector<vector<pair<int, int>>> a(n, vector<pair<int, int>>(m));
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            cin >> a[i][j].first;
        }
    }
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            cin >> a[i][j].second;
        }
    }
    vector<vector<int>> res;
    while (res.empty() and tm + 0.32 > clock() * 1. / CLOCKS_PER_SEC)
        res = rndchk(a);
    if (!res.empty()) {
        cout << "DA\n";
        for (auto i : res) {
            for (auto j : i)
                cout << j << ' ';
            cout << '\n';
        }
    } else {
        cout << "NE\n";
    }
}
signed main() {
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    int t;
    cin >> t;
    while (t--) {
        solve();
    }
}
/*
1
3 2
-2 0
0 2
0 2
3 0
0 2
0 2




2
2 2
1 -2
0 -3
4 0
1 -2
2 2
0 0
0 1
0 0
0 1

 */