#Code by Sounak, IIESTS
#------------------------------warmup----------------------------

import os
import sys
import math
from io import BytesIO, IOBase
from fractions import Fraction
import collections
from itertools import permutations
from collections import defaultdict
from collections import deque
import threading

#sys.setrecursionlimit(300000)
#threading.stack_size(10**8)

BUFSIZE = 8192

class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)

class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")

sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")

#-------------------game starts now-----------------------------------------------------
class Factorial:
    def __init__(self, MOD):
        self.MOD = MOD
        self.factorials = [1, 1]
        self.invModulos = [0, 1]
        self.invFactorial_ = [1, 1]

    def calc(self, n):
        if n <= -1:
            print("Invalid argument to calculate n!")
            print("n must be non-negative value. But the argument was " + str(n))
            exit()
        if n < len(self.factorials):
            return self.factorials[n]
        nextArr = [0] * (n + 1 - len(self.factorials))
        initialI = len(self.factorials)
        prev = self.factorials[-1]
        m = self.MOD
        for i in range(initialI, n + 1):
            prev = nextArr[i - initialI] = prev * i % m
        self.factorials += nextArr
        return self.factorials[n]

    def inv(self, n):
        if n <= -1:
            print("Invalid argument to calculate n^(-1)")
            print("n must be non-negative value. But the argument was " + str(n))
            exit()
        p = self.MOD
        pi = n % p
        if pi < len(self.invModulos):
            return self.invModulos[pi]
        nextArr = [0] * (n + 1 - len(self.invModulos))
        initialI = len(self.invModulos)
        for i in range(initialI, min(p, n + 1)):
            next = -self.invModulos[p % i] * (p // i) % p
            self.invModulos.append(next)
        return self.invModulos[pi]

    def invFactorial(self, n):
        if n <= -1:
            print("Invalid argument to calculate (n^(-1))!")
            print("n must be non-negative value. But the argument was " + str(n))
            exit()
        if n < len(self.invFactorial_):
            return self.invFactorial_[n]
        self.inv(n)  # To make sure already calculated n^-1
        nextArr = [0] * (n + 1 - len(self.invFactorial_))
        initialI = len(self.invFactorial_)
        prev = self.invFactorial_[-1]
        p = self.MOD
        for i in range(initialI, n + 1):
            prev = nextArr[i - initialI] = (prev * self.invModulos[i % p]) % p
        self.invFactorial_ += nextArr
        return self.invFactorial_[n]

class Combination:
    def __init__(self, MOD):
        self.MOD = MOD
        self.factorial = Factorial(MOD)

    def ncr(self, n, k):
        if k < 0 or n < k:
            return 0
        k = min(k, n - k)
        f = self.factorial
        return f.calc(n) * f.invFactorial(max(n - k, k)) * f.invFactorial(min(k, n - k)) % self.MOD
#-------------------------------------------------------------------------
mod=10**9+7
for _ in range (int(input())):
    n,m=map(int,input().split())
    a=list()
    for i in range (n):
        b=list(map(int,input().split()))
        a.append(b)
    for i in range (n):
        for j in range (m):
            c=0
            el=a[i][j]
            if i!=0 and a[i-1][j]==el+1:
                c=1
            if i!=n-1 and a[i+1][j]==el+1:
                c=1
            if j!=0 and a[i][j-1]==el+1:
                c=1
            if j!=m-1 and a[i][j+1]==el+1:
                c=1
            if c==0:
                a[i][j]+=1
    for i in range (n):
        print(*a[i])


for _ in range(int(input())):
    n,m = map(int,input().split())
    mat = [[int(x) for x in input().split()] for y in range(n)]
    for i in range(n):
        for j in range(m):
            if mat[i][j] % 2 != (i+j) % 2:
                mat[i][j] += 1
    for x in mat:
        print(" ".join([str(y) for y in x]))


import sys
import math
import bisect
from sys import stdin, stdout
from math import gcd, floor, sqrt, log
from collections import defaultdict as dd
from bisect import bisect_left as bl, bisect_right as br
from collections import Counter
from collections import defaultdict as dd

#sys.setrecursionlimit(100000000)

inp = lambda: int(input())
strng = lambda: input().strip()
jn = lambda x, l: x.join(map(str, l))
strl = lambda: list(input().strip())
mul = lambda: map(int, input().strip().split())
mulf = lambda: map(float, input().strip().split())
seq = lambda: list(map(int, input().strip().split()))

ceil = lambda x: int(x) if (x == int(x)) else int(x) + 1
ceildiv = lambda x, d: x // d if (x % d == 0) else x // d + 1

flush = lambda: stdout.flush()
stdstr = lambda: stdin.readline()
stdint = lambda: int(stdin.readline())
stdpr = lambda x: stdout.write(str(x))
stdmap = lambda: map(int, stdstr().split())
stdarr = lambda: list(map(int, stdstr().split()))

mod = 1000000007

for _ in range(stdint()):
    n,m = stdmap()

    matrix = []

    for i in range(n):
        row = stdarr()
        matrix.append(row)

    eo = [[None for __ in range(m)] for ___ in range(n)]

    change = [[0 for __ in range(m)] for ___ in range(n)]

    for i in range(n):
        for j in range(m):
            if(matrix[i][j]%2 == 0):
                eo[i][j] = 0
            else:
                eo[i][j] = 1

    f = eo[0][0]

    lastR = f
    last = f

    suitable = [[None for __ in range(m)] for ___ in range(n)]

    for i in range(n):
        if(i != 0):
            lastR ^= 1
            last = lastR
        for j in range(m):
            if(i == 0 and j == 0):
                suitable[i][j] = f
            elif(j == 0):
                suitable[i][j] = lastR
            else:
                last ^= 1
                suitable[i][j] = last

    for i in range(n):
        for j in range(m):
            if(suitable[i][j] != eo[i][j]):
                matrix[i][j] += 1

    for i in range(n):
        print(*matrix[i])


import sys
import math
import bisect
from sys import stdin, stdout
from math import gcd, floor, sqrt, log
from collections import defaultdict as dd
from bisect import bisect_left as bl, bisect_right as br
from collections import Counter
from collections import defaultdict as dd

#sys.setrecursionlimit(100000000)

inp = lambda: int(input())
strng = lambda: input().strip()
jn = lambda x, l: x.join(map(str, l))
strl = lambda: list(input().strip())
mul = lambda: map(int, input().strip().split())
mulf = lambda: map(float, input().strip().split())
seq = lambda: list(map(int, input().strip().split()))

ceil = lambda x: int(x) if (x == int(x)) else int(x) + 1
ceildiv = lambda x, d: x // d if (x % d == 0) else x // d + 1

flush = lambda: stdout.flush()
stdstr = lambda: stdin.readline()
stdint = lambda: int(stdin.readline())
stdpr = lambda x: stdout.write(str(x))
stdmap = lambda: map(int, stdstr().split())
stdarr = lambda: list(map(int, stdstr().split()))

mod = 1000000007

for _ in range(stdint()):
    n,m = stdmap()

    matrix = []

    for i in range(n):
        row = stdarr()
        matrix.append(row)

    eo = [[None for __ in range(m)] for ___ in range(n)]

    change = [[0 for __ in range(m)] for ___ in range(n)]

    for i in range(n):
        for j in range(m):
            if(matrix[i][j]%2 == 0):
                eo[i][j] = 0
            else:
                eo[i][j] = 1

    f = eo[0][0]

    lastR = f
    last = f

    suitable = [[None for __ in range(m)] for ___ in range(n)]

    for i in range(n):
        if(i != 0):
            lastR ^= 1
        for j in range(m):
            if(i == 0 and j == 0):
                suitable[i][j] = f
            elif(j == 0):
                suitable[i][j] = lastR
            else:
                last ^= 1
                suitable[i][j] = last

    for i in range(n):
        for j in range(m):
            if(suitable[i][j] != eo[i][j]):
                matrix[i][j] += 1

    for i in range(n):
        print(*matrix[i])


t=int(input())

for _ in range(t):
	n,m=map(int,input().split())
	mat=[[] for i in range(n)]
	for o in range(n):
		mat[o]=list(map(int,input().split()))

	for j in range(1,m):
		if mat[0][j]==mat[0][j-1]:
			mat[0][j]+=1
	for i in range(1,n):
		if mat[i][0]==mat[i-1][0]:
			mat[i][0]+=1
		for j in range(1,m):
			if mat[i][j]==mat[i][j-1]:
				mat[i][j]+=1

	for i in range(n):
		print(*mat[i])


t=int(input())

for _ in range(t):
	n,m=map(int,input().split())
	mat=[[] for i in range(n)]
	for o in range(n):
		mat[o]=list(map(int,input().split()))

	for j in range(1,m):
		if mat[0][j]==mat[0][j-1]:
			mat[0][j]+=1
	for i in range(1,n):
		for j in range(m):
			if mat[i][j]==mat[i][j-1]:
				mat[i][j]+=1

	for i in range(n):
		print(*mat[i])


t=int(input())

ad=[(1,0),(0,1),(-1,0),(0,-1)]
def aze(x,y):
	if x<n and x>-1 and y<m and y>-1:
		return(True)
	return(False)

def dfs(k):
	q=[k]
	while q:
		x=q.pop()
		i,j=x//m,x%m
		for w in ad:
			ip,jp=i+w[0],j+w[1]
			if not aze(ip,jp):continue
			if vi[jp+ip*m] or mat[i][j]!=mat[ip][jp]:continue
			q.append(jp+ip*m)
			mat[ip][jp]+=1
			vi[jp+ip*m]=True
for _ in range(t):
	n,m=map(int,input().split())
	mat=[[] for i in range(n)]
	vi=[False for i in range(n*m)]
	for o in range(n):
		mat[o]=list(map(int,input().split()))

	for i in range(n):
		for j in range(m):
			dfs(j+i*m)

	for i in range(n):
		print(*mat[i])


t=int(input())

print()
def aze(i,j,u,v):
	if i>-1 and i<n and j<m and j>-1 and mat[u][v]==mat[i][j]:
		return(True)
	return(False)

for _ in range(t):
	n,m=map(int,input().split())
	mat=[[] for i in range(n)]
	for o in range(n):
		mat[o]=list(map(int,input().split()))

	for i in range(n):
		for j in range(m):
			if aze(i+1,j,i,j) or aze(i,j+1,i,j) or aze(i-1,j,i,j) or aze(i,j-1,i,j):
				mat[i][j]+=1
	for i in range(n):
		print(*mat[i])
	print()


t=int(input())

ad=[(1,0),(0,1),(-1,0),(0,-1)]
def aze(x,y):
	if x<n and x>-1 and y<m and y>-1:
		return(True)
	return(False)

def dfs(k):
	q=[k]
	while q:
		x=q.pop()
		i,j=x//m,x%m
		for w in ad:
			ip,jp=i+w[0],j+w[1]
			if not aze(ip,jp):continue
			if mat[i][j]!=mat[ip][jp]:continue
			q.append(jp+ip*m)
			if vi[jp+ip*m]:
				mat[ip][jp]-=1
				vi[jp+ip*m]=False
			else:mat[ip][jp]+=1

for _ in range(t):
	n,m=map(int,input().split())
	mat=[[] for i in range(n)]
	vi=[False for i in range(n*m)]
	for o in range(n):
		mat[o]=list(map(int,input().split()))

	for i in range(n):
		for j in range(m):
			dfs(j+i*m)

	for i in range(n):
		print(*mat[i])


t=int(input())

ad=[(1,0),(0,1),(-1,0),(0,-1)]
def aze(x,y):
	if x<n and x>-1 and y<m and y>-1:
		return(True)
	return(False)
print()
def dfs(k):
	q=[k]
	while q:
		x=q.pop()
		i,j=x//m,x%m
		for w in ad:
			ip,jp=i+w[0],j+w[1]
			if not aze(ip,jp):continue
			if mat[i][j]!=mat[ip][jp]:continue
			q.append(jp+ip*m)
			if vi[jp+ip*m]:
				mat[ip][jp]-=1
				vi[jp+ip*m]=False
			else:
				mat[ip][jp]+=1
				vi[jp+ip*m]=True

for _ in range(t):
	n,m=map(int,input().split())
	mat=[[] for i in range(n)]
	vi=[False for i in range(n*m)]
	for o in range(n):
		mat[o]=list(map(int,input().split()))

	for i in range(n):
		for j in range(m):
			dfs(j+i*m)

	for i in range(n):
		print(*mat[i])
	print()


import bisect
import collections
import copy
import functools
import heapq
import itertools
import math
import random
import re
import sys
import time
import string
sys.setrecursionlimit(99999)

for _ in range(int(input())):
    m,n = map(int,input().split())
    for i in range(m):
        arr = list(map(int,input().split()))
        for j in range(n):
            if (arr[j]-i-j)&1:
                arr[j]+=1
        print(*arr)


import sys
def get_ints(): return list(map(int, sys.stdin.readline().strip().split()))
t=int(input())
for _ in range(t):
    n,m=get_ints()
    a=[]
    for i in range(n):
        a.append(get_ints())
    for i in range(n):
        for j in range(m):
            x,y=a[i][j],a[i][j]+1
            c,b=-1,-1
            if i-1>=0:
                c=a[i-1][j]
            if j-1>=0:
                b=a[i][j-1]
            if y!=c and y!=b:
                u,v=-1,-1
                if i+1<n:
                    u=a[i+1][j]
                if j+1<m:
                    v=a[i][j+1]
                if x==u or x==v:
                    a[i][j]=y
    for i in range(n):
        print(*a[i])


import sys
def get_ints(): return list(map(int, sys.stdin.readline().strip().split()))
t=int(input())
for _ in range(t):
    n,m=get_ints()
    a=[]
    for i in range(n):
        a.append(get_ints())
    for i in range(n):
        for j in range(m):
            if (i+j)%2==0:
                if a[i][j]%2!=0:
                    a[i][j]=a[i][j]+1
            else:
                if a[i][j]%2!=1:
                    a[i][j]=a[i][j]+1
    for i in range(n):
        print(*a[i])


import sys
def get_ints(): return list(map(int, sys.stdin.readline().strip().split()))
t=int(input())
for _ in range(t):
    n,m=get_ints()
    a=[]
    for i in range(n):
        a.append(get_ints())
    for i in range(n):
        for j in range(m):
            if (i+j)%2==0:
                if a[i][j]%2!=0:
                    a[i][j]=a[i][j]+1
            else:
                if a[i][j]%2==1:
                    a[i][j]=a[i][j]+1
    for i in range(n):
        print(*a[i])


from copy import deepcopy

def solve(a, n, m, changed):
    need_change = []
    for i in range(n):
        for j in range(m):

            if i < n - 1 and a[i][j] == a[i + 1][j]:
                need_change.append((i, j))
                continue
            if i > 0 and a[i][j] == a[i - 1][j]:
                need_change.append((i, j))
                continue
            if j < m - 1 and a[i][j] == a[i][j + 1]:
                need_change.append((i, j))
                continue
            if j > 0 and a[i][j] == a[i][j - 1]:
                need_change.append((i, j - 1))
                continue
    need_change_filtered = []
    for (i, j) in need_change:
        if i < n - 1 and a[i][j] == a[i + 1][j] - 1:
            continue
        if i > 0 and a[i][j] == a[i - 1][j] - 1:
            continue
        if j < m - 1 and a[i][j] == a[i][j + 1] - 1:
            continue
        if j > 0 and a[i][j] == a[i][j - 1] - 1:
            continue

        need_change_filtered.append((i, j))

    need_change_filtered = set(need_change_filtered)

    while len(need_change_filtered):
        (i, j) = need_change_filtered.pop()
        a[i][j] += 1
        if (i - 1, j) in need_change_filtered:
            need_change_filtered.remove((i - 1, j))
        if (i + 1, j) in need_change_filtered:
            need_change_filtered.remove((i + 1, j))
        if (i, j - 1) in need_change_filtered:
            need_change_filtered.remove((i, j - 1))
        if (i, j + 1) in need_change_filtered:
            need_change_filtered.remove((i, j + 1))

    return True, a

if __name__ == "__main__":
    t = int(input())

    while t:
        t -= 1
        n, m = map(int, input().split())
        a = [list(map(int, input().split())) for _ in range(n)]
        changed = [[False for _ in range(m)] for _ in range(n)]
        _, a = solve(a, n, m, changed)
        for row in a:
            for item in row:
                print(item, end=" ")
            print()


from copy import deepcopy

def solve(a, n, m, changed):
    need_change = []
    for i in range(n):
        for j in range(m):

            if i < n - 1 and a[i][j] == a[i + 1][j]:
                need_change.append((i, j))
                continue
            if i > 0 and a[i][j] == a[i - 1][j]:
                need_change.append((i, j))
                continue
            if j < m - 1 and a[i][j] == a[i][j + 1]:
                need_change.append((i, j))
                continue
            if j > 0 and a[i][j] == a[i][j - 1]:
                need_change.append((i, j))
                continue
    need_change_filtered = []
    for (i, j) in need_change:
        if i < n - 1 and a[i][j] == a[i + 1][j] - 1:
            continue
        if i > 0 and a[i][j] == a[i - 1][j] - 1:
            continue
        if j < m - 1 and a[i][j] == a[i][j + 1] - 1:
            continue
        if j > 0 and a[i][j] == a[i][j - 1] - 1:
            continue

        need_change_filtered.append((i, j))

    need_change_filtered = set(need_change_filtered)

    while len(need_change_filtered):
        (i, j) = need_change_filtered.pop()
        a[i][j] += 1
        if (i - 1, j) in need_change_filtered:
            need_change_filtered.remove((i - 1, j))
        if (i + 1, j) in need_change_filtered:
            need_change_filtered.remove((i + 1, j))
        if (i, j - 1) in need_change_filtered:
            need_change_filtered.remove((i, j - 1))
        if (i, j + 1) in need_change_filtered:
            need_change_filtered.remove((i, j + 1))

    return True, a

if __name__ == "__main__":
    t = int(input())

    while t:
        t -= 1
        n, m = map(int, input().split())
        a = [list(map(int, input().split())) for _ in range(n)]
        changed = [[False for _ in range(m)] for _ in range(n)]
        _, a = solve(a, n, m, changed)
        for row in a:
            for item in row:
                print(item, end=" ")
            print()


def solve(a):
    for i in range(len(a)):
        for j in range(len(a[i])):
            if (i + j) % 2 == 0 and a[i][j] % 2 != 0:
                a[i][j] += 1
            elif (i + j) % 2 != 0 and a[i][j] % 2 == 0:
                a[i][j] += 1

    for row in a:
        for item in row:
            print(item, end = " ")
        print()

if __name__ == "__main__":
    t = int(input())

    while t:
        t -= 1
        n, m = map(int, input().split())

        a = [list(map(int, input().split())) for _ in range(n)]
        solve(a)


from copy import deepcopy

def solve(a, n, m):
    for i in range(n):
        for j in range(m):
            need_change = []
            if i < n - 1 and a[i][j] == a[i + 1][j]:
                need_change.append((i + 1, j))
            if i > 0 and a[i][j] == a[i - 1][j]:
                need_change.append((i - 1, j))
            if j < m - 1 and a[i][j] == a[i][j + 1]:
                need_change.append((i, j + 1))
            if j > 0 and a[i][j] == a[i][j - 1]:
                need_change.append((i, j - 1))

            if len(need_change) > 0:
                m1 = deepcopy(a)
                m1[i][j] += 1
                result, mat = solve(m1, n, m)

                if result:
                    return True, mat

                for (k, t) in need_change:
                    a[k][t] += 1

                result, mat = solve(a, n, m)
                if result:
                    return True, mat
    return True, a

if __name__ == "__main__":
    t = int(input())

    while t:
        t -= 1
        n, m = map(int, input().split())
        a = [list(map(int, input().split())) for _ in range(n)]

        _, a = solve(a, n, m)
        for row in a:
            for item in row:
                print(item, end=" ")
            print()


from copy import deepcopy

def solve(a, n, m, changed):
    for i in range(n):
        for j in range(m):
            need_change = []
            if i < n - 1 and a[i][j] == a[i + 1][j]:
                need_change.append((i + 1, j))
            if i > 0 and a[i][j] == a[i - 1][j]:
                need_change.append((i - 1, j))
            if j < m - 1 and a[i][j] == a[i][j + 1]:
                need_change.append((i, j + 1))
            if j > 0 and a[i][j] == a[i][j - 1]:
                need_change.append((i, j - 1))

            if len(need_change) > 0:
                flag = False
                if not changed[i][j]:
                    flag = True
                    m1 = deepcopy(a)
                    ch_copy = deepcopy(changed)
                    m1[i][j] += 1
                    changed[i][j] = True
                    result, mat = solve(m1, n, m, changed)

                    if result:
                        return True, mat

                if all([not changed[r][c] for r,c in need_change]):
                    flag = True
                    ch_copy = deepcopy(changed)
                    for (k, t) in need_change:
                        a[k][t] += 1
                        ch_copy[k][t] = True

                    result, mat = solve(a, n, m, ch_copy)
                    if result:
                        return True, mat
                if not flag:
                    return False, a
    return True, a

if __name__ == "__main__":
    t = int(input())

    while t:
        t -= 1
        n, m = map(int, input().split())
        a = [list(map(int, input().split())) for _ in range(n)]
        changed = [[False for _ in range(m)] for _ in range(n)]
        _, a = solve(a, n, m, changed)
        for row in a:
            for item in row:
                print(item, end=" ")
            print()


##

for _ in range(int(input())):
	v=[]
	n,m=map(int,input().split())
	for i in range(n):
		v.append(list(map(int,input().split())))
	for i in range(n):
		for j in range(m):
			if (i+j)%2 != v[i][j]%2:
				v[i][j]+=1
	for i in range(len(v)):
		print(*v[i])


for _ in range(int(input())):
	v=[]
	n,m=map(int,input().split())
	for i in range(n):
		v.append(list(map(int,input().split())))
# print(v)
	for i in range(n):
		for j in range(m-1):
			# print(v[i][j],end=' ')
			if v[i][j]==v[i][j+1]:
				v[i][j]+=1
		# print()

	# print(v)
	for i in range(len(v)):
		print(*v[i])


for _ in range(int(input())):
	n,m=map(int,input().split())
	l=[]
	for i in range(n):
		lt=list(map(int,input().split()))
		l.append(lt)
	for i in range(n):
		for j in range(m):
			if((i+j)%2!=(l[i][j])%2):
				l[i][j]+=1
			print(l[i][j],end=" ")
		print()


import sys
# import math
# import re
# from heapq import *
# from collections import defaultdict as dd
# from collections import OrderedDict as odict
# from collections import Counter as cc
# from collections import deque
# sys.setrecursionlimit(3*(10**5)+1)#thsis is must
# mod = 10**9+7; md = 998244353
# m = 2**32
input = lambda: sys.stdin.readline().strip()
inp = lambda: list(map(int,sys.stdin.readline().strip().split()))
# def C(n,r,mod):
# 	if r>n:
# 		return 0
# 	num = den = 1
# 	for i in range(r):
# 		num = (num*(n-i))%mod
# 		den = (den*(i+1))%mod
# 	return (num*pow(den,mod-2,mod))%mod
# def pfcs(M = 100000):
# 	pfc = [0]*(M+1)
# 	for i in range(2,M+1):
# 		if pfc[i]==0:
# 			for j in range(i,M+1,i):
# 				pfc[j]+=1
# 	return pfc
#______________________________________________________
for _ in range(int(input())):
	n,m = list(map(int,input().split()))
	b = [list(map(int,input().split())) for i in range(n)]
	for i in range(n):
		for j in range(m):
			if (i+j)%2==b[i][j]%2:
				b[i][j]+=1
	for i in b:
		print(*i)


import sys
# import math
# import re
# from heapq import *
# from collections import defaultdict as dd
# from collections import OrderedDict as odict
# from collections import Counter as cc
# from collections import deque
# sys.setrecursionlimit(3*(10**5)+1)#thsis is must
# mod = 10**9+7; md = 998244353
# m = 2**32
input = lambda: sys.stdin.readline().strip()
inp = lambda: list(map(int,sys.stdin.readline().strip().split()))
# def C(n,r,mod):
# 	if r>n:
# 		return 0
# 	num = den = 1
# 	for i in range(r):
# 		num = (num*(n-i))%mod
# 		den = (den*(i+1))%mod
# 	return (num*pow(den,mod-2,mod))%mod
# def pfcs(M = 100000):
# 	pfc = [0]*(M+1)
# 	for i in range(2,M+1):
# 		if pfc[i]==0:
# 			for j in range(i,M+1,i):
# 				pfc[j]+=1
# 	return pfc
#______________________________________________________
for _ in range(int(input())):
	n,m = list(map(int,input().split()))
	b = [list(map(int,input().split())) for i in range(n)]
	# b = [[0 for i in range(m)] for j in range(n)]
	d = [[1,0],[-1,0],[0,1],[0,-1]]
	def dir(i,j):
		res = []
		for l,r in d:
			row = i+l
			col = j+r
			if row<0 or row>=n or col<0 or col>=m:
				continue
			# print(row,col,"lol")s
			res.append(b[row][col])
		return res
	for i in range(n):
		for j in range(m):
			tar = b[i][j]
			res = dir(i,j)
			count = res.count(tar)
			count2 = res.count(tar+1)
			if count2==0 and count>=1:
				b[i][j] = tar+1
			else:
				b[i][j] = tar
	for i in b:
		print(*i)


import sys
# import math
# import re
# from heapq import *
# from collections import defaultdict as dd
# from collections import OrderedDict as odict
# from collections import Counter as cc
# from collections import deque
# sys.setrecursionlimit(3*(10**5)+1)#thsis is must
# mod = 10**9+7; md = 998244353
# m = 2**32
input = lambda: sys.stdin.readline().strip()
inp = lambda: list(map(int,sys.stdin.readline().strip().split()))
# def C(n,r,mod):
# 	if r>n:
# 		return 0
# 	num = den = 1
# 	for i in range(r):
# 		num = (num*(n-i))%mod
# 		den = (den*(i+1))%mod
# 	return (num*pow(den,mod-2,mod))%mod
# def pfcs(M = 100000):
# 	pfc = [0]*(M+1)
# 	for i in range(2,M+1):
# 		if pfc[i]==0:
# 			for j in range(i,M+1,i):
# 				pfc[j]+=1
# 	return pfc
#______________________________________________________
for _ in range(int(input())):
	n,m = list(map(int,input().split()))
	b = [list(map(int,input().split())) for i in range(n)]
	# b = [[0 for i in range(m)] for j in range(n)]
	d = [[1,0],[-1,0],[0,1],[0,-1]]
	def dir(i,j):
		res = []
		for l,r in d:
			row = i+l
			col = j+r
			if row<0 or row>=n or col<0 or col>=m:
				continue
			# print(row,col,"lol")s
			res.append(b[row][col])
		return res
	for i in range(n):
		for j in range(m):
			tar = b[i][j]
			res = dir(i,j)
			# print(res)
			count = res.count(tar)
			count2 = res.count(tar+1)
			if count2==0:
				b[i][j]+=1
	# for i in range(n):
	# 	for j in range(m):
	# 		tar = b[i][j]
	# 		res = dir(i,j)
	# 		count =  res.count(tar)
	# 		count2 = res.count(tar+1)
	# 		if count2==0 and count==1:
	# 			b[i][j]+=1

	for i in b:
		print(*i)


import sys
# import math
# import re
# from heapq import *
# from collections import defaultdict as dd
# from collections import OrderedDict as odict
# from collections import Counter as cc
# from collections import deque
# sys.setrecursionlimit(3*(10**5)+1)#thsis is must
# mod = 10**9+7; md = 998244353
# m = 2**32
input = lambda: sys.stdin.readline().strip()
inp = lambda: list(map(int,sys.stdin.readline().strip().split()))
# def C(n,r,mod):
# 	if r>n:
# 		return 0
# 	num = den = 1
# 	for i in range(r):
# 		num = (num*(n-i))%mod
# 		den = (den*(i+1))%mod
# 	return (num*pow(den,mod-2,mod))%mod
# def pfcs(M = 100000):
# 	pfc = [0]*(M+1)
# 	for i in range(2,M+1):
# 		if pfc[i]==0:
# 			for j in range(i,M+1,i):
# 				pfc[j]+=1
# 	return pfc
#______________________________________________________
for _ in range(int(input())):
	n,m = list(map(int,input().split()))
	b = [list(map(int,input().split())) for i in range(n)]
	# b = [[0 for i in range(m)] for j in range(n)]
	d = [[1,0],[-1,0],[0,1],[0,-1]]
	def dir(i,j):
		res = []
		for l,r in d:
			row = i+l
			col = j+r
			if row<0 or row>=n or col<0 or col>=m:
				continue
			# print(row,col,"lol")s
			res.append(b[row][col])
		return res
	for i in range(n):
		for j in range(m):
			tar = b[i][j]
			res = dir(i,j)
			count = res.count(tar)
			count2 = res.count(tar+1)
			if count2==0 and count==1:
				b[i][j] = tar+1
			else:
				b[i][j] = tar
	for i in b:
		print(*i)


import sys
# import math
# import re
# from heapq import *
# from collections import defaultdict as dd
# from collections import OrderedDict as odict
# from collections import Counter as cc
# from collections import deque
# sys.setrecursionlimit(3*(10**5)+1)#thsis is must
# mod = 10**9+7; md = 998244353
# m = 2**32
input = lambda: sys.stdin.readline().strip()
inp = lambda: list(map(int,sys.stdin.readline().strip().split()))
# def C(n,r,mod):
# 	if r>n:
# 		return 0
# 	num = den = 1
# 	for i in range(r):
# 		num = (num*(n-i))%mod
# 		den = (den*(i+1))%mod
# 	return (num*pow(den,mod-2,mod))%mod
# def pfcs(M = 100000):
# 	pfc = [0]*(M+1)
# 	for i in range(2,M+1):
# 		if pfc[i]==0:
# 			for j in range(i,M+1,i):
# 				pfc[j]+=1
# 	return pfc
#______________________________________________________
for _ in range(int(input())):
	n,m = list(map(int,input().split()))
	b = [list(map(int,input().split())) for i in range(n)]
	# b = [[0 for i in range(m)] for j in range(n)]
	d = [[1,0],[-1,0],[0,1],[0,-1]]
	def dir(i,j):
		res = []
		for l,r in d:
			row = i+l
			col = j+r
			if row<0 or row>=n or col<0 or col>=m:
				continue
			# print(row,col,"lol")s
			res.append(b[row][col])
		return res
	for i in range(n):
		for j in range(m):
			tar = b[i][j]
			res = dir(i,j)
			# print(res)
			count = res.count(tar)
			count2 = res.count(tar+1)
			if count>1:
				b[i][j] = tar+1
	for i in range(n):
		for j in range(m):
			tar = b[i][j]
			res = dir(i,j)
			count =  res.count(tar)
			count2 = res.count(tar+1)
			if count2==0 and count==1:
				b[i][j]+=1

	for i in b:
		print(*i)


import sys
# import math
# import re
# from heapq import *
# from collections import defaultdict as dd
# from collections import OrderedDict as odict
# from collections import Counter as cc
# from collections import deque
# sys.setrecursionlimit(3*(10**5)+1)#thsis is must
# mod = 10**9+7; md = 998244353
# m = 2**32
input = lambda: sys.stdin.readline().strip()
inp = lambda: list(map(int,sys.stdin.readline().strip().split()))
# def C(n,r,mod):
# 	if r>n:
# 		return 0
# 	num = den = 1
# 	for i in range(r):
# 		num = (num*(n-i))%mod
# 		den = (den*(i+1))%mod
# 	return (num*pow(den,mod-2,mod))%mod
# def pfcs(M = 100000):
# 	pfc = [0]*(M+1)
# 	for i in range(2,M+1):
# 		if pfc[i]==0:
# 			for j in range(i,M+1,i):
# 				pfc[j]+=1
# 	return pfc
#______________________________________________________
for _ in range(int(input())):
	n,m = list(map(int,input().split()))
	b = [list(map(int,input().split())) for i in range(n)]
	# b = [[0 for i in range(m)] for j in range(n)]
	d = [[1,0],[-1,0],[0,1],[0,-1]]
	def dir(i,j):
		res = []
		for l,r in d:
			row = i+l
			col = j+r
			if row<0 or row>=n or col<0 or col>=m:
				continue
			# print(row,col,"lol")s
			res.append(b[row][col])
		return res
	for i in range(n):
		for j in range(m):
			tar = b[i][j]
			res = dir(i,j)
			count = res.count(tar)
			count2 = res.count(tar+1)
			if count2==0 and count<2:
				b[i][j] = tar+1
			else:
				b[i][j] = tar
	for i in b:
		print(*i)


'''
# Sample code to perform I/O:

name = input()                  # Reading input from STDIN
print('Hi, %s.' % name)         # Writing output to STDOUT

# Warning: Printing unwanted or ill-formatted data to output will cause the test cases to fail
'''

# Write your code here

import sys
import heapq
import copy
import math
import decimal
input = sys.stdin.readline
def inp():
    return(int(input()))
def inlt():
    return(list(map(int,input().split())))
def insr():
    s = input()
    return(list(s[:len(s) - 1]))
def insr2():
    s = input()
    s = list(s[:len(s) - 1])
    s = [ord(s[i]) - ord("a") for i in range(len(s))]
    return s
def invr():
    return(map(int,input().split()))
############ ---- Input Functions ---- #######End
# #####
def pr_list(a):
    print( *a , sep=" ")

def  solve():
    [n,m]  = inlt()
    grid = []
    for i in range(n):
        a = inlt()
        grid.append(a)
    for i in range(n):
        for j in range(m):
            if (i+j)%2 != grid[i][j]%2 :
                grid[i][j]= grid[i][j] + 1
    for i in range(n):
        pr_list(grid[i])

def main():
    test = 1

    test = inp()
    for i in range(test):
        solve()

if __name__== "__main__":
    main()


'''
# Sample code to perform I/O:

name = input()                  # Reading input from STDIN
print('Hi, %s.' % name)         # Writing output to STDOUT

# Warning: Printing unwanted or ill-formatted data to output will cause the test cases to fail
'''

# Write your code here

import sys
import heapq
import copy
import math
import decimal
input = sys.stdin.readline
def inp():
    return(int(input()))
def inlt():
    return(list(map(int,input().split())))
def insr():
    s = input()
    return(list(s[:len(s) - 1]))
def insr2():
    s = input()
    s = list(s[:len(s) - 1])
    s = [ord(s[i]) - ord("a") for i in range(len(s))]
    return s
def invr():
    return(map(int,input().split()))
############ ---- Input Functions ---- #######End
# #####
def pr_list(a):
    print( *a , sep=" ")

def  solve():
    [n,m]  = inlt()
    grid = []
    for i in range(n):
        a = inlt()
        grid.append(a)
    for i in range(n):
        for j in range(m):
            if i == 0 and j ==0:
                continue
            elif i == 0:
                if grid[i][j] <=grid[i][j -1]:
                    grid[i][j] = grid[i][j -1] + 1
            elif j == 0:
                if grid[i][j] <= grid[i-1][j ]:
                    grid[i][j] = grid[i-1][j] + 1
            else:
                if grid[i][j] <= max(grid[i-1][j] , grid[i][j-1]):
                    grid[i][j] = max(grid[i-1][j] , grid[i][j-1]) + 1
    for i in range(n):
        pr_list(grid[i])
    return

def main():
    test = 1

    test = inp()
    for i in range(test):
        solve()

if __name__== "__main__":
    main()


t = int(input())
for j in range(t):
    n,m = map(int,input().split())
    matrix=[]
    for i in range(n):
        l = list(map(int,input().split()))
        matrix.append(l)
    for i in range(n):
        for j in range(m):
            if i%2==0:
                if matrix[i][j]%2!=j%2:
                    matrix[i][j]+=1
            else:
                if matrix[i][j]%2==j%2:
                    matrix[i][j]+=1
    for j in range(n):
        print(*matrix[j])


t = int(input())
for j in range(t):
    n,m = map(int,input().split())
    matrix = [[0 for i in range(m)]for j in range(n)]
    used = [[False for i in range(m)]for j in range(n)]
    for j in range(n):
        for i in range(m):
            if j%2==0:
                if matrix[i][j]%2!=i%2:
                    matrix[i][j]+=1
            else:
                if matrix[i][j]%2==i%2:
                    matrix[i][j]+=1
    for j in range(n):
        print(*matrix[j])


import sys
input=sys.stdin.readline

T=int(input())

for _ in range(T):
    n,m=map(int,input().split())
    A=[]
    for i in range(n):
        A.append(list(map(int,input().split())))

    for i in range(n):
        for j in range(m):
            if ((A[i][j]%2)!=(i+j)%2):
                A[i][j]=A[i][j]+1

    for i in range(n):
        print(*A[i])


import sys
input=sys.stdin.readline
T=int(input())
for _ in range(T):
    n,m=map(int,input().split())
    A=[]
    for i in range(n):
        A.append(list(map(int,input().split())))

    vis=[[0 for i in range(m)] for j in range(n)]
    B=[[0 for i in range(m)] for j in range(n)]
    for i in range(n):
        for j in range(m):
            B[i][j]=A[i][j]
    for k in range(n):
        for i in range(n):
            for j in range(m):
                if (vis[i][j]==0):
                    #print(i,j)
                    flag=0
                    if (i>0 and B[i-1][j]==(A[i][j]+1)):
                        continue

                    if (i<n-1 and B[i+1][j]==(A[i][j]+1)):
                        continue

                    if (j>0 and B[i][j-1]==(A[i][j]+1)):
                        continue

                    if (j<m-1 and B[i][j+1]==(A[i][j]+1)):
                        continue

                    #print("Hello")
                    if (i>0 and B[i-1][j]==A[i][j]):
                        vis[i-1][j]=1
                        B[i][j]=A[i][j]+1
                        flag=1

                    if (i<n-1 and B[i+1][j]==A[i][j]):
                        vis[i+1][j]=1
                        B[i][j]=A[i][j]+1
                        flag=1

                    if (j>0 and B[i][j-1]==A[i][j]):
                        vis[i][j-1]=1
                        B[i][j]=A[i][j]+1
                        flag=1

                    if (j<m-1 and B[i][j+1]==A[i][j]):
                        vis[i][j+1]=1
                        B[i][j]=A[i][j]+1
                        flag=1

    for i in range(n):
        print(*B[i])


import sys
input=sys.stdin.readline
T=int(input())
for _ in range(T):
    n,m=map(int,input().split())
    A=[]
    for i in range(n):
        A.append(list(map(int,input().split())))

    vis=[[0 for i in range(m)] for j in range(n)]
    B=[[0 for i in range(m)] for j in range(n)]
    for i in range(n):
        for j in range(m):
            B[i][j]=A[i][j]
    for k in range(max(n,m)):
        for i in range(n):
            for j in range(m):
                if (vis[i][j]==0):
                    #print(i,j)
                    flag=0
                    if (i>0 and B[i-1][j]==(A[i][j]+1)):
                        continue

                    if (i<n-1 and B[i+1][j]==(A[i][j]+1)):
                        continue

                    if (j>0 and B[i][j-1]==(A[i][j]+1)):
                        continue

                    if (j<m-1 and B[i][j+1]==(A[i][j]+1)):
                        continue

                    #print("Hello")
                    if (i>0 and B[i-1][j]==A[i][j]):
                        vis[i-1][j]=1
                        B[i][j]=A[i][j]+1
                        flag=1

                    if (i<n-1 and B[i+1][j]==A[i][j]):
                        vis[i+1][j]=1
                        B[i][j]=A[i][j]+1
                        flag=1

                    if (j>0 and B[i][j-1]==A[i][j]):
                        vis[i][j-1]=1
                        B[i][j]=A[i][j]+1
                        flag=1

                    if (j<m-1 and B[i][j+1]==A[i][j]):
                        vis[i][j+1]=1
                        B[i][j]=A[i][j]+1
                        flag=1

    for i in range(n):
        print(*B[i])


t=int(input())
for i in range(t):
    n,m=[int(x) for x in input().split()]
    arr=[[int(x) for x in input().split()] for i in range(n)]

    for i in range(n):
        for j in range(m):
            if((i+j)%2!=arr[i][j]%2):
                arr[i][j]+=1

    for row in arr:
        for i in row:
            print(i,end=" ")
        print()


t=int(input())
while t>0:
    t-=1
    n,m=map(int,input().split())
    a=[]
    for i in range(n):
        b=list(map(int,input().split()))
        a.append(b)
    for i in range(n):
        for j in range(m):
            k=0
            if (i-1)>=0 and (i-1)<n and k==0:
                if a[i-1][j]==a[i][j]:
                    a[i][j]+=1
                    k=1
            if (i+1)>=0 and (i+1)<n and k==0:
                if a[i+1][j]==a[i][j]:
                    a[i][j]+=1
                    k=1
            if (j+1)>=0 and (j+1)<m and k==0:
                if a[i][j+1]==a[i][j]:
                    a[i][j]+=1
                    k=1
            if (j-1)>=0 and (j-1)<m and k==0:
                if a[i][j-1]==a[i][j]:
                    a[i][j]+=1
                    k=1
    for i in range(n):
        print(*a[i])


def inp():
    return(int(input()))
def inlt():
    return(list(map(int,input().split())))
def insr():
    s = input()
    return(list(s[:len(s) ]))
def invr():
    return(map(int,input().split()))

y = inp()

for _ in range(y):
    [r,c] = inlt()
    m = []
    for i in range(r):
        m.append(inlt())
    fixed_pos = []
    while(len(fixed_pos)<r*c):
        #print(fixed_pos)
        min_val = 10000000000
        min_pos = [0,0]
        for i in range(r):
            for j in range(c):
                if(m[i][j] < min_val and (i,j) not in fixed_pos):
                    min_val = m[i][j]
                    min_pos = [i,j]
        fixed_pos.append((min_pos[0],min_pos[1]))
        if(min_pos[0] < r-1):
            if(m[min_pos[0]+1][min_pos[1]] == min_val):
                if((min_pos[0]+1,min_pos[1]) in fixed_pos):
                    m[min_pos[0]][min_pos[1]] += 1
                else:
                    m[min_pos[0]+1][min_pos[1]] += 1
                    fixed_pos.append((min_pos[0]+1,min_pos[1]))
        if(min_pos[1] < c-1):
            if(m[min_pos[0]][min_pos[1]+1] == min_val):
                if((min_pos[0],min_pos[1]+1) in fixed_pos):
                    m[min_pos[0]][min_pos[1]] += 1
                else:
                    m[min_pos[0]][min_pos[1]+1] += 1
                    fixed_pos.append((min_pos[0],min_pos[1]+1))

    for i in range(r):
        ans = ""
        for j in range(c):
            ans += str(m[i][j]) + " "
        print(ans)


def inp():
    return(int(input()))
def inlt():
    return(list(map(int,input().split())))
def insr():
    s = input()
    return(list(s[:len(s) ]))
def invr():
    return(map(int,input().split()))

y = inp()

for _ in range(y):
    [r,c] = inlt()
    m = []
    for i in range(r):
        m.append(inlt())

    #print(m)

    max_val = 0
    fixed_pos = []
    while(len(fixed_pos)<r*c):
        #print(fixed_pos)
        min_val = 10000000000
        min_pos = [0,0]
        for i in range(r):
            for j in range(c):
                if((i,j) in fixed_pos):
                    continue
                if(m[i][j] < min_val):
                    min_val = m[i][j]
                    min_pos = [i,j]
        fixed_pos.append((min_pos[0],min_pos[1]))
        if(min_pos[0] < r-1):
            if(m[min_pos[0]+1][min_pos[1]] == min_val):
                if((min_pos[0]+1,min_pos[1]) in fixed_pos):
                    m[min_pos[0]][min_pos[1]] += 1
                else:
                    m[min_pos[0]+1][min_pos[1]] += 1
                    fixed_pos.append((min_pos[0]+1,min_pos[1]))
        if(min_pos[1] < c-1):
            if(m[min_pos[0]][min_pos[1]+1] == min_val):
                if((min_pos[0],min_pos[1]+1) in fixed_pos):
                    m[min_pos[0]][min_pos[1]] += 1
                else:
                    m[min_pos[0]][min_pos[1]+1] += 1
                    fixed_pos.append((min_pos[0],min_pos[1]+1))

    for i in range(r):
        ans = ""
        for j in range(c):
            ans += str(m[i][j]) + " "
        print(ans)


def inp():
    return(int(input()))
def inlt():
    return(list(map(int,input().split())))
def insr():
    s = input()
    return(list(s[:len(s) ]))
def invr():
    return(map(int,input().split()))

y = inp()

for _ in range(y):
    [r,c] = inlt()
    m = []
    for i in range(r):
        m.append(inlt())
    b = m.copy()
    for i in range(r):
        for j in range(c):
            if(j < c - 1):
                #print(i,j,m[i][j])
                if(m[i][j] == m[i][j+1]):
                    #print("hi")
                    if(i > 0):
                        if(m[i][j+1]+1 != m[i-1][j+1]):
                            m[i][j+1] += 1
                        else:
                            m[i][j] += 1
                    else:
                        m[i][j+1] += 1
            if(i < r - 1):
                #print(i,j,m[i][j])
                if(m[i][j] == m[i+1][j]):
                    if(j > 0):

                        if(m[i+1][j]+1 != m[i+1][j-1]):
                            m[i+1][j] += 1
                        else:
                            m[i][j] += 1
                    else:
                        m[i+1][j] += 1

    #print(m)

    for i in range(r):
        ans = ""
        for j in range(c):
            ans += str(m[i][j]) + " "
        print(ans)


def inp():
    return(int(input()))
def inlt():
    return(list(map(int,input().split())))
def insr():
    s = input()
    return(list(s[:len(s) ]))
def invr():
    return(map(int,input().split()))

y = inp()

for _ in range(y):
    [r,c] = inlt()
    m = []
    for i in range(r):
        m.append(inlt())

    #print(m)

    max_val = 0
    fixed_pos = []
    while(len(fixed_pos)<r*c):
        #print(fixed_pos)
        min_val = 101
        min_pos = [0,0]
        for i in range(r):
            for j in range(c):
                if((i,j) in fixed_pos):
                    continue
                if(m[i][j] < min_val):
                    min_val = m[i][j]
                    min_pos = [i,j]
        fixed_pos.append((min_pos[0],min_pos[1]))
        if(min_pos[0] < r-1):
            if(m[min_pos[0]+1][min_pos[1]] == min_val):
                if((min_pos[0]+1,min_pos[1]) in fixed_pos):
                    m[min_pos[0]][min_pos[1]] += 1
                else:
                    m[min_pos[0]+1][min_pos[1]] += 1
                    fixed_pos.append((min_pos[0]+1,min_pos[1]))
        if(min_pos[1]<c-1):
            if(m[min_pos[0]][min_pos[1]+1] == min_val):
                if((min_pos[0],min_pos[1]+1) in fixed_pos):
                    m[min_pos[0]][min_pos[1]] += 1
                else:
                    m[min_pos[0]][min_pos[1]+1] += 1
                    fixed_pos.append((min_pos[0],min_pos[1]+1))

    #print(m)

    for i in range(r):
        ans = ""
        for j in range(c):
            ans += str(m[i][j]) + " "
        print(ans)


def inp():
    return(int(input()))
def inlt():
    return(list(map(int,input().split())))
def insr():
    s = input()
    return(list(s[:len(s) ]))
def invr():
    return(map(int,input().split()))

y = inp()

for _ in range(y):
    [r,c] = inlt()
    m = []
    for i in range(r):
        m.append(inlt())
    b = m.copy()
    for i in range(r):
        for j in range(c):
            if((i+j) % 2 != m[i][j] % 2 ):
                m[i][j] += 1

    #print(m)

    for i in range(r):
        ans = ""
        for j in range(c):
            ans += str(m[i][j]) + " "
        print(ans)


import sys
try:sys.stdin,sys.stdout=open('in.txt','r'),open('out.txt','w')
except:pass
ii1=lambda:int(sys.stdin.readline().strip()) # for interger
is1=lambda:sys.stdin.readline().strip() # for str
iia=lambda:list(map(int,sys.stdin.readline().strip().split())) # for List[int]
isa=lambda:sys.stdin.readline().strip().split() # for List[str]
mod=int(1e9 + 7);from collections import *;
from itertools import *
###################### Start Here ######################

for _ in range(ii1()):
    n , m = iia()
    arr = []
    for i in range(n):
        arr.append(iia())
    for i in range(n):
        tra = [(0,1),(0,-1),(1,0),(-1,0)]
        for j in range(m):
            adj=[]
            for di,dj in tra:
                if 0<=i+di<n and 0<=j+dj<m:
                    adj.append(arr[i+di][j+dj])
            if arr[i][j] in adj:
                arr[i][j]+=1
    [print(*arr[i]) for i in range(n)]


import sys
try:sys.stdin,sys.stdout=open('in.txt','r'),open('out.txt','w')
except:pass
ii1=lambda:int(sys.stdin.readline().strip()) # for interger
is1=lambda:sys.stdin.readline().strip() # for str
iia=lambda:list(map(int,sys.stdin.readline().strip().split())) # for List[int]
isa=lambda:sys.stdin.readline().strip().split() # for List[str]
mod=int(1e9 + 7);from collections import *;
from itertools import *
###################### Start Here ######################
def main():
    n , m = iia()
    arr = []
    for i in range(n):
        arr.append(iia())
    dp = [[[0,0]]*m for i in range(n)]
    for i in range(n):
        for j in range(m):
            if arr[i][j]%2==(i+j)%2:
                arr[i][j]+=1

    [print(*arr[i]) for i in range(n)]
if __name__=='__main__':
    for i in range(ii1()):
        main()


#include <iostream>

using namespace std;

#define li(n) for(int i=0; i<n; i++)
#define lj(n) for(int j=0; j<n; j++)
#define lk(n) for(int k=0; k<n; k++)

void test();

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);

    int t;
    cin >> t;

    while(t--)
    {
        test();
    }
}

