Torus

No attempts yetTime limit1sMemory limit256 MB

Problem

A regular polygon has all sides of the same length and all interior angles equal. A regular tiling of the Euclidean plane covers the whole plane with congruent regular polygons that do not overlap and meet vertex to vertex. Only three regular polygons admit a regular tiling: the square, the equilateral triangle and the regular hexagon. A lattice is an infinite graph whose drawing on the plane is a regular tiling, so there are the square lattice, the triangular lattice and the hexagonal lattice.

Such a lattice turns into a graph with finitely many vertices and edges that keeps the symmetry of the infinite one. The finite graph can be drawn on the surface of a torus with no edge crossings, and the regions bounded by its shortest cycles have similar shapes and sizes. A graph obtained this way is a toroidal lattice, defined as follows.

Definition 1. Let mm and nn be integers with m3m \ge 3 and n3n \ge 3. The vertex set of the m×nm \times n toroidal square lattice is {vi,j:0im1, 0jn1}\{v_{i,j} : 0 \le i \le m-1,\ 0 \le j \le n-1\}. Two vertices vi,jv_{i,j} and vi,jv_{i',j'} are joined by an edge when one of the following holds.

  • i=ii' = i and jj+1(modn)j' \equiv j+1 \pmod{n}
  • j=jj' = j and ii+1(modm)i' \equiv i+1 \pmod{m}

Definition 2. Let mm and nn be integers with m3m \ge 3, n3n \ge 3 and mm even. The m×nm \times n toroidal triangular lattice is the m×nm \times n toroidal square lattice together with the following edges, for all ii and jj.

  • If ii is even, join vi,jv_{i,j} and vi,jv_{i',j'}, where ii+1(modm)i' \equiv i+1 \pmod{m} and jj1(modn)j' \equiv j-1 \pmod{n}.
  • If ii is odd, join vi,jv_{i,j} and vi,jv_{i',j'}, where ii+1(modm)i' \equiv i+1 \pmod{m} and jj+1(modn)j' \equiv j+1 \pmod{n}.

Definition 3. Let mm and nn be integers with m4m \ge 4, n4n \ge 4 and both even. The vertex set of the m×nm \times n toroidal hexagonal lattice is {vi,j:0im1, 0jn1}\{v_{i,j} : 0 \le i \le m-1,\ 0 \le j \le n-1\}. Two vertices vi,jv_{i,j} and vi,jv_{i',j'} are joined by an edge when one of the following holds.

  • j=jj' = j and ii+1(modm)i' \equiv i+1 \pmod{m}
  • i=ii' = i and jj+1(modn)j' \equiv j+1 \pmod{n} and i+j0(mod2)i+j \equiv 0 \pmod{2}

To help a project that builds a library of lattice graph algorithms, write a program that finds a cycle visiting every vertex of a given m×nm \times n toroidal lattice exactly once. A cycle is a sequence (u1,u2,,umn)(u_1, u_2, \dots, u_{mn}) of mnmn distinct vertices such that uku_k and uk+1u_{k+1} are adjacent for every k{1,,mn1}k \in \{1, \dots, mn-1\}, and umnu_{mn} and u1u_1 are adjacent as well. The toroidal square lattice is simple, so only the toroidal triangular and toroidal hexagonal lattices appear in the input.

Input

The first line contains the number of test cases TT, a positive integer. Each of the next TT lines contains three integers mm, nn and pp separated by spaces, with 3m1113 \le m \le 111, 3n1113 \le n \le 111 and p{3,6}p \in \{3, 6\}. The graph of that test case is the m×nm \times n toroidal triangular lattice if p=3p = 3, and the m×nm \times n toroidal hexagonal lattice if p=6p = 6.

The given lattice always satisfies its definition: mm is even when p=3p = 3, and both mm and nn are even and at least 4 when p=6p = 6.

Output

Print the results for the TT test cases in the given order. For each test case, the first line contains an integer that tells whether a cycle exists: 1 if it does, -1 if it does not. Only when the first line is 1, it is followed by mnmn lines listing the vertices of the cycle in order. Print vertex vi,jv_{i,j} as (i,j), with no space or tab inside a line.

Several cycles can exist, so only the cycle built by the following rule is accepted. Below, amodba \bmod b is the remainder in the range from 00 to b1b-1.

For p=3p = 3, print in this order.

  1. v0,0v_{0,0}
  2. For j=0,1,,n1j = 0, 1, \dots, n-1 in this order: print v1,j,v2,j,,vm1,jv_{1,j}, v_{2,j}, \dots, v_{m-1,j} if jj is even, and vm1,j,vm2,j,,v1,jv_{m-1,j}, v_{m-2,j}, \dots, v_{1,j} if jj is odd.
  3. v0,n1v_{0,n-1}
  4. v0,n2,v0,n3,,v0,1v_{0,n-2}, v_{0,n-3}, \dots, v_{0,1}

For p=6p = 6, for k=0,1,,n/21k = 0, 1, \dots, n/2-1 in this order, print these 2m2m vertices.

  1. v(1t)modm, 2kv_{(1-t) \bmod m,\ 2k} for t=0,1,,m1t = 0, 1, \dots, m-1
  2. v(2+t)modm, 2k+1v_{(2+t) \bmod m,\ 2k+1} for t=0,1,,m1t = 0, 1, \dots, m-1

This rule produces a cycle for every input that satisfies the constraints, so the first line of each test case is always 1.