#include <iostream>
#include <vector>

using namespace std;

int main()
{
	int N, M, t;
	cin >> t;
	vector<pair<int, int>> a;
	int l[10][300][300], m[10][300][300], u[10][300][300];
	for (int k = 0; k < t; k++) {
		cin >> N >> M;
		for (int i = 0; i < N; i++) {
			for (int j = 0; j < M; j++) {
				cin >> l[k][i][j];
			}
		}
		for (int i = 0; i < N; i++) {
			for (int j = 0; j < M; j++) {
				cin >> u[k][i][j];
			}
		}
		for (int i = 0; i < N; i++) {
			for (int j = 0; j < M; j++) {
				m[k][i][j] = 0;
			}
		}
		a.push_back(make_pair(N, M));
	}
	for (int k = 0; k < t; k++) {
		for (int i = 0; i < a[k].first; i++) {
			for (int j = 0; j < a[k].second; j++) {
				while (m[k][i][j] < l[k][i][j]) {
					for (int e = 0; e < a[k].second; e++) {
						m[k][i][e]++;
					}
				}
				while (m[k][i][j] > u[k][i][j]) {
					for (int e = 0; e < a[k].first; e++) {
						m[k][e][j]--;
					}
				}
			}
		}
		bool ind = true;
		for (int i = 0; i < a[k].first; i++) {
			for (int j = 0; j < a[k].second; j++) {
				if (m[k][i][j]<l[k][i][j] && m[k][i][j] > u[k][i][j])
					ind = false;
			}
		}
		if (ind) {
			cout << "DA" << endl;
			for (int i = 0; i < a[k].first; i++) {
				for (int j = 0; j < a[k].second; j++) {
					cout << m[k][i][j] << " ";
				}
				cout << endl;
			}
		}
		else
			cout << "NE" << endl;
	}
	return 0;
}