#include<iostream>
#include <bits/stdc++.h>

using namespace std;

int main() {
	int t;
	cin >> t;

	for (int i = 0; i < t; i++) {
		int n, m;
		cin >> n;
		cin >> m;

		int **l = new int*[m];
		int **u = new int*[m];
		int **res = new int*[m];

		int *hMax = new int[m];
		for (int x = 0; x < m;x++) {
			hMax[x] = INT_MIN;
			l[x] = new int[n];
			for (int y = 0;y < n;y++) {
				cin >> l[x][y];
				if (l[x][y] > hMax[x]) {
					hMax[x] = l[x][y];
				}
			}
		}

		for (int x = 0; x < m;x++) {
			u[x] = new int[n];
			for (int y = 0;y < n;y++) {
				cin >> u[x][y];
			}
		}

		int *minDiff = new int[n];
		for (int y = 0; y < n; y++) {
			minDiff[y] = INT_MAX;
			for (int x = 0; x < m; x++) {
				if(hMax[x] - l[x][y] < minDiff[y]) {
					minDiff[y] = hMax[x] - l[x][y];
				}
			}
		}

		bool valid = true;
		for (int x = 0; x < m; x++) {
			res[x] = new int[n];
			for (int y = 0; y < n; y++) {
				res[x][y] = hMax[x] - minDiff[y];
				if (res[x][y] > u[x][y]) {
					valid = false;
					cout << "NE" << endl;
					break;
				}
			}
			if (!valid) {
				break;
			}
		}

		if (valid) {
			cout << "DA" << endl;
			for (int x = 0; x < m;x++) {
				for (int y = 0;y < n;y++) {
					cout << res[x][y] << " ";
				}
				cout << endl;
			}
		}
	}

	return 0;
}