#include #include #include using namespace std; using namespace atcoder; using mint = modint998244353; #define rep(i,n) for (int i = 0; i < (n); ++i) #define Inf32 1000000001 #define Inf64 1000000000000000000LL void dfs(vector> a,vector> xs,vector> ys,int cur){ if(a.size()*a[0].size()%4==0&&cur > a.size()*a[0].size()/4){ rep(i,a.size()){ rep(j,a[i].size()){ if(j!=0)cout<<' '; cout< t = {Inf32,Inf32}; rep(i,n){ rep(j,m){ if(a[i][j]==0){ t = {i,j}; break; } } if(t.first!=Inf32)break; } rep(i,n){ rep(j,m){ rep(k,xs.size()){ auto copy = a; bool f =true; rep(l,xs[k].size()){ int xx = i+xs[k][l]; int yy = j+ys[k][l]; if(xx<0||xx>=n||yy<0||yy>=m){ f = false; break; } if(copy[xx][yy]){ f = false; break; } copy[xx][yy] = cur; } if(f&©[t.first][t.second]!=0){ dfs(copy,xs,ys,cur+1); } } } } } vector> get(int n,int m){ vector> t = {{0,0,0},{0,1,1},{2,2,1},{3,2,1},{3,2,4},{3,3,4},{5,4,4},{5,5,5}}; int cur = 6; int xx = n; if(n==3)xx = m; while(t.size()!=xx){ rep(i,8){ t.push_back(t[i]); rep(j,t.back().size())t.back()[j] += cur; } cur += 6; } if(m==3)return t; vector> r(n,vector(m)); rep(i,n){ rep(j,m){ r[i][j] = t[j][i]; } } return r; } int main(){ /* vector> xs,ys; xs.push_back({0,1,2,2}); ys.push_back({0,0,0,1}); xs.push_back({0,0,0,1}); ys.push_back({0,1,2,0}); xs.push_back({0,0,1,2}); ys.push_back({0,1,1,1}); xs.push_back({0,1,1,1}); ys.push_back({2,0,1,2}); rep(i,4){ xs.push_back(xs[i]); ys.push_back(ys[i]); int ma = 0; rep(j,xs[i].size())ma = max(ma,xs[i][j]); rep(j,xs[i].size())xs.back()[j] = ma-xs[i][j]; } int n,m; cin>>n>>m; vector a(n,vector(m)); dfs(a,xs,ys,1); return 0; */ int _t; cin>>_t; rep(_,_t){ int n,m; cin>>n>>m; if((n%4&&m%4)){ cout<<-1<> a(n,vector(m)); if(m%4==0){ for(int i=0;i0){ n += dx; auto r = get(dx,m); rep(i,r.size()){ rep(j,r[i].size()){ r[i][j] += cur; } } rep(j,r.size())a.push_back(r[j]); } else if(dy>0){ m += dy; auto r = get(n,dy); rep(i,r.size()){ rep(j,r[i].size()){ r[i][j] += cur; } } rep(i,r.size()){ rep(j,r[i].size()){ a[i].push_back(r[i][j]); } } } rep(i,n){ rep(j,m){ if(j!=0)cout<<' '; cout<