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

bool proveri(int n,int f,const vector<vector<long long>>& l,
             const vector<vector<long long>>& u,
            vector<vector<long long>>& m){
    for(int i = 0;i < n;i++){
        if(l[i][0] < 0) continue;
        long long maxI = 0;
        int indI = i;
        for(int j = 0;j < n;j++){
            long long z = l[i][0]-l[j][0];
            if(z > maxI){
                maxI = z;
                indI = j;
            }
        }
        for(int j = 0;j < f;j++) m[i][j] = maxI;
    }
    for(int j = 0;j < f;j++){
        long long z = m[0][j]-l[0][j];
        for(int i = 0;i < n;i++){
            m[i][j] -= z;
        }
    }
    for(int i = 0;i < n;i++){
        for(int j = 0;j < f;j++){
            if(m[i][j] < l[i][j] || m[i][j] > u[i][j]) return false;
        }
    }
    return true;
}

int main(){
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    int t,n,f;
    cin >> t;
    for(int i = 0;i < t;i++){
        cin >> n >> f;
        vector<vector<long long>> l(n, vector<long long>(f));
        vector<vector<long long>> u(n, vector<long long>(f));
        vector<vector<long long>> m(n, vector<long long>(f));
        for(int j = 0;j < n;j++){
            for(int k = 0;k < f;k++){
                cin >> l[j][k];
            }
        }
        for(int j = 0;j < n;j++){
            for(int k = 0;k < f;k++){
                cin >> u[j][k];
            }
        }
        if(proveri(n,f,l,u,m)){
            cout << "DA\n";
            for(int j = 0;j < n;j++){
                for(int k = 0;k < f;k++){
                    cout << m[j][k] << " ";
                }
                cout << "\n";
            }
        }
        else cout << "NE\n";
    }
}