#include <bits/stdc++.h>

#define ll long long
#define ld long double
#define pb push_back
#define fi first
#define se second
#define en '\n'
#define sp ' '
#define tb '\t'
#define ri(n) int n; cin >> n
#define rl(n) ll n; cin >> n
#define rs(s) string s; cin >> s
#define rc(c) char c; cin >> c
#define rv(v) for (auto &x : v) cin >> x
#define pven(v) for (auto x : v) cout << x << en
#define pv(v) for (auto x : v) cout << x << sp; cout << en
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define yes cout << "YES" << en
#define no cout << "NO" << en
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
#define ssort(a, b) if (a < b) swap(a, b)
#define bitcnt(a) (__builtin_popcountll(a))
#define bithigh(a) (63-__builtin_clzll(a))
#define lg bithigh
#define highpow(a) (1LL << (ll)lg(a))

using namespace std;

const ll LINF = 4e18;
const int mxN = 5e2+10, INF = 2e9;
ll n, m, l[mxN][mxN], r[mxN][mxN], mat[mxN][mxN], R[mxN], C[mxN], k[mxN];

void Generate(){

    for (int i = 0; i < m-1; i++)
        k[i] = R[i+1] - R[i];
    for (int i = 0; i < n; i++) mat[i][0] = C[i];
    for (int i = 0; i < n; i++)
        for (int j = 1; j < m; j++)
            mat[i][j] = mat[i][j-1] + k[j-1];
}

void Fix(int mask){

    R[0] = C[0] = (mask&1 ? r[0][0] : l[0][0]);
    for (int i = 1; i < m; i++)
        R[i] = ((mask>>i)&1 ? r[0][i] : l[0][i]);
    for (int i = 1; i < n; i++)
        C[i] = ((mask>>(m-1+i))&1 ? r[i][0] : l[i][0]);
    Generate();
}

bool Check(){

    for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++)
            if (mat[i][j] < l[i][j] || mat[i][j] > r[i][j])
                return 0;
    return 1;
}

bool Brute(){

    for (int mask = 0; mask < (1<<(n+m-1)); mask++){
        Fix(mask);
        if (Check()) return 1;
    }
    return 0;
}

bool Fixed(){

    R[0] = C[0] = l[0][0];
    for (int i = 1; i < m; i++)
        R[i] = l[0][i];
    for (int i = 1; i < n; i++)
        C[i] = l[i][0];
    Generate();
    return Check();
}

void Solve(){

    cin >> n >> m;
    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 >> r[i][j];
    bool ans;
    if (n <= 10 && m <= 10) ans = Brute();
    else ans = Fixed();
    if (ans){
        cout << "DA" << en;
        for (int i = 0; i < n; i++){
            for (int j = 0; j < m; j++)
                cout << mat[i][j] << sp;
            cout << en;
        }
    }
    else cout << "NE" << en;
}

int main(){

    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0); cerr.tie(0);
    cout << setprecision(12) << fixed;
    cerr << setprecision(12) << fixed;
    cerr << "Started!" << endl;

    int t = 1;
    cin >> t;
    while (t--)
        Solve();

    return 0;
}