using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Opklada
{
    internal class Program
    {
        static int[] Minimum(int[,] matrica)
        {
            int _min = matrica[0, 0];
            int[] _minimum = new int[matrica.GetLength(0)];
            for (int i = 0; i < matrica.GetLength(0); i++)
            {
                _min = matrica[i, 0];
                for (int j = 0; j < matrica.GetLength(1); j++)
                {
                    if (matrica[i, j] < _min) _min = matrica[i, j];
                }
                _minimum[i] = _min;
            }
            return _minimum;
        }
        static int[] Maksimum(int[,] matrica)
        {
            int _max = matrica[0, 0];
            int[] _maksimum = new int[matrica.GetLength(0)];
            for (int i = 0; i < matrica.GetLength(0); i++)
            {
                _max = matrica[i, 0];
                for (int j = 0; j < matrica.GetLength(1); j++)
                {
                    if (matrica[i, j] > _max) _max = matrica[i, j];
                }
                _maksimum[i] = _max;
            }
            return _maksimum;
        }
        static void Main(string[] args)
        {
            int T = Convert.ToInt32(Console.ReadLine());
            string[] ispis = new string[T];
            for (int i = 0; i < T; i++)
            {
                string[] str = Console.ReadLine().Split();
                int[,] l = new int[Convert.ToInt32(str[0]), Convert.ToInt32(str[1])];
                int[,] u = new int[Convert.ToInt32(str[0]), Convert.ToInt32(str[1])];
                for (int j = 0; j < l.GetLength(1); j++)
                {
                    string[] str1 = Console.ReadLine().Split();
                    for (int k = 0; k < l.GetLength(0); k++)
                    {
                        l[k, j] = Convert.ToInt32(str1[k]);
                    }
                }
                for (int j = 0; j < u.GetLength(1); j++)
                {
                    string[] str1 = Console.ReadLine().Split();
                    for (int k = 0; k < l.GetLength(0); k++)
                    {
                        u[k, j] = Convert.ToInt32(str1[k]);
                    }
                }

                int[] minimum = Minimum(u);
                int[] maksimum = Maksimum(l);
                bool isti = false;
                for (int j = 0; j < minimum.Length; j++)
                {
                    if (minimum[j] != maksimum[j]) { isti = false; break; }
                    else isti = true;
                }
                if (isti)
                {
                    string st = "";
                    for (int k = 0; k < l.GetLength(1); k++)
                    {
                        for (int j = 0; j < minimum.Length; j++)
                            st += Convert.ToString(minimum[j]) + " ";
                        st += "\n";
                    }
                    ispis[i] = "DA\n" + st;
                }
                else ispis[i] = "NE\n";
            }
            for (int i = 0; i < ispis.Length; i++)
                Console.Write(ispis[i]);
        }
    }
}