void test()
{
    int n, m;
    cin >> n >> m;

    int a[100][100];
    li(n) lj(m) cin >> a[i][j];

    li(n) lj(m) {
        // i -> row
        // j -> col

        // i+j % 2 == 0 -> 0
        if (a[i][j] != (i+j) % 2) {
            a[i][j]++;
        }
    }

    li(n) {
        lj(m) {
            cout << a[i][j] << " ";
        }
        cout << endl;
    }
}


import sys
import math

# Constants
mod = 998244353

# Functions Settings
dynamic_factorial = False

# Pre-defined function

# Begin

# Testcase handler
def main(test_case = False):
    n = int(input()) if test_case else 1
    for _ in range(n):
        test()
        print()

# Binary method of power
def fast_pow(a:int, b:int):
    res = 1
    while b > 0:
        if b & 1:
            res *= a
        a *= a
        b >>= 1
    return res

# Combination of 2
def c2(n):
    return n * (n-1) // 2

# Get the list form of input
def get():
    return list(map(int, input().split()))

# Number of 1s in binary form
def bits(n: int):
    return list(bin(n)).count('1')

# For interactive problems
def flush():
    sys.stdout.flush()

# Print array and split it with space
def parr(arr):
    print(*arr, sep=' ')

# GCD (int a, int b)
# Return: value of GCD(a, b)
def gcd(a, b):
    while b:
        if b % a == 0:
            break
        tmp = a
        a = b % a
        b = tmp
    return a

