#ifdef NACHIA
#define _GLIBCXX_DEBUG
#else
#define NDEBUG
#endif
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using i64 = long long;
using u64 = unsigned long long;
#define rep(i,n) for(int i=0; i<int(n); i++)
const i64 INF = 1001001001001001001;
template<typename A> void chmin(A& l, const A& r){ if(r < l) l = r; }
template<typename A> void chmax(A& l, const A& r){ if(l < r) l = r; }
#include <atcoder/modint>
using Modint = atcoder::static_modint<998244353>;
using namespace std;

void solve(int N, int M, int off, vector<vector<int>>& dest){
    if(N == 0 || M == 0) return;
    if(N % 2 == 1 || M % 2 == 1){
        bool f = M % 2 == 1;
        vector<string> g = {
            "00111233",
            "04122253",
            "04445553"
        };
        if(f){
            int d = M - 3;
            rep(i,N/8) rep(x,8) rep(y,3) dest[i*8+x][d+y] = (g[y][x] - '0') + 6 * i + 1 + off;
            solve(N, M-3, off + N/8*6, dest);
        } else {
            int d = N - 3;
            rep(i,M/8) rep(x,8) rep(y,3) dest[d+y][i*8+x] = (g[y][x] - '0') + 6 * i + 1 + off;
            solve(N-3, M, off + M/8*6, dest);
        }
        return;
    }
    bool f = M % 4 == 2;
    if(f) swap(N, M);
    vector<vector<int>> ans(N, vector<int>(M));
    rep(i,N/2) rep(j,M/4){
        int x = j * N + i * 2 + 1;
        ans[i*2+0][j*4+0] = x+0;
        ans[i*2+0][j*4+1] = x+0;
        ans[i*2+0][j*4+2] = x+0;
        ans[i*2+0][j*4+3] = x+1;
        ans[i*2+1][j*4+0] = x+0;
        ans[i*2+1][j*4+1] = x+1;
        ans[i*2+1][j*4+2] = x+1;
        ans[i*2+1][j*4+3] = x+1;
    }
    if(f){
        rep(i,N) rep(j,M) dest[j][i] = ans[i][j] + off;
    } else {
        rep(i,N) rep(j,M) dest[i][j] = ans[i][j] + off;
    }
}

void testcase(){
    int N, M; cin >> N >> M;
    if(N * M % 8 != 0){ cout << "-1\n"; return; }
    if(N == 1 || M == 1){ cout << "-1\n"; return; }
    cout << (N*M/4) << "\n";
    vector<vector<int>> ans(N, vector<int>(M));
    solve(N, M, 0, ans);
    rep(i,N){
        rep(j,M){
            if(j) cout << " ";
            cout << ans[i][j];
        } cout << "\n";
    }
}

int main(){
    ios::sync_with_stdio(false); cin.tie(nullptr);
    int T; cin >> T;
    rep(t,T) testcase();
    return 0;
}