#include <bits/stdc++.h>

using namespace std;

int main()
{
    int T;
    cin >> T;
    while (T--){
        int N, M;
        cin >> N >> M;
        int l[N][M], u[N][M], m[N][M];
        memset(m, 0, sizeof 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 >> 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 moguce = true;
        for (int i = 0; i < N && moguce; i++){
            for (int j = 0; j < M && moguce; j++){
                if (m[i][j] < l[i][j] || m[i][j] > u[i][j])
                    moguce = false;
            }
        }
        if (moguce){
            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;
}