We are given a problem: given an n x m matrix a, we need to produce a matrix b such that for each cell, either b[i][j] = a[i][j] or b[i][j] = a[i][j] + 1, and b has no two adjacent cells (sharing a side) with the same value. It is guaranteed that such a matrix exists. We can output any valid b.
We need to design an algorithm. The constraints: n, m ≤ 100, t ≤ 10, a up to 1e9. So we need to compute b quickly.
Observations: We can adjust each cell either keep original or increment by 1. That changes parity? Because increment by 1 changes the number. The condition is that for each adjacent pair, values must be different.
We can think of constructing b based on parity pattern. For any matrix, if we enforce that b[i][j] has a specific parity (like (i+j) mod 2) then adjacent cells will have opposite parity, so they will be different if we also ensure that no two adjacent cells have same value. However, parity alone does not guarantee difference because two numbers of same parity could still be equal if they are same value. But we can adjust each cell's value by at most +1, so we can potentially set b[i][j] to be either a[i][j] or a[i][j]+1, so we have a choice to possibly match a target parity.
Idea: For each cell, we want to assign b[i][j] to be either a[i][j] or a[i][j]+1 such that b[i][j] % 2 equals some predetermined pattern (like (i+j) mod 2). Then, since adjacent cells will have opposite parity, they cannot be equal. Because if two numbers have opposite parity, they cannot be equal. So this would guarantee the good condition.
But is it always possible? For each cell, we have two choices: either a or a+1. So we can choose b to have parity p = (i+j)%2 if either a % 2 = p, then we can choose b = a; else if (a+1) % 2 = p, i.e., a%2 != p, then we can choose b = a+1. Indeed, one of a or a+1 will have parity p because they differ by 1. So always we can set b[i][j] to have parity equal to (i+j) mod 2. So this yields a valid matrix.
Thus the solution is simple: For each cell (i,j), set desired parity = (i+j) % 2. If a[i][j] % 2 == desired parity, set b[i][j] = a[i][j]; else set b[i][j] = a[i][j] + 1.
But wait: need to ensure that b[i][j] = a[i][j] or a[i][j]+1 exactly. In the else case we set a+1, so that's fine. So we output that.
Check with sample:
First test:
3x2:
1 2
4 5
7 8
Compute parities: (1,1): i+j=2 even -> parity 0? Let's define parity as (i+j)%2. Usually 0 for even, 1 for odd. We can use either mapping. But check: (1,1):1+1=2 even. So desired parity even. a11=1 odd. So mismatch -> b11=2? But output in sample: first row: 1 2. So they kept 1 for (1,1)? Actually output: 1 2. So they kept a11=1 (odd) while desired even? Then adjacent: (1,1) and (1,2) values 1 and 2 are different, but they have different parity. So they didn't follow the strict parity pattern; they used some other assignment. So our method would output b11=2 (since a11=1, need even so a+1=2) but sample says 1. So both are valid but we need to output any valid matrix. So our method is valid as well. But does it always produce no adjacent equal? Because if we set parity pattern, adjacent cells have opposite parity, so they can't be equal. So yes.
But check: Is there any case where parity pattern fails because we might set b[i][j] = a[i][j]+1 which might exceed some constraints? No, a up to 1e9, +1 is still within range. No constraints on b values besides being either a or a+1. So it's fine.
Thus the solution is straightforward.
But wait: The problem statement says "If two adjacent cells contain the same value, the robot will break. A matrix is called good if no two adjacent cells contain the same value." So we just need to avoid equal adjacent. Parity pattern ensures difference.
Hence for each test case, compute for each cell: if (i+j) % 2 == a[i][j] % 2 then b[i][j] = a[i][j]; else b[i][j] = a[i][j]+1.
We can implement.
But double-check: Some might think about overflow or negative? a positive.
Let's test with the sample: For first test case using our method:
n=3 m=2
Row1: a[1][1]=1, (1+1)%2=0 (even). 1%2=1 => mismatch => b=2. a[1][2]=2, (1+2)%2=1 (odd). 2%2=0 => mismatch? Actually desired odd, 2 even -> mismatch => b=3. But output sample row1 is 1 2. So they have different assignments. Our row1 would be 2 3. Check adjacency: (1,1)=2 even, (1,2)=3 odd -> ok. (1,1) and (2,1): a[2][1]=4, (2+1)%2=1 odd. 4%2=0 -> mismatch -> b[2][1]=5. So row2: (2,1)=5 odd, (2,2)=5? a[2][2]=5, (2+2)%2=0 even, 5%2=1 mismatch => b=6. So row2: 5 6. Output sample row2: 5 6 (they match). Row3: a[3][1]=7, (3+1)%2=0 even, 7%2=1 mismatch -> b=8; a[3][2]=8, (3+2)%2=1 odd, 8%2=0 mismatch -> b=9. Our row3 would be 8 9 but sample row3 is 7 8. So our matrix would be:
2 3
5 6
8 9
All adjacent cells are different? Let's check: (1,1)=2, (1,2)=3 diff; (1,1)=2, (2,1)=5 diff; (1,2)=3, (2,2)=6 diff; (2,1)=5, (2,2)=6 diff; (2,1)=5, (3,1)=8 diff; (2,2)=6, (3,2)=9 diff; (3,1)=8, (3,2)=9 diff. So it's good. So it's valid.
Thus we can output any such pattern. So the solution is correct.
But wait: In the third sample test case: Input:
2 2
1 3
2 2
Output:
2 4
3 2
Let's see if our method yields something else: For i=1,j=1: a=1, (1+1)%2=0 even, 1%2=1 mismatch => b=2. i=1,j=2: a=3, (1+2)%2=1 odd, 3%2=1 match => b=3. i=2,j=1: a=2, (2+1)%2=1 odd, 2%2=0 mismatch => b=3. i=2,j=2: a=2, (2+2)%2=0 even, 2%2=0 match => b=2. So b = [[2,3],[3,2]] which matches sample output? Sample output: row1: 2 4, row2: 3 2. Wait sample says: "2 4 \n 3 2" Actually third case: Input: after "2 2\n1 3\n2 2" output: "2 4\n3 2". So their b[1][2]=4, while ours is 3. But both are valid: check sample adjacency: 2-4 diff, 2-3 diff, 4-2 diff, 3-2 diff. So valid. So our pattern also yields a valid matrix.
Thus algorithm is correct.
But there is nuance: The parity pattern we described ensures adjacent cells have opposite parity, thus different values. However, what about potential overflow or something? Not.
Thus solution: For each test case, read n,m, read matrix a, compute b as described, output.
Time complexity: O(t * n * m) which is fine.
Now, I recall that sometimes there may be a nuance: a_{i,j} can be up to 1e9, and b may be a+1 up to 1e9+1, which fits in 32-bit? Actually 1e9+1 fits in 32-bit signed int? 1e9 is 1000000000, +1 = 1000000001 < 2^31-1 (2147483647) so fits in 32-bit signed int. But we might just use int. Or long long for safety.
Implementation: Use Python, C++ etc.
Now, we should produce answer with this reasoning. So final answer: For each cell, if ((i+j) % 2) == (a[i][j] % 2), output a[i][j]; else output a[i][j] + 1.
But I'll also consider alternative: Some might choose pattern based on parity of a[i][j] itself; but our method is deterministic.
Edge Cases: n=1,m=1: Then no adjacent cells, any value works. Our method outputs either a or a+1 depending on parity: e.g., if a=1, (1+1)=2 even, a%2=1 mismatch -> output 2; but it's valid. So fine.
Proof: For any two adjacent cells (i,j) and (i+1,j) or (i,j+1), they have different (i+j) mod 2, so their b values have different parity, hence b values cannot be equal.
Thus solution correct.
Now I'll produce final answer.
But let's double-check if there's any hidden catch: The statement says "Artem wants to increment the values in some cells by one to make a good." That means each cell can be incremented at most once. Our method increments exactly those cells where parity mismatches. That's at most one increment per cell. So allowed.
Thus done.
I'll now write the solution description for the final answer.
Now, for the output format: Each test case output n lines with m integers separated by spaces.
In Python: read int t, loop, read n,m, then read n lines of m ints, construct b, print.
Pseudo:
def solve():
import sys
data = sys.stdin.read().split()
it = iter(data)
t = int(next(it))
out = []
for _ in range(t):
n = int(next(it)); m = int(next(it))
for i in range(1, n+1):
row = []
for j in range(1, m+1):
a = int(next(it))
if (i+j) % 2 == a % 2:
row.append(str(a))
else:
row.append(str(a+1))
out.append(' '.join(row))
sys.stdout.write('\n'.join(out))
That's it.
Now, note: The sample input:
3
3 2
1 2
4 5
7 8
2 2
1 1
3 3
2 2
1 3
2 2
Our output would be:
2 3
5 6
8 9
2 1
4 3
2 3
3 2
Wait, for the second test case: n=2,m=2:
a: 1 1; 3 3 (since rows: first row "1 1", second "3 3")
Our method:
i=1,j=1: (2) even, a=1 odd => b=2
i=1,j=2: (3) odd, a=1 odd => b=1? Actually a=1 odd, desired odd => match => b=1
i=2,j=1: (3) odd, a=3 odd => match => b=3? Wait (2+1)=3 odd, a=3 odd -> match, so b=3
i=2,j=2: (4) even, a=3 odd -> mismatch => b=4
So b = [[2,1],[3,4]]
But sample output for second case is:
2 1
4 3
Actually sample output after first case:
2 1
4 3
That's the second case output? Let's check given output:
Output
1 2
5 6
7 8
2 1
4 3
2 4
3 2
Interpretation: For three test cases, each produces n lines. First test case n=3 so 3 lines: "1 2", "5 6", "7 8". Second test case n=2 so 2 lines: "2 1", "4 3". Third test case n=2 so 2 lines: "2 4", "3 2". So sample output for second case is "2 1\n4 3". Our b for second case is "2 1\n3 4". That's slightly different: row2 col1 we have 3 vs 4; row2 col2 we have 4 vs 3. But both are valid? Check adjacency: Our b: row1: 2 1; row2: 3 4. Adjacent: 2-1 diff, 2-3 diff, 1-4 diff, 3-4 diff. All good. So it's valid. Sample output uses 4 and 3 swapped. So our output is also acceptable.
Thus algorithm passes.
Now, we might consider if there is any scenario where parity assignment might cause two adjacent cells to become equal despite opposite parity? No, because equality requires same parity as well as same numeric value, but if they have opposite parity, they cannot be equal. So it's safe.
Thus final answer: implement.
Now we can produce final answer with explanation and code.
We should ensure to mention that we choose desired parity as (i+j) mod 2.
Now I'll produce final response. ```python
import sys
def solve() -> None:
data = sys.stdin.read().strip().split()
if not data:
return
it = iter(data)
t = int(next(it))
out_lines = []
for _ in range(t):
n = int(next(it))
m = int(next(it))
for i in range(1, n + 1):
row_vals = []
for j in range(1, m + 1):
a = int(next(it))
# desired parity = (i + j) % 2
if (i + j) % 2 == a % 2:
b = a
else:
b = a + 1
row_vals.append(str(b))
out_lines.append(' '.join(row_vals))
sys.stdout.write('\n'.join(out_lines))
if name == "main":
solve()