T=int(input()) for t in range(T): N,M=map(int,input().split()) C0=[[0,0,0,1],[0,1,1,1]] C1=[[0,0,0,2,3,3,3,5,5],[0,1,2,2,2,3,4,5],[0,1,1,1,4,4,4,5]] XYC0=[(x,y,C0[x][y]) for x in range(2) for y in range(4)] XYC1=[(x,y,C1[x][y]) for x in range(3) for y in range(8)] ans_lst=[[None]*M for n in range(N)] def solve(N,M): if N%2==0 and M%4==0: retu=[[None]*M for n in range(N)] cur=1 for n in range(0,N,2): for m in range(0,M,4): for x,y,c in XYC0: retu[n+x][m+y]=cur+c cur+=2 elif N>=2 and M%8==0: cur=1 retu=[[None]*M for n in range(N)] if N%2: for m in range(0,M,8): for x,y,c in XYC1: retu[x][m+y]=cur+c cur+=6 for n in range(3 if N%2 else 0,N,8): for m in range(0,M,8): for x,y,c in XYC1: retu[n+x][m+y]=cur+c cur+=6 elif N%4==0 and M%2==0 or N%8==0 and M>=2: retu=solve(M,N) retu=[[retu[m][n] for m in range(M)] for n in range(N)] else: retu=None return retu ans_lst=solve(N,M) if ans_lst==None: print(-1) else: print(N*M//4) for n in range(N): print(*ans_lst[n])