# Extended GCD (int a, int b)
# Return: array of [GCD(a, b), [x0, y0]]
# Where a * x0 + b * y0 = 1
# If GCD(a, b) != 1 then return False
def ext_gcd(a: int, b: int):
    if (b == 0):
        return [a, [1, 0]]

    res = ext_gcd(b, a % b)
    g = res[0]
    if g != 1:
        return False
    x1 = res[1][0]
    y1 = res[1][1]
    x = y1
    y = x1 - y1 * (a // b)

    return [g, [x, y]]

# Get all different values of array a with their count
def get_set(a):
    res = {}
    for x in a:
        if x in res:
            res[x] += 1
        else:
            res[x] = 1
    return res

# Factorial
fact = {0: 1}
def factorial(n):
    if dynamic_factorial:
        if n not in fact:
            fact[n] = factorial(n-1) * n
        return fact[n]
    else:
        res = 1
        while n > 0:
            res = (res * n) % mod
            n -= 1
        return res

# Sum of array
def sum_of(a):
    res = 0
    for x in a:
        res = (res + x) % mod
    return res

# Prod of array
def prod_of(a):
    res = 1
    for x in a:
        res = (res * x) % mod
    return res

# Number of different elements
def different_elements(arr):
    ans = 0
    n = len(arr)
    i = 0
    while i < n:
        ans += 1
        while i < n-1 and arr[i] == arr[i+1]:
            i += 1
        i += 1
    return ans
# End

def test():
    n, m = get()
    a = [ get() for i in range(n) ]

    for i in range(n):
        for j in range(m):
            if (i+j) % 2 != a[i][j]:
                a[i][j] += 1

    for i in range(n):
        parr(a[i])

main(test_case = True)


import sys
import math

# Constants
mod = 998244353

# Functions Settings
dynamic_factorial = False

# Pre-defined function

# Begin

# Testcase handler
def main(test_case = False):
    n = int(input()) if test_case else 1
    for _ in range(n):
        test()
        print()

# Binary method of power
def fast_pow(a:int, b:int):
    res = 1
    while b > 0:
        if b & 1:
            res *= a
        a *= a
        b >>= 1
    return res

# Combination of 2
def c2(n):
    return n * (n-1) // 2

# Get the list form of input
def get():
    return list(map(int, input().split()))

# Number of 1s in binary form
def bits(n: int):
    return list(bin(n)).count('1')

# For interactive problems
def flush():
    sys.stdout.flush()

# Print array and split it with space
def parr(arr):
    print(*arr, sep=' ')

# GCD (int a, int b)
# Return: value of GCD(a, b)
def gcd(a, b):
    while b:
        if b % a == 0:
            break
        tmp = a
        a = b % a
        b = tmp
    return a

# Extended GCD (int a, int b)
# Return: array of [GCD(a, b), [x0, y0]]
# Where a * x0 + b * y0 = 1
# If GCD(a, b) != 1 then return False
def ext_gcd(a: int, b: int):
    if (b == 0):
        return [a, [1, 0]]

    res = ext_gcd(b, a % b)
    g = res[0]
    if g != 1:
        return False
    x1 = res[1][0]
    y1 = res[1][1]
    x = y1
    y = x1 - y1 * (a // b)

    return [g, [x, y]]

# Get all different values of array a with their count
def get_set(a):
    res = {}
    for x in a:
        if x in res:
            res[x] += 1
        else:
            res[x] = 1
    return res

# Factorial
fact = {0: 1}
def factorial(n):
    if dynamic_factorial:
        if n not in fact:
            fact[n] = factorial(n-1) * n
        return fact[n]
    else:
        res = 1
        while n > 0:
            res = (res * n) % mod
            n -= 1
        return res

# Sum of array
def sum_of(a):
    res = 0
    for x in a:
        res = (res + x) % mod
    return res

# Prod of array
def prod_of(a):
    res = 1
    for x in a:
        res = (res * x) % mod
    return res

# Number of different elements
def different_elements(arr):
    ans = 0
    n = len(arr)
    i = 0
    while i < n:
        ans += 1
        while i < n-1 and arr[i] == arr[i+1]:
            i += 1
        i += 1
    return ans
# End

def test():
    n, m = get()
    a = [ get() for i in range(n) ]

    for i in range(n):
        for j in range(m):
            if i+j % 2 != a[i][j]:
                a[i][j] += 1

    for i in range(n):
        parr(a[i])

main(test_case = True)


import sys
import math

# Constants
mod = 998244353

# Functions Settings
dynamic_factorial = False

# Pre-defined function

# Begin

# Testcase handler
def main(test_case = False):
    n = int(input()) if test_case else 1
    for _ in range(n):
        test()
        print()

# Binary method of power
def fast_pow(a:int, b:int):
    res = 1
    while b > 0:
        if b & 1:
            res *= a
        a *= a
        b >>= 1
    return res

# Combination of 2
def c2(n):
    return n * (n-1) // 2

# Get the list form of input
def get():
    return list(map(int, input().split()))

# Number of 1s in binary form
def bits(n: int):
    return list(bin(n)).count('1')

# For interactive problems
def flush():
    sys.stdout.flush()

# Print array and split it with space
def parr(arr):
    print(*arr, sep=' ')

# GCD (int a, int b)
# Return: value of GCD(a, b)
def gcd(a, b):
    while b:
        if b % a == 0:
            break
        tmp = a
        a = b % a
        b = tmp
    return a

# Extended GCD (int a, int b)
# Return: array of [GCD(a, b), [x0, y0]]
# Where a * x0 + b * y0 = 1
# If GCD(a, b) != 1 then return False
def ext_gcd(a: int, b: int):
    if (b == 0):
        return [a, [1, 0]]

    res = ext_gcd(b, a % b)
    g = res[0]
    if g != 1:
        return False
    x1 = res[1][0]
    y1 = res[1][1]
    x = y1
    y = x1 - y1 * (a // b)

    return [g, [x, y]]

# Get all different values of array a with their count
def get_set(a):
    res = {}
    for x in a:
        if x in res:
            res[x] += 1
        else:
            res[x] = 1
    return res

# Factorial
fact = {0: 1}
def factorial(n):
    if dynamic_factorial:
        if n not in fact:
            fact[n] = factorial(n-1) * n
        return fact[n]
    else:
        res = 1
        while n > 0:
            res = (res * n) % mod
            n -= 1
        return res

# Sum of array
def sum_of(a):
    res = 0
    for x in a:
        res = (res + x) % mod
    return res

# Prod of array
def prod_of(a):
    res = 1
    for x in a:
        res = (res * x) % mod
    return res

# Number of different elements
def different_elements(arr):
    ans = 0
    n = len(arr)
    i = 0
    while i < n:
        ans += 1
        while i < n-1 and arr[i] == arr[i+1]:
            i += 1
        i += 1
    return ans
# End

def test():
    n, m = get()
    a = [ get() for i in range(n) ]

    for i in range(n):
        for j in range(m):
            if (i+j) % 2 != a[i][j] % 2:
                a[i][j] += 1

    for i in range(n):
        parr(a[i])

main(test_case = True)


from sys import stdin, stdout
from math import factorial as f
from random import randint, shuffle
# input = stdin.readline

for _ in range(int(input())):
    n, m = map(int,input().split())
    a = [[*map(int, input().split())] for _ in range(n)]
    for i in range(n):
        for j in range(m):
            if ((i + j) & 1) ^ (a[i][j] & 1):
                a[i][j] += 1
    for i in a:
        print(*i)


for _ in range(int(input())):
    n,m=map(int,input().split())
    mat=[[0]*m for _ in range(n)]
    for i in range(n):
        mat[i]=list(map(int,input().split()))
    for i in range(n):
        for j in range(m):
            if i==0 and j!=0:
                if mat[i][j]==mat[i][j-1]:
                    mat[i][j]+=1
                    continue
            if j==0 and i!=0:
                if mat[i-1][j]==mat[i][j]:
                    mat[i][j]+=1
                    continue
            if mat[i][j]==mat[i-1][j] or mat[i][j]==mat[i][j-1]:
                mat[i][j]+=1
            if mat[i][j]==mat[i-1][j] or mat[i][j]==mat[i][j-1]:
                mat[i][j]+=1
    for i in range(n):
        for j in range(m):
            print(mat[i][j],end=' ')
        print('')


for _ in range(int(input())):
    n,m=map(int,input().split())
    mat=[[0]*m for _ in range(n)]
    for i in range(n):
        mat[i]=list(map(int,input().split()))
    big=0
    for i in range(n):
        for j in range(m):
            big=max(big,mat[i][j])
    for i in range(n):
        for j in range(m):
            print(big,end=' ')
            big+=1
        print('')


for _ in range(int(input())):
    n,m=map(int,input().split())
    mat=[[0]*m for _ in range(n)]
    for i in range(n):
        mat[i]=list(map(int,input().split()))
    for i in range(n):
        for j in range(m):
            if (i+j)%2==mat[i][j]%2:
                continue
            else:
                mat[i][j]+=1
    for i in range(n):
        for j in range(m):
            print(mat[i][j],end=' ')
        print('')


t = int(input())
for _ in range(t):
    x, y = [int(i) for i in input().split()]
    l = []
    for __ in range(x):
        l.append([int(i) for i in input().split()])
    for i in range(x):
        for j in range(y):
            if (i + j) % 2 == 0:
                if l[i][j] % 2 == 1:
                    l[i][j] += 1
            elif l[i][j] % 2 == 0:
                l[i][j] += 1
    for i in range(x):
        for j in range(y):
            print(l[i][j], end=" ")
        print()


import math, sys
from collections import defaultdict, Counter, deque
from functools import reduce

INF = float('inf')
MOD = int(1e9) + 7

def dfs(i, j, mat, visited):
	if 0 <= i < len(visited) and 0 <= j < len(visited[0]) and not visited[i][j]:

		visited[i][j] = 1

		adj = [
			(i - 1, j),
			(i, j + 1),
			(i + 1, j),
			(i, j - 1)
		]

		for x, y in adj:
			if 0 <= x < len(mat) and 0 <= y < len(mat[0]) and mat[x][y] == mat[i][j]:
				mat[x][y] += 1
				dfs(x, y, mat, visited)

	return

def solve():
	n, m = vars()

	mat = []

	for i in range(n):
		mat.append(array())

	visited = [[ 0 for i in range(n)] for j in range(m) ]

	for i in range(n):
		for j in range(m):
			dfs(i, j, mat, visited)

	for row in mat:
		print(*row)
	# print()

def main():
	t = 1
	t = int(input())
	for _ in range(t):
		solve()

def gcd(a, b):
	while b:
		a, b = b, a % b
	return a

def input():
	return sys.stdin.readline().rstrip('\n').strip()

def print(*args, sep=' ', end='\n'):
	first = True
	for arg in args:
		if not first:
			sys.stdout.write(str(sep))
		sys.stdout.write(str(arg))
		first = False

	sys.stdout.write(end)

def vars():
	return map(int, input().split())

def array():
	return list(map(int, input().split()))

if __name__ == '__main__':

	main()


import math, sys
from collections import defaultdict, Counter, deque
from functools import reduce

INF = float('inf')
MOD = int(1e9) + 7

def solve():
	n, m = vars()

	mat = []

	for i in range(n):
		mat.append(array())

	for i in range(n):
		for j in range(m):
			if (i + j) % 2 != mat[i][j] % 2:
				mat[i][j] += 1

	for row in mat:
		print(*row)
	# print()

def main():
	t = 1
	t = int(input())
	for _ in range(t):
		solve()

def gcd(a, b):
	while b:
		a, b = b, a % b
	return a

def input():
	return sys.stdin.readline().rstrip('\n').strip()

def print(*args, sep=' ', end='\n'):
	first = True
	for arg in args:
		if not first:
			sys.stdout.write(str(sep))
		sys.stdout.write(str(arg))
		first = False

	sys.stdout.write(end)

def vars():
	return map(int, input().split())

def array():
	return list(map(int, input().split()))

if __name__ == '__main__':

	main()


import math, sys
from collections import defaultdict, Counter, deque
from functools import reduce

INF = float('inf')
MOD = int(1e9) + 7

def dfs(i, j, mat, visited):
	if 0 <= i < len(visited) and 0 <= j < len(visited[0]) and not visited[i][j]:

		visited[i][j] = 1

		adj = [
			(i - 1, j - 1),
			(i - 1, j),
			(i - 1, j + 1),
			(i, j + 1),
			(i + 1, j + 1),
			(i + 1, j),
			(i + 1, j - 1),
			(i, j - 1)
		]

		for x, y in adj:
			if 0 <= x < len(mat) and 0 <= y < len(mat[0]) and mat[x][y] == mat[i][j]:
				mat[x][y] += 1
				dfs(x, y, mat, visited)

	return

def solve():
	n, m = vars()

	mat = []

	for i in range(n):
		mat.append(array())

	visited = [[ 0 for i in range(n)] for j in range(m) ]

	for i in range(n):
		for j in range(m):
			dfs(i, j, mat, visited)

	for row in mat:
		print(*row)
	# print()

def main():
	t = 1
	t = int(input())
	for _ in range(t):
		solve()

def gcd(a, b):
	while b:
		a, b = b, a % b
	return a

def input():
	return sys.stdin.readline().rstrip('\n').strip()

def print(*args, sep=' ', end='\n'):
	first = True
	for arg in args:
		if not first:
			sys.stdout.write(str(sep))
		sys.stdout.write(str(arg))
		first = False

	sys.stdout.write(end)

def vars():
	return map(int, input().split())

def array():
	return list(map(int, input().split()))

if __name__ == '__main__':

	main()


import io
import os

def solve(R, C, grid):
    for r in range(R):
        for c in range(C):
            if grid[r][c] % 2 == (r + c) % 2:
                grid[r][c] += 1
    return "\n".join(" ".join(map(str, row)) for row in grid)

if __name__ == "__main__":
    input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

    TC = int(input())
    for tc in range(1, TC + 1):
        N, M = [int(x) for x in input().split()]
        grid = [[int(x) for x in input().split()] for i in range(N)]

        ans = solve(N, M, grid)
        print(ans)


import io
import os

from collections import Counter, defaultdict, deque

from heapq import heappush, heappop, heapify

def solve(R, C, grid):
    if False:
        print(R, C)
        for row in grid:
            print(row)

    drdc = [(-1, 0), (0, -1), (0, 1), (1, 0)]
    heap = []
    for r in range(R):
        for c in range(C):
            heappush(heap, (-grid[r][c], r + c, r, c))

    while heap:
        neg, _, r, c = heappop(heap)
        bad = False
        for dr, dc in drdc:
            nr = r + dr
            nc = c + dc
            if 0 <= nr < R and 0 <= nc < C:
                if grid[nr][nc] == grid[r][c] + 1:
                    bad = True
                    break
        if not bad:
            grid[r][c] += 1
    return "\n".join(" ".join(map(str, row)) for row in grid)

if __name__ == "__main__":
    input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

    TC = int(input())
    for tc in range(1, TC + 1):
        N, M = [int(x) for x in input().split()]
        grid = [[int(x) for x in input().split()] for i in range(N)]

        ans = solve(N, M, grid)
        print(ans)


T = int(input())
for _ in range(T):
    n, m = map(int, input().split())
    ls = [list(map(int, input().split())) for _ in range(n)]
    for i in range(n):
        for j in range(m):
            if (i+j)%2 == 0:
                if ls[i][j]%2: ls[i][j] += 1
            else:
                if ls[i][j]%2 == 0: ls[i][j] += 1

    for i in range(n):
        print(*ls[i])


import sys
ii = lambda: sys.stdin.readline().strip()
idata = lambda: [int(qw) for qw in ii().split()]

def solve():
    n, m = idata()
    for i in range(n):
        data = idata()
        if i % 2:
            for j in range(m):
                if j % 2:
                    data[j] = (data[j] + 1) // 2 * 2
                else:
                    data[j] = data[j] // 2 * 2 + 1
        else:
            for j in range(m):
                if j % 2 == 0:
                    data[j] = (data[j] + 1) // 2 * 2
                else:
                    data[j] = data[j] // 2 * 2 + 1
        print(*data)
    return

for _t in range(int(ii())):
    solve()


#include <iostream>
#include <vector>
#include <queue>
#include <cstdio>
#include <ctime>
#include <cstdlib>
#include <cmath>
#include <algorithm>

using namespace std;
typedef unsigned long long LL;

const int maxn=3e5+2;
const LL moder=998244353;
long N;
vector<int>vec[maxn];

int carr[maxn];
bool connected[maxn];
int lastsearch_pos=0;
int rednum,blacknum;
LL black_pow_store[33];
LL red_pow_store[33];
LL red30_remain;
LL black30_remain;

LL colorer(int stpos,int color)
{
    if(carr[stpos]==0)
    {
        carr[stpos]=color;
        if(color==1)
            ++rednum;
        else ++blacknum;
    }
    else
    {
        if(color!=carr[stpos])//wrong paint
            return 0;

        return 1;
    }
    LL res;
    for(vector<int>::iterator it=vec[stpos].begin();it!=vec[stpos].end();it++)
    {
        res=colorer(*it,-color);
        if(res==0)
            return 0;
    }
    return 1;
}

void flush_unit(int i)
{
    connected[i]=false;
    carr[i]=0;
    if(!vec[i].empty())
        vec[i].clear();
}
int search_uncolor_pos()
{
    for(int i=lastsearch_pos;i<maxn;i++)
    {
        if(connected[i]==true&&carr[i]==0)
        {
            lastsearch_pos=i;
            return i;
        }
    }
    return -1;
}
LL store_res(LL num,int mode)
{
    LL res=1;
    if(mode==2)
    {
        int red_remain=num%30;
        LL red_moder=num/30;
        for(int i=0;i<red_moder;i++)
        {
            res*=red30_remain;
            res%=moder;
        }
        res*=red_pow_store[red_remain];
        res%=moder;
    }
    if(mode==3)
    {
        int black_remain=num%28;
        LL black_moder=num/28;
        for(int i=0;i<black_moder;i++)
        {
            res*=black30_remain;
            res%=moder;
        }
        res*=black_pow_store[black_remain];
        res%=moder;
    }

    return res;
}
int main()
{
    int testnum;
    cin>>testnum;
    red_pow_store[0]=1;
    black_pow_store[0]=1;
    for(int i=1;i<=30;i++)
    {
        red_pow_store[i]=red_pow_store[i-1]<<1;
        black_pow_store[i]=black_pow_store[i-1]*3;
    }
    red30_remain=red_pow_store[30]%moder;
    black30_remain=black_pow_store[28]%moder;

    for(int tester=0;tester<testnum;tester++)
    {
        int verticenum;
        int edgenum;
        LL connectnum=0;
        int fi=-1,se=0;

        scanf("%d%d",&verticenum,&edgenum);
        lastsearch_pos=0;

        for(int i=0;i<maxn;i++)
            flush_unit(i);

        for(int i=0;i<edgenum;i++)
        {
            scanf("%d%d",&fi,&se);
            if(connected[fi]==false)
            {
                connected[fi]=true;
                connectnum++;
            }
            if(connected[se]==false)
            {
                connected[se]=true;
                connectnum++;
            }
            vec[fi].push_back(se);
            vec[se].push_back(fi);
        }
        LL newres=1;
        LL cores=1;
        LL subres=1;
        while (fi!=-1&&cores!=0) {

            rednum=0;
            blacknum=0;
            cores=colorer(fi, 1);
            if(cores!=0)
            {
                subres=store_res(rednum, 2);
                subres+=store_res(blacknum, 2);
                subres%=moder;
            }
            else{
                newres=0;
                break;
            }
            newres*=subres;
            newres%=moder;

            fi=search_uncolor_pos();
            if(fi==-1)
                break;
        }
        if(cores==0)
        {
            cout<<cores<<endl;
            continue;
        }

        newres*=store_res(verticenum-connectnum, 3);
        newres%=moder;
        cout<<newres<<endl;
    }
    return 0;
}
/*
 12
 8 7
 2 3
 3 4
 4 5
 5 2
 6 7
 7 8
 8 6
 1 0
 2 1
 1 2
 3 3
 1 2
 2 3
 1 3
 3 2
 2 3
 3 1
 4 4
 1 2
 2 3
 3 4
 4 1
 4 4
 1 2
 2 3
 3 1
 4 1
 6 9
 1 4
 1 5
 1 6
 2 4
 2 5
 2 6
 3 4
 3 5
 3 6
 100000 0
 1000 5
 55 56
 56 57
 57 58
 58 59
 59 55
 11 11
 1 2
 2 3
 3 4
 4 5
 5 6
 6 7
 7 8
 8 9
 9 10
 10 11
 11 1
 10 12
 1 2
 2 3
 3 4
 4 1
 1 5
 5 6
 6 7
 7 1
 1 8
 8 9
 9 10
 10 1

 */


#include <iostream>
#include <vector>
#include <queue>
#include <cstdio>
#include <ctime>
#include <cstdlib>
#include <cmath>
#include <algorithm>

using namespace std;
typedef long long LL;

const int maxn=3e5+12;
const LL moder=998244353;
long N;
vector<int>vec[maxn];
vector<pair<int,int>>edge;
int carr[maxn];
bool connected[maxn];
int lastsearch_pos=0;
int rednum,blacknum;
LL black_pow_store[33];
LL red_pow_store[33];
LL red30_remain;
LL black30_remain;

LL colorer(int stpos,int color)
{
    if(carr[stpos]==0)
    {
        carr[stpos]=color;
        if(color==1)
            ++blacknum;
        else ++rednum;
    }
    else
    {
        return 1;
    }
    for(int i=0;i<vec[stpos].size();i++)
       colorer(vec[stpos][i],-color);

    return 1;
}
int valid_map()
{
    while (!edge.empty()) {
        if(carr[edge.back().first]==carr[edge.back().second])
            return 0;
        edge.pop_back();
    }
    return 1;
}
void flush_unit(int i)
{
    connected[i]=false;
    carr[i]=0;
    if(!vec[i].empty())
    vec[i].clear();
}
int search_uncolor_pos()
{
    for(int i=lastsearch_pos;i<maxn;i++)
    {
        if(connected[i]==true&&carr[i]==0)
        {
            lastsearch_pos=i+1;
            return i;
        }
    }
    return 0;
}
LL store_res(int num,int mode)
{
    LL res=1;
    if(mode==2)
    {
        int red_remain=num%30;
        int red_moder=num/30;
        for(int i=0;i<red_moder;i++)
        {
            res*=red30_remain;
            res%=moder;
        }
        res*=red_pow_store[red_remain];
        res%=moder;
    }
    if(mode==3)
    {
        int black_remain=num%30;
        int black_moder=num/30;
        for(int i=0;i<black_moder;i++)
        {
            res*=black30_remain;
            res%=moder;
        }
        res*=black_pow_store[black_remain];
        res%=moder;
    }

    return res;
}
int main()
{
    int testnum;
    cin>>testnum;
    red_pow_store[0]=1;
    black_pow_store[0]=1;
    for(int i=1;i<=30;i++)
    {
        red_pow_store[i]=red_pow_store[i-1]<<1;
        black_pow_store[i]=black_pow_store[i-1]*3;
    }
    red30_remain=red_pow_store[30]%moder;
    black30_remain=black_pow_store[30]%moder;

    for(int tester=0;tester<testnum;tester++)
    {
        int verticenum;
        int edgenum;
        int connectnum=0;
        int fi=0,se=0;

        scanf("%d%d",&verticenum,&edgenum);
        lastsearch_pos=0;
        edge.clear();
        for(int i=0;i<maxn;i++)
            flush_unit(i);

        for(int i=0;i<edgenum;i++)
        {
            scanf("%d%d",&fi,&se);
            edge.push_back(make_pair(fi, se));
            if(connected[fi]==false)
            {
                connected[fi]=true;
                connectnum++;
            }
            if(connected[se]==false)
            {
                connected[se]=true;
                connectnum++;
            }
            vec[fi].push_back(se);
            vec[se].push_back(fi);
        }
        LL newres=1;
        LL cores=1;
        LL subres=1;
            while (fi!=0&&cores!=0) {
                fi=search_uncolor_pos();
                if(fi==0)
                    break;

                rednum=0;
                blacknum=0;
                cores=colorer(fi, 1);
                if(valid_map()==1)
                {
                    subres=store_res(rednum, 2);
                    subres+=store_res(blacknum, 2);
                    subres%=moder;
                }
                else{
                    cores=0;
                    break;
                }
                newres*=subres;
                newres%=moder;
            }
            if(cores==0)
            {
                cout<<cores<<endl;
                continue;
            }

        newres*=store_res(verticenum-connectnum, 3);
        newres%=moder;

        cout<<newres<<endl;
    }
    return 0;
}
/*

 */


#include <iostream>
#include <vector>
#include <queue>
#include <cstdio>
#include <ctime>
#include <cstdlib>
#include <cmath>
#include <algorithm>

using namespace std;
typedef long long LL;

const int maxn=3e5+2;
const LL moder=998244353;
long N;
vector<int>vec[maxn];
int carr[maxn];
bool connected[maxn];
int lastsearch_pos=0;
int rednum,blacknum;
LL black_pow_store[33];
LL red_pow_store[33];
LL red30_remain;
LL black30_remain;

LL colorer(int stpos,int color)
{
    if(carr[stpos]==0)
    {
        carr[stpos]=color;
        if(color==1)
            ++blacknum;
        else ++rednum;
    }
    else
    {
        if(color!=carr[stpos])//wrong paint
            return 0;

        return 1;
    }
    LL res;
    for(int i=0;i<vec[stpos].size();i++)
    {
       res=colorer(vec[stpos][i],-color);
       if(res==0)
           return 0;
    }
    return 1;
}

void flush_unit(int i)
{
    connected[i]=false;
    carr[i]=0;
    if(!vec[i].empty())
    vec[i].clear();
}
int search_uncolor_pos()
{
    for(int i=lastsearch_pos;i<maxn;i++)
    {
        if(connected[i]==true&&carr[i]==0)
        {
            lastsearch_pos=i;
            return i;
        }
    }
    return -1;
}
LL store_res(LL num,int mode)
{
    LL res=1;
    if(mode==2)
    {
        int red_remain=num%30;
        LL red_moder=num/30;
        for(int i=0;i<red_moder;i++)
        {
            res*=red30_remain;
            res%=moder;
        }
        res*=red_pow_store[red_remain];
        res%=moder;
    }
    if(mode==3)
    {
        int black_remain=num%30;
        LL black_moder=num/30;
        for(int i=0;i<black_moder;i++)
        {
            res*=black30_remain;
            res%=moder;
        }
        res*=black_pow_store[black_remain];
        res%=moder;
    }

    return res;
}
int main()
{
    int testnum;
    cin>>testnum;
    red_pow_store[0]=1;
    black_pow_store[0]=1;
    for(int i=1;i<=30;i++)
    {
        red_pow_store[i]=red_pow_store[i-1]<<1;
        black_pow_store[i]=black_pow_store[i-1]*3;
    }
    red30_remain=red_pow_store[30]%moder;
    black30_remain=black_pow_store[30]%moder;

    for(int tester=0;tester<testnum;tester++)
    {
        int verticenum;
        int edgenum;
        LL connectnum=0;
        int fi=-1,se=0;

        scanf("%d%d",&verticenum,&edgenum);
        lastsearch_pos=0;

        for(int i=0;i<maxn;i++)
            flush_unit(i);

        for(int i=0;i<edgenum;i++)
        {
            scanf("%d%d",&fi,&se);
            if(connected[fi]==false)
            {
                connected[fi]=true;
                connectnum++;
            }
            if(connected[se]==false)
            {
                connected[se]=true;
                connectnum++;
            }
            vec[fi].push_back(se);
            vec[se].push_back(fi);
        }
        LL newres=1;
        LL cores=1;
        LL subres=1;
            while (fi!=-1&&cores!=0) {

                rednum=0;
                blacknum=0;
                cores=colorer(fi, 1);
                if(cores!=0)
                {
                    subres=store_res(rednum, 2);
                    subres+=store_res(blacknum, 2);
                    subres%=moder;
                }
                else{
                    newres=0;
                    break;
                }
                newres*=subres;
                newres%=moder;

                fi=search_uncolor_pos();
                if(fi==-1)
                    break;
            }
            if(cores==0)
            {
                cout<<cores<<endl;
                continue;
            }

        newres*=store_res(verticenum-connectnum, 3);
        newres%=moder;
        cout<<newres<<endl;
    }
    return 0;
}
/*
 12
 8 7
 2 3
 3 4
 4 5
 5 2
 6 7
 7 8
 8 6
 1 0
 2 1
 1 2
 3 3
 1 2
 2 3
 1 3
 3 2
 2 3
 3 1
 4 4
 1 2
 2 3
 3 4
 4 1
 4 4
 1 2
 2 3
 3 1
 4 1
 6 9
 1 4
 1 5
 1 6
 2 4
 2 5
 2 6
 3 4
 3 5
 3 6
 100000 0
 1000 5
 55 56
 56 57
 57 58
 58 59
 59 55
 11 11
 1 2
 2 3
 3 4
 4 5
 5 6
 6 7
 7 8
 8 9
 9 10
 10 11
 11 1
 10 12
 1 2
 2 3
 3 4
 4 1
 1 5
 5 6
 6 7
 7 1
 1 8
 8 9
 9 10
 10 1

 */


#include <iostream>
#include <vector>
#include <queue>
#include <cstdio>
#include <ctime>
#include <cstdlib>
#include <cmath>
#include <algorithm>

using namespace std;
typedef long long LL;

const int maxn=3e5+2;
const LL moder=998244353;
long N;
vector<int>vec[maxn];
vector<int>meet;
int carr[maxn];
bool connected[maxn];
int lastsearch_pos=0;
int rednum,blacknum;
LL black_pow_store[33];
LL red_pow_store[33];
LL red30_remain;
LL black30_remain;

LL colorer(int stpos,int color)
{
    if(carr[stpos]==0)
    {
        meet.push_back(stpos);
        carr[stpos]=color;
        if(color==1)
            ++blacknum;
        else ++rednum;
    }
    else
    {
        if(color!=carr[stpos])//wrong paint
            return 0;

        return 1;
    }
    LL res;
    for(int i=0;i<vec[stpos].size();i++)
    {
        res=colorer(vec[stpos][i],-color);
        if(res==0)
            return 0;
    }
    return 1;
}

void flush_unit(int i)
{
    connected[i]=false;
    carr[i]=0;
    if(!vec[i].empty())
        vec[i].clear();
}
int search_uncolor_pos()
{
    for(int i=lastsearch_pos;i<maxn;i++)
    {
        if(connected[i]==true&&carr[i]==0)
        {
            lastsearch_pos=i;
            return i;
        }
    }
    return -1;
}
LL store_res(LL num,int mode)
{
    LL res=1;
    if(mode==2)
    {
        int red_remain=num%30;
        LL red_moder=num/30;
        for(int i=0;i<red_moder;i++)
        {
            res*=red30_remain;
            res%=moder;
        }
        res*=red_pow_store[red_remain];
        res%=moder;
    }
    if(mode==3)
    {
        int black_remain=num%30;
        LL black_moder=num/30;
        for(int i=0;i<black_moder;i++)
        {
            res*=black30_remain;
            res%=moder;
        }
        res*=black_pow_store[black_remain];
        res%=moder;
    }

    return res;
}
int main()
{
    int testnum;
    cin>>testnum;
    red_pow_store[0]=1;
    black_pow_store[0]=1;
    for(int i=1;i<=30;i++)
    {
        red_pow_store[i]=red_pow_store[i-1]<<1;
        black_pow_store[i]=black_pow_store[i-1]*3;
    }
    red30_remain=red_pow_store[30]%moder;
    black30_remain=black_pow_store[30]%moder;

    for(int tester=0;tester<testnum;tester++)
    {
        int verticenum;
        int edgenum;
        LL connectnum=0;
        int fi=-1,se=0;

        scanf("%d%d",&verticenum,&edgenum);
        lastsearch_pos=0;

        for(int i=0;i<maxn;i++)
            flush_unit(i);

        for(int i=0;i<edgenum;i++)
        {
            scanf("%d%d",&fi,&se);
            if(connected[fi]==false)
            {
                connected[fi]=true;
                connectnum++;
            }
            if(connected[se]==false)
            {
                connected[se]=true;
                connectnum++;
            }
            vec[fi].push_back(se);
            vec[se].push_back(fi);
        }
        LL newres=1;
        LL cores=1;
        LL subres=1;
        while (fi!=-1&&cores!=0) {

            meet.clear();
            rednum=0;
            blacknum=0;
            cores=colorer(fi, 1);
            if(cores!=0)
            {
                subres=store_res(rednum, 2);
                while (!meet.empty()) {
                    carr[meet.back()]=0;
                    meet.pop_back();
                }
                rednum=0;
                blacknum=0;
                cores=colorer(fi, -1);
                if(cores==0)
                {
                    newres=0;
                }
                subres+=store_res(rednum, 2);
                subres%=moder;
            }
            else{
                newres=0;
                break;
            }
            newres*=subres;
            newres%=moder;

            fi=search_uncolor_pos();
            if(fi==-1)
                break;
        }
        if(cores==0)
        {
            cout<<cores<<endl;
            continue;
        }

        newres*=store_res(verticenum-connectnum, 3);
        newres%=moder;
        cout<<newres<<endl;
    }
    return 0;
}
/*
 12
 8 7
 2 3
 3 4
 4 5
 5 2
 6 7
 7 8
 8 6
 1 0
 2 1
 1 2
 3 3
 1 2
 2 3
 1 3
 3 2
 2 3
 3 1
 4 4
 1 2
 2 3
 3 4
 4 1
 4 4
 1 2
 2 3
 3 1
 4 1
 6 9
 1 4
 1 5
 1 6
 2 4
 2 5
 2 6
 3 4
 3 5
 3 6
 100000 0
 1000 5
 55 56
 56 57
 57 58
 58 59
 59 55
 11 11
 1 2
 2 3
 3 4
 4 5
 5 6
 6 7
 7 8
 8 9
 9 10
 10 11
 11 1
 10 12
 1 2
 2 3
 3 4
 4 1
 1 5
 5 6
 6 7
 7 1
 1 8
 8 9
 9 10
 10 1

 */


#include <iostream>
#include <vector>
#include <queue>
#include <cstdio>
#include <ctime>
#include <cstdlib>
#include <cmath>
#include <algorithm>

using namespace std;
typedef long long LL;
/*
 1
 8 9
 1 2
 2 3
 3 4
 4 5
 5 6
 6 7
 7 8
 3 5
 2 4

 */

const int maxn=3e5+12;
const LL moder=998244353;
long N;

vector<int>vec[maxn];
int carr[maxn];
bool connected[maxn];
LL colorer(int stpos,int color)
{
    if(carr[stpos]==0)
    {
        carr[stpos]=color;
        if(color==1)
        for(int p=0;p<vec[stpos].size();p++)
        {
           if(carr[vec[stpos][p]]==1)
               return 0;//fail
        }
            if(color==-1)
            for(int p=0;p<vec[stpos].size();p++)
            {
                if(carr[vec[stpos][p]]==-1)
                    return 0;//fail
            }
    }
    else
    {
        return 1;
    }
    LL res=1;
    if(color<0)
        res=2;

    for(int i=0;i<vec[stpos].size();i++)
    {
       res*=colorer(vec[stpos][i],-color);
    }
    return res%moder;
}

void initial()
{
    for(int i=0;i<maxn;i++)
    {
        connected[i]=false;
        carr[i]=0;
        if(vec[i].empty())
            continue;
        vec[i].clear();
    }
}
int main()
{
    int testnum;
    cin>>testnum;
    for(int tester=0;tester<testnum;tester++)
    {
        initial();
        int verticenum;
        int edgenum;
        int connectnum=0;
        int fi=0,se=0;
        scanf("%d%d",&verticenum,&edgenum);
        for(int i=0;i<edgenum;i++)
        {
            scanf("%d%d",&fi,&se);
            if(connected[fi]==false)
            {
                connected[fi]=true;
                connectnum++;
            }
            if(connected[se]==false)
            {
                connected[se]=true;
                connectnum++;
            }
            vec[fi].push_back(se);
            vec[se].push_back(fi);
        }
        LL res=colorer(fi, 1);
        LL unconnect=1;
        for(int i=0;i<maxn;i++)
            carr[i]=0;
        res+=colorer(fi, -1);

        if(connected[fi]==false)
            res=1;

        for(int u=0;u<verticenum-connectnum;u++)
        {
            unconnect*=3;
            if(u%20==0)
                unconnect=unconnect%moder;
        }

        unconnect%=moder;
        res%=moder;
        res*=unconnect;
        res%=moder;

        cout<<res<<endl;
    }
    return 0;
}
/*

 */


#include <iostream>
#include <vector>
#include <queue>
#include <cstdio>
#include <ctime>
#include <cstdlib>
#include <cmath>
#include <algorithm>

using namespace std;
typedef long long LL;
/*

 */

const int maxn=3e5+12;
const LL moder=998244353;
long N;

vector<int>vec[maxn];
vector<int>meet;
vector<int>lastuse;
int carr[maxn];
bool connected[maxn];
int history[maxn];
int paintnum=0;
int lastsearch_pos=0;
int border=0;
int rednum,blacknum;
LL black_pow_store[33];
LL red_pow_store[33];
LL red30_remain;
LL black30_remain;

LL colorer(int stpos,int color)
{
    if(carr[stpos]==0)
    {
        meet.push_back(stpos);
        carr[stpos]=color;
        history[stpos]=1;
        if(color==1)
        {
            rednum++;
            for(int p=0;p<vec[stpos].size();p++)
            {
                if(carr[vec[stpos][p]]==1)
                    return 0;//fail
            }
        }

            if(color==-1)
            {
                blacknum++;
                for(int p=0;p<vec[stpos].size();p++)
                {
                    if(carr[vec[stpos][p]]==-1)
                        return 0;//fail
                }
            }

    }
    else
    {
        return 1;
    }
    LL res=1;
    if(color<0)
        res=2;

    for(int i=0;i<vec[stpos].size();i++)
    {
       if(carr[vec[stpos][i]]==0)
       res*=colorer(vec[stpos][i],-color);

        if(res==0)
            return 0;
        if(i%20==19)
            res%=moder;
    }
    return res%moder;
}
void flush_unit(int i)
{
    connected[i]=false;
    history[i]=0;
    if(!vec[i].empty())
    vec[i].clear();
}
int search_uncolor_pos()
{
    for(int i=lastsearch_pos;i<maxn;i++)
    {
        if(connected[i]==true&&history[i]==0)
        {
            lastsearch_pos=i+1;
            return i;
        }
    }
    return 0;
}
LL store_res(int num,int mode)
{
    LL res=1;
    if(mode==2)
    {
        int red_remain=num%30;
        int red_moder=num/30;
        for(int i=0;i<red_moder;i++)
        {
            res*=red30_remain;
            res%=moder;
        }
        res*=red_pow_store[red_remain];
        res%=moder;
    }
    if(mode==3)
    {
        int black_remain=num%30;
        int black_moder=num/30;
        for(int i=0;i<black_moder;i++)
        {
            res*=black30_remain;
            res%=moder;
        }
        res*=black_pow_store[black_remain];
        res%=moder;
    }

    return res;
}
int main()
{
    int testnum;
    cin>>testnum;
    red_pow_store[0]=1;
    black_pow_store[0]=1;
    for(int i=1;i<=30;i++)
    {
        red_pow_store[i]=red_pow_store[i-1]<<1;
        black_pow_store[i]=black_pow_store[i-1]*3;
    }
    red30_remain=red_pow_store[30]%moder;
    black30_remain=black_pow_store[30]%moder;

    for(int tester=0;tester<testnum;tester++)
    {

        int verticenum;
        int edgenum;
        int connectnum=0;
        int fi=0,se=0;
        int orfi;
        scanf("%d%d",&verticenum,&edgenum);
        lastsearch_pos=0;
        for(int i=0;i<maxn;i++)
            flush_unit(i);

        border=0;
        for(int i=0;i<edgenum;i++)
        {
            scanf("%d%d",&fi,&se);

            if(connected[fi]==false)
            {
                connected[fi]=true;
                connectnum++;
            }
            if(connected[se]==false)
            {
                connected[se]=true;
                connectnum++;
            }
            vec[fi].push_back(se);
            vec[se].push_back(fi);
        }
        orfi=fi;
        LL newres=1;
        LL cores=1;

            while (fi!=0&&cores!=0) {
                fi=search_uncolor_pos();
                rednum=0;
                blacknum=0;
                if(fi==0)
                    break;
                while (!meet.empty()) {
                    carr[meet.back()]=0;
                    meet.pop_back();
                }
                cores=colorer(fi, 1);
                if(cores!=0)
                {
                    newres=store_res(rednum, 2);
                    newres%=moder;
                    newres+=store_res(blacknum, 2);
                    newres%=moder;
                }
            }
            if(cores==0)
            {
                cout<<cores<<endl;
                continue;
            }
        newres*=store_res(verticenum-connectnum, 3);
        newres%=moder;

        cout<<newres<<endl;
    }
    return 0;
}
/*

 */


#include <iostream>
#include <vector>
#include <queue>
#include <cstdio>
#include <ctime>
#include <cstdlib>
#include <cmath>
#include <algorithm>

using namespace std;
typedef long long LL;
/*

 */

const int maxn=3e5+12;
const LL moder=998244353;
long N;

vector<int>vec[maxn];
int carr[maxn];
bool connected[maxn];
int paintnum=0;
LL colorer(int stpos,int color)
{
    if(carr[stpos]==0)
    {
        carr[stpos]=color;
        paintnum++;
        if(color==1)
        for(int p=0;p<vec[stpos].size();p++)
        {
           if(carr[vec[stpos][p]]==1)
               return 0;//fail
        }
            if(color==-1)
            for(int p=0;p<vec[stpos].size();p++)
            {
                if(carr[vec[stpos][p]]==-1)
                    return 0;//fail
            }
    }
    else
    {
        return 1;
    }
    LL res=1;
    if(color<0)
        res=2;

    for(int i=0;i<vec[stpos].size();i++)
    {
       res*=colorer(vec[stpos][i],-color);
    }
    return res%moder;
}

void initial()
{
    paintnum=0;
    for(int i=0;i<maxn;i++)
    {
        connected[i]=false;
        carr[i]=0;
        if(vec[i].empty())
            continue;
        vec[i].clear();
    }
}
int search_uncolor_pos()
{
    for(int i=0;i<maxn;i++)
    {
        if(connected[i]==true&&carr[i]==0)
            return i;
    }
    return 0;
}
int main()
{
    int testnum;
    cin>>testnum;
    for(int tester=0;tester<testnum;tester++)
    {
        initial();
        int verticenum;
        int edgenum;
        int connectnum=0;
        int fi=0,se=0;
        scanf("%d%d",&verticenum,&edgenum);
        for(int i=0;i<edgenum;i++)
        {
            scanf("%d%d",&fi,&se);
            if(connected[fi]==false)
            {
                connected[fi]=true;
                connectnum++;
            }
            if(connected[se]==false)
            {
                connected[se]=true;
                connectnum++;
            }
            vec[fi].push_back(se);
            vec[se].push_back(fi);
        }
        LL res;
        LL cores;
        LL unconnect=1;
        if(connected[fi]==false)
            res=1;
        else{
            res=colorer(fi, 1);
            while (paintnum!=connectnum) {
                fi=search_uncolor_pos();
                res*=colorer(fi, 1);
            }
            for(int i=0;i<maxn;i++)
                carr[i]=0;
            paintnum=0;
            cores=colorer(fi, -1);
            while (paintnum!=connectnum) {
                fi=search_uncolor_pos();
                cores*=colorer(fi, 1);
            }
            res+=cores;
        }

        for(int u=0;u<verticenum-connectnum;u++)
        {
            unconnect*=3;
            if(u%15==0)
                unconnect=unconnect%moder;
        }

        unconnect%=moder;
        res%=moder;
        res*=unconnect;
        res%=moder;

        cout<<res<<endl;
    }
    return 0;
}
/*

 */


#include <iostream>
#include <vector>
#include <queue>
#include <cstdio>
#include <ctime>
#include <cstdlib>
#include <cmath>
#include <algorithm>

using namespace std;
typedef long long LL;
/*

 */

const int maxn=3e5+12;
const LL moder=998244353;
long N;

vector<int>vec[maxn];
int carr[maxn];
bool connected[maxn];
int history[maxn];
int paintnum=0;
int lastsearch_pos=0;
LL colorer(int stpos,int color)
{
    if(carr[stpos]==0)
    {
        carr[stpos]=color;
        history[stpos]=1;
        if(color==1)
        for(int p=0;p<vec[stpos].size();p++)
        {
           if(carr[vec[stpos][p]]==1)
               return 0;//fail
        }
            if(color==-1)
            for(int p=0;p<vec[stpos].size();p++)
            {
                if(carr[vec[stpos][p]]==-1)
                    return 0;//fail
            }
    }
    else
    {
        return 1;
    }
    LL res=1;
    if(color<0)
        res=2;

    for(int i=0;i<vec[stpos].size();i++)
    {
       if(carr[vec[stpos][i]]==0)
       res*=colorer(vec[stpos][i],-color);
        if(i%20==19)
            res%=moder;
    }
    return res%moder;
}

void initial()
{
    paintnum=0;
    for(int i=0;i<maxn;i++)
    {
        connected[i]=false;
        carr[i]=0;
        lastsearch_pos=0;
        history[i]=0;
        if(vec[i].empty())
            continue;
        vec[i].clear();
    }
}

int search_uncolor_pos()
{
    for(int i=lastsearch_pos;i<maxn;i++)
    {
        if(connected[i]==true&&history[i]==0)
        {
            lastsearch_pos=i+1;
            return i;
        }
    }
    return 0;
}
int main()
{
    int testnum;
    cin>>testnum;
    for(int tester=0;tester<testnum;tester++)
    {
        initial();
        int verticenum;
        int edgenum;
        int connectnum=0;
        int fi=0,se=0;
        int orfi;
        scanf("%d%d",&verticenum,&edgenum);
        for(int i=0;i<edgenum;i++)
        {
            scanf("%d%d",&fi,&se);
            if(connected[fi]==false)
            {
                connected[fi]=true;
                connectnum++;
            }
            if(connected[se]==false)
            {
                connected[se]=true;
                connectnum++;
            }
            vec[fi].push_back(se);
            vec[se].push_back(fi);
        }
        orfi=fi;
        LL res=1;
        LL cores=1;
        LL unconnect=1;

            while (fi!=0) {
                fi=search_uncolor_pos();
                if(fi==0)
                    break;
                for(int i=0;i<maxn;i++)
                    carr[i]=0;
                cores=colorer(fi, 1);
                for(int i=0;i<maxn;i++)
                    carr[i]=0;
                cores+=colorer(fi, -1);

                cores%=moder;
                res*=cores;
                res%=moder;
            }

        for(int u=0;u<verticenum-connectnum;u++)
        {
            unconnect*=3;
            if(u%15==14)
                unconnect%=moder;
        }

        unconnect%=moder;
        res%=moder;
        res*=unconnect;
        res%=moder;

        cout<<res<<endl;
    }
    return 0;
}
/*
 2
 2 1
 1 2
 4 6
 1 2
 1 3
 1 4
 2 3
 2 4
 3 4

 */


#include <iostream>
#include <vector>
#include <queue>
#include <cstdio>
#include <ctime>
#include <cstdlib>
#include <cmath>
#include <algorithm>

using namespace std;
typedef long long LL;

const int maxn=3e5+2;
const LL moder=998244353;
long N;
vector<int>vec[maxn];
vector<pair<int,int>>edge;
int carr[maxn];
bool connected[maxn];
int lastsearch_pos=0;
int rednum,blacknum;
LL black_pow_store[33];
LL red_pow_store[33];
LL red30_remain;
LL black30_remain;

LL colorer(int stpos,int color)
{
    if(carr[stpos]==0)
    {
        carr[stpos]=color;
        if(color==1)
            ++blacknum;
        else ++rednum;
    }
    else
    {
        return 1;
    }
    for(int i=0;i<vec[stpos].size();i++)
    {
        if(carr[vec[stpos][i]]==0)
        colorer(vec[stpos][i],-color);
        else{
            if(carr[vec[stpos][i]]==color)
                return 0;
        }
    }

    return 1;
}

void flush_unit(int i)
{
    connected[i]=false;
    carr[i]=0;
    if(!vec[i].empty())
    vec[i].clear();
}
int search_uncolor_pos()
{
    for(int i=lastsearch_pos;i<maxn;i++)
    {
        if(connected[i]==true&&carr[i]==0)
        {
            lastsearch_pos=i+1;
            return i;
        }
    }
    return 0;
}
LL store_res(int num,int mode)
{
    LL res=1;
    if(mode==2)
    {
        int red_remain=num%30;
        int red_moder=num/30;
        for(int i=0;i<red_moder;i++)
        {
            res*=red30_remain;
            res%=moder;
        }
        res*=red_pow_store[red_remain];
        res%=moder;
    }
    if(mode==3)
    {
        int black_remain=num%30;
        int black_moder=num/30;
        for(int i=0;i<black_moder;i++)
        {
            res*=black30_remain;
            res%=moder;
        }
        res*=black_pow_store[black_remain];
        res%=moder;
    }

    return res;
}
int main()
{
    int testnum;
    cin>>testnum;
    red_pow_store[0]=1;
    black_pow_store[0]=1;
    for(int i=1;i<=30;i++)
    {
        red_pow_store[i]=red_pow_store[i-1]<<1;
        black_pow_store[i]=black_pow_store[i-1]*3;
    }
    red30_remain=red_pow_store[30]%moder;
    black30_remain=black_pow_store[30]%moder;

    for(int tester=0;tester<testnum;tester++)
    {
        int verticenum;
        int edgenum;
        int connectnum=0;
        int fi=0,se=0;

        scanf("%d%d",&verticenum,&edgenum);
        lastsearch_pos=0;
        edge.clear();
        for(int i=0;i<maxn;i++)
            flush_unit(i);

        for(int i=0;i<edgenum;i++)
        {
            scanf("%d%d",&fi,&se);
            edge.push_back(make_pair(fi, se));
            if(connected[fi]==false)
            {
                connected[fi]=true;
                connectnum++;
            }
            if(connected[se]==false)
            {
                connected[se]=true;
                connectnum++;
            }
            vec[fi].push_back(se);
            vec[se].push_back(fi);
        }
        LL newres=1;
        LL cores=1;
        LL subres=1;
            while (fi!=0&&cores!=0) {
                fi=search_uncolor_pos();
                if(fi==0)
                    break;

                rednum=0;
                blacknum=0;
                cores=colorer(fi, 1);
                if(cores!=0)
                {
                    subres=store_res(rednum, 2);
                    subres+=store_res(blacknum, 2);
                    subres%=moder;
                }
                else{
                    break;
                }
                newres*=subres;
                newres%=moder;
            }
            if(cores==0)
            {
                cout<<cores<<endl;
                continue;
            }

        newres*=store_res(verticenum-connectnum, 3);
        newres%=moder;

        cout<<newres<<endl;
    }
    return 0;
}
/*
 12
 8 7
 2 3
 3 4
 4 5
 5 2
 6 7
 7 8
 8 6
 1 0
 2 1
 1 2
 3 3
 1 2
 2 3
 1 3
 3 2
 2 3
 3 1
 4 4
 1 2
 2 3
 3 4
 4 1
 4 4
 1 2
 2 3
 3 1
 4 1
 6 9
 1 4
 1 5
 1 6
 2 4
 2 5
 2 6
 3 4
 3 5
 3 6
 100000 0
 1000 5
 55 56
 56 57
 57 58
 58 59
 59 55
 11 11
 1 2
 2 3
 3 4
 4 5
 5 6
 6 7
 7 8
 8 9
 9 10
 10 11
 11 1
 10 12
 1 2
 2 3
 3 4
 4 1
 1 5
 5 6
 6 7
 7 1
 1 8
 8 9
 9 10
 10 1

 */