Toroidal grid

No attempts yetTime limit1sMemory limit256 MB

Problem

The m×nm \times n rectangular grid is the graph whose vertices correspond to the points of the plane with an integer xx coordinate from 00 to n1n-1 and an integer yy coordinate from 00 to m1m-1, and which has an edge between two vertices exactly when the distance between their two points is 11. The grid has nn vertices in each of its mm rows and mm vertices in each of its nn columns. The vertex in row ii and column jj is written (i,j)(i,j), where 0im10 \le i \le m-1 and 0jn10 \le j \le n-1.

Add an edge between (i,0)(i,0) and (i,n1)(i,n-1) for every row ii, and an edge between (0,j)(0,j) and (m1,j)(m-1,j) for every column jj. Every row then forms a cycle of length nn and every column forms a cycle of length mm. The graph built this way is called the m×nm \times n toroidal grid, because you can draw it on a torus with no two edges crossing.

Write a program that, for a given m×nm \times n toroidal grid, finds a cycle passing through every vertex exactly once. Such a cycle is a sequence (v1,v2,,vmn)(v_1, v_2, \ldots, v_{mn}) of mnmn distinct vertices in which vkv_k and vk+1v_{k+1} are adjacent for every kk with 1kmn11 \le k \le mn-1, and vmnv_{mn} and v1v_1 are adjacent as well.

Input

Read the data from standard input. The first line has one integer TT, the number of test cases. Each of the next TT lines has two integers mm and nn, meaning that the input graph on that line is the m×nm \times n toroidal grid. Here 3m,n1003 \le m, n \le 100.

Output

Write the data to standard output. For each test case, first print on its own line one integer telling whether a cycle exists: 1 if it does, -1 if it does not. Only when that line is 1, print the vertex sequence of the cycle on the next mnmn lines. Print the vertex (i,j)(i,j) as (i,j) with no space. No line may contain a whitespace character (a blank or a tab).

A toroidal grid has many such cycles, so exactly one of them is accepted.

  • Start at (0,0)(0,0).
  • Sweep only columns 00 to n2n-2 of each row, in snake order. Row 00 runs from column 00 toward column n2n-2, row 11 runs from column n2n-2 toward column 00, and the direction keeps alternating. At the end of a row, step straight down to the next row without changing the column, then sweep that row.
  • After row m1m-1 is finished, move to (m1,n1)(m-1,n-1), then walk up column n1n-1 from (m1,n1)(m-1,n-1) to (0,n1)(0,n-1).
  • The last vertex (0,n1)(0,n-1) is adjacent to (0,0)(0,0) through the wrap edge of row 00, so the cycle closes.

Every toroidal grid with 3m,n1003 \le m, n \le 100 has such a cycle, so the first line of every test case is always 1.