import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;

import java.util.Stack;
import java.util.Queue;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.StringTokenizer;

/* Problem: Spreadsheet (https://uva.onlinejudge.org/external/1/196.html) 
 * The following class implements 3 different approaches to solve the problem.
 */
public class spreadsheet
{
    public static class Cell
    {
        public int value = 0;
        public boolean hasValue = false;
        public int nextDepependency = 0;
        public ArrayList<Cell> cells = new ArrayList<Cell>();
        public int nDependencies = 0;

        public boolean hasMoreDependencies()
        {
            return (this.nextDepependency < this.cells.size());
        }

        public Cell nextDepependency()
        {
            return this.cells.get(this.nextDepependency++);
        }

        public void parse(String content, Cell[][] spreadsheet, boolean flipDependencies)
        {
            /* If the cell contains a formula... */
            if (content.charAt(0) == '=')
            {
                /* ... find every cell in it and append it to this cell's
                 * `list of references`...
                 */
                StringTokenizer tokenizer = new StringTokenizer(content.substring(1), "+");

                if (flipDependencies)
                {
                    while (tokenizer.hasMoreTokens())
                    {
                        Cell referredCell = Cell.findCell(tokenizer.nextToken(), spreadsheet);
                        referredCell.cells.add(this);
                        this.nDependencies += 1;
                    }
                }
                else
                {
                    while (tokenizer.hasMoreTokens())
                        this.cells.add(Cell.findCell(tokenizer.nextToken(), spreadsheet));
                }

                /* ALTERNATIVE PARSING *****************************************
                for (String cellName : content.substring(1).split("\\+"))
                    this.cells.add(Cell.findCell(cellName, spreadsheet));
                ***************************************************************/

                this.hasValue = false;
            }
            else
            {
                /* ... otherwise store the value of the cell. */
                this.value = Integer.valueOf(content);
                this.hasValue = true;
                this.nDependencies = 0;
            }
        }

        public static Cell findCell(String name, Cell[][] spreadsheet)
        {
            int iRow = 0;
            int iCol = -1;

            for (int i = 0; i < name.length(); i++)
            {
                if ('A' <= name.charAt(i) && name.charAt(i) <= 'Z')
                {
                    iCol += 1;
                    iCol *= 26;
                    iCol += (name.charAt(i) - 'A');
                }
                else
                {
                    iRow *= 10;
                    iRow += (name.charAt(i) - '0');
                }
            }

            return spreadsheet[iRow - 1][iCol];
        }

        public int eval()
        {
            if (!this.hasValue)
            {
                for (Cell cell : this.cells)
                    this.value += cell.eval();
                this.hasValue = true;
            }

            return this.value;
        }
    }

    public static void evalCell(Cell cell)
    {
        Stack<Cell> stack = new Stack<Cell>();

        if (!cell.hasValue)
        {
            stack.push(cell);

            while (!stack.empty())
            {
                cell = stack.peek();

                if (cell.hasMoreDependencies())
                {
                    cell = cell.nextDepependency();

                    if (!cell.hasValue)
                        stack.push(cell);
                }
                else
                {
                    cell = stack.pop();
                    cell.eval();
                }
            }
        }
    }

    public static void evalSpreadsheet(Cell[][] spreadsheet)
    {
        Queue<Cell> queue = new LinkedList<Cell>();

        for (int iRow = 0; iRow < spreadsheet.length; iRow++)
            for (int iCol = 0; iCol < spreadsheet[0].length; iCol++)
                if (spreadsheet[iRow][iCol].nDependencies == 0)
                    queue.add(spreadsheet[iRow][iCol]);
        
        Cell cell = null;

        while ((cell = queue.poll()) != null)
        {
            for (Cell dependentCell : cell.cells)
            {
                dependentCell.value += cell.value;

                if (--dependentCell.nDependencies == 0)
                {
                    dependentCell.hasValue = true;
                    queue.add(dependentCell);
                }
            }
        }
    }

    public static void main(String[] arguments) throws IOException
    {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

        /* Read the number of spreadsheets from the input. */
        int N = Integer.valueOf(reader.readLine());

        /* The approach used, see comments in the code:
         *     0 - recursive
         *     1 - non-recursive #1
         *     2 - non-recursive #2
         */
        int usedSolution = 0;

        /* Process every spreadsheet, one by one. */
        while (N-- > 0)
        {
            String dimensions[] = reader.readLine().split("\\s+");

            int nColumns = Integer.valueOf(dimensions[0]);
            int nRows = Integer.valueOf(dimensions[1]);

            /* Allocate cells for spreadsheet. */
            Cell[][] spreadsheet = new Cell[nRows][nColumns];

            for (int iRow = 0; iRow < nRows; iRow++)
                for (int iCol = 0; iCol < nColumns; iCol++)
                    spreadsheet[iRow][iCol] = new Cell();

            /* Read the spreadsheet from the input. */
            for (int iRow = 0; iRow < nRows; iRow++)
            {
                int iCol = 0;

                StringTokenizer tokenizer = new StringTokenizer(reader.readLine());

                while (tokenizer.hasMoreTokens())
                    spreadsheet[iRow][iCol++].parse(tokenizer.nextToken(), spreadsheet, usedSolution == 2);

                /* ALTERNATIVE PARSING *****************************************
                for (String cell : reader.readLine().split("\\s+"))
                    spreadsheet[iRow][iCol++].parse(cell, spreadsheet);
                ***************************************************************/
            }

            /* RECURSIVE SOLUTION
             * ------------------
             * Evaluate the content of every cell of the spreadsheet,
             * and print it out with substituted values.
             *
             * DO NOT COMMENT THE CODE BELOW!!!
             */
            if (usedSolution == 0)
                for (int iRow = 0; iRow < nRows; iRow++)
                    for (int iCol = 0; iCol < nColumns; iCol++)
                        spreadsheet[iRow][iCol].eval();

            /* NON-RECURSIVE SOLUTION #1
             * -------------------------
             * Evaluate the content of every cell of the spreadsheet,
             * and print it out with substituted values. 
             */
            if (usedSolution == 1)
                for (int iRow = 0; iRow < nRows; iRow++)
                    for (int iCol = 0; iCol < nColumns; iCol++)
                        evalCell(spreadsheet[iRow][iCol]);

            /* NON-RECURSIVE SOLUTION #2
             * -------------------------
             * Evaluate the content of every cell of the spreadsheet,
             * and print it out with substituted values.
             */
            if (usedSolution == 2)
                evalSpreadsheet(spreadsheet);

            /* Print out the spreadsheet with evaluated cells. */
            for (int iRow = 0; iRow < nRows; iRow++)
            {
                System.out.print(spreadsheet[iRow][0].value);
                for (int iCol = 1; iCol < nColumns; iCol++)
                    System.out.print(" " + spreadsheet[iRow][iCol].value);
                System.out.println();
            }
        }
    }
}