Square Math (Small)

On a small grid of digits and plus/minus signs, find the shortest, then lexicographically smallest, walking expression that evaluates left to right to each query value.

Medium5BFSBrute forceImplementationNo attempts yetTime limit5sMemory limit512 MB

Problem

Consider a square board with WW cells on each side, so W2W^2 cells in total. Each cell holds one of the following:

  • a single digit from 0 to 9,
  • the addition sign +,
  • the subtraction sign -.

Add one more condition. No two cells that touch horizontally or vertically may both hold digits, and no two cells that touch horizontally or vertically may both hold operators (+ or -). A board that satisfies this condition is called an arithmetic square.

Square Math is a puzzle played on an arithmetic square. You start on a digit cell, move one cell at a time horizontally or vertically, and stop on a digit cell. Reading the characters along the walk in order gives an expression, and evaluating that expression from left to right fixes a single value. Here is an example.

2+3
+4-
1+0

This board is an arithmetic square with W=3W = 3. Start on the 2 in the top left corner, move one cell right, then one cell down: the walk spells 2+4, whose value is 6. Move one more cell right, then one cell up: the walk spells 2+4-3, whose value is 3.

There is no limit on how many times a cell is used. Stepping to a neighbor and immediately stepping back to the cell you came from is allowed. Given an arithmetic square and a list of queries, find a Square Math expression whose value equals each query.

Input

The first line contains one integer TT, the number of test cases. TT test cases follow. The first line of each test case contains two integers WW and QQ. The next WW lines contain WW characters each and form the arithmetic square. Every board in the input satisfies the condition above. The next line contains QQ integers separated by spaces, the values that must be produced with Square Math. These values are called queries. Every query given has at least one Square Math expression that produces it.

Limits

  • 1T601 \le T \le 60
  • 2W102 \le W \le 10
  • 1Q201 \le Q \le 20
  • each query is between 11 and 5050

Output

For each test case, first print Case #X: on a line by itself, where XX is the test case number starting from 1. Then, for each query of that test case in input order, print on its own line a Square Math expression whose value equals the query.

If several Square Math expressions are possible, print the shortest one. If several shortest ones exist, print the lexicographically smallest one. Expressions of equal length place digits and operators in the same positions, so the only thing to remember when comparing them is that + comes before -.