using System;

namespace ConsoleApp11
{
    class Program
    {
        static bool racun(int[,] l, int[,] u, int[,] matrica, bool[,] promenjeni)
        {
            bool promenjen = false;
            for (int i = 0; i < matrica.GetLength(0); i++)
            {
                for (int j = 0; j < matrica.GetLength(1); j++)
                {
                    if (promenjeni[i, j] && (matrica[i, j] < l[i, j] || matrica[i, j] > u[i, j])) return false;
                    if (matrica[i, j] < l[i, j] || matrica[i, j] > u[i, j]) { promenjen = true; promenjeni[i, j] = true; }
                    while (matrica[i, j] < l[i, j]) { plus(matrica, i); }
                    while (matrica[i, j] > u[i, j]) { minus(matrica, j); }
                }
            }
            if (!promenjen) return true;
            else { return racun(l, u, matrica, promenjeni); }
        }
        static void plus(int[,] matrica, int i)
        {
            for (int j = 0; j < matrica.GetLength(1); j++) matrica[i, j]++;
        }
        static void minus(int[,] matrica, int j)
        {
            for (int i = 0; i < matrica.GetLength(0); i++) matrica[i, j]--;
        }
        static void ispis(int[,] matrica, ref string resenje)
        {
            for (int i = 0; i < matrica.GetLength(0); i++)
            {
                for (int j = 0; j < matrica.GetLength(1); j++)
                {
                    resenje += matrica[i, j] + " ";
                }
                resenje += '\n';
            }
        }
        static void Main(string[] args)
        {
            string resenje = "";
            int t = Convert.ToInt32(Console.ReadLine());
            for (int i = 0; i < t; i++)
            {
                string[] unos1 = Console.ReadLine().Split();
                int n = Convert.ToInt32(unos1[0]);
                int m = Convert.ToInt32(unos1[1]);
                int[,] l = new int[n, m];
                int[,] u = new int[n, m];
                int[,] matrica = new int[n, m];
                for (int j = 0; j < n; j++)
                {
                    string[] unos2 = Console.ReadLine().Split();
                    for (int k = 0; k < m; k++) l[j, k] = Convert.ToInt32(unos2[k]);
                }
                for (int j = 0; j < n; j++)
                {
                    string[] unos2 = Console.ReadLine().Split();
                    for (int k = 0; k < m; k++) u[j, k] = Convert.ToInt32(unos2[k]);
                }
                if (racun(l, u, matrica, new bool[matrica.GetLength(0), matrica.GetLength(1)]))
                {
                    resenje += "DA\n";
                    ispis(matrica, ref resenje);
                }
                else resenje += "NE\n";
            }
            Console.Write(resenje);
        }
    }
}