T=int(input()) for i in range(T): N,M=map(int,input().split()) if N%4==0 and M%2==0: print(N*M//4) ans=[[0]*M for i in range(N)] cnt=1 for i in range(0,N,4): for j in range(0,M,2): ans[i][j]=cnt ans[i][j+1]=cnt ans[i+1][j]=cnt ans[i+2][j]=cnt ans[i+3][j]=cnt+1 ans[i+3][j+1]=cnt+1 ans[i+2][j+1]=cnt+1 ans[i+1][j+1]=cnt+1 cnt+=2 for i in ans: print(*i) elif N%2==0 and M%4==0: print(N*M//4) ans=[[0]*M for i in range(N)] cnt=1 for i in range(0,M,4): for j in range(0,N,2): ans[j][i]=cnt ans[j+1][i]=cnt ans[j][i+1]=cnt ans[j][i+2]=cnt ans[j][i+3]=cnt+1 ans[j+1][i+3]=cnt+1 ans[j+1][i+2]=cnt+1 ans[j+1][i+1]=cnt+1 cnt+=2 for i in ans: print(*i) elif N%8==0 and M>=3: print(N*M//4) ans=[[0]*M for i in range(N)] cnt=1 for i in range(0,N,8): ans[i][0]=cnt ans[i][1]=cnt ans[i][2]=cnt ans[i+1][0]=cnt cnt+=1 ans[i+1][1]=cnt ans[i+1][2]=cnt ans[i+2][2]=cnt ans[i+3][2]=cnt cnt+=1 ans[i+2][0]=cnt ans[i+2][1]=cnt ans[i+3][1]=cnt ans[i+4][1]=cnt cnt+=1 ans[i+3][0]=cnt ans[i+4][0]=cnt ans[i+5][0]=cnt ans[i+5][1]=cnt cnt+=1 ans[i+4][2]=cnt ans[i+5][2]=cnt ans[i+6][2]=cnt ans[i+6][1]=cnt cnt+=1 ans[i+6][0]=cnt ans[i+7][0]=cnt ans[i+7][1]=cnt ans[i+7][2]=cnt cnt+=1 for i in range(0,N,4): for j in range(3,M,2): ans[i][j]=cnt ans[i][j+1]=cnt ans[i+1][j]=cnt ans[i+2][j]=cnt ans[i+3][j]=cnt+1 ans[i+3][j+1]=cnt+1 ans[i+2][j+1]=cnt+1 ans[i+1][j+1]=cnt+1 cnt+=2 for i in ans: print(*i) elif M%8==0 and N>=3: print(N*M//4) N,M=M,N ans=[[0]*M for i in range(N)] cnt=1 for i in range(0,N,8): ans[i][0]=cnt ans[i][1]=cnt ans[i][2]=cnt ans[i+1][0]=cnt cnt+=1 ans[i+1][1]=cnt ans[i+1][2]=cnt ans[i+2][2]=cnt ans[i+3][2]=cnt cnt+=1 ans[i+2][0]=cnt ans[i+2][1]=cnt ans[i+3][1]=cnt ans[i+4][1]=cnt cnt+=1 ans[i+3][0]=cnt ans[i+4][0]=cnt ans[i+5][0]=cnt ans[i+5][1]=cnt cnt+=1 ans[i+4][2]=cnt ans[i+5][2]=cnt ans[i+6][2]=cnt ans[i+6][1]=cnt cnt+=1 ans[i+6][0]=cnt ans[i+7][0]=cnt ans[i+7][1]=cnt ans[i+7][2]=cnt cnt+=1 for i in range(0,N,4): for j in range(3,M,2): ans[i][j]=cnt ans[i][j+1]=cnt ans[i+1][j]=cnt ans[i+2][j]=cnt ans[i+3][j]=cnt+1 ans[i+3][j+1]=cnt+1 ans[i+2][j+1]=cnt+1 ans[i+1][j+1]=cnt+1 cnt+=2 ans=[list(x) for x in zip(*ans)] for i in ans: print(*i) else: print(-1)