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 m and n be integers with m≥3 and n≥3. The vertex set of the m×n toroidal square lattice is {vi,j:0≤i≤m−1, 0≤j≤n−1}. Two vertices vi,j and vi′,j′ are joined by an edge when one of the following holds.
Definition 2. Let m and n be integers with m≥3, n≥3 and m even. The m×n toroidal triangular lattice is the m×n toroidal square lattice together with the following edges, for all i and j.
Definition 3. Let m and n be integers with m≥4, n≥4 and both even. The vertex set of the m×n toroidal hexagonal lattice is {vi,j:0≤i≤m−1, 0≤j≤n−1}. Two vertices vi,j and vi′,j′ are joined by an edge when one of the following holds.
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×n toroidal lattice exactly once. A cycle is a sequence (u1,u2,…,umn) of mn distinct vertices such that uk and uk+1 are adjacent for every k∈{1,…,mn−1}, and umn and u1 are adjacent as well. The toroidal square lattice is simple, so only the toroidal triangular and toroidal hexagonal lattices appear in the input.
The first line contains the number of test cases T, a positive integer. Each of the next T lines contains three integers m, n and p separated by spaces, with 3≤m≤111, 3≤n≤111 and p∈{3,6}. The graph of that test case is the m×n toroidal triangular lattice if p=3, and the m×n toroidal hexagonal lattice if p=6.
The given lattice always satisfies its definition: m is even when p=3, and both m and n are even and at least 4 when p=6.
Print the results for the T 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 mn lines listing the vertices of the cycle in order. Print vertex vi,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, amodb is the remainder in the range from 0 to b−1.
For p=3, print in this order.
For p=6, for k=0,1,…,n/2−1 in this order, print these 2m vertices.
This rule produces a cycle for every input that satisfies the constraints, so the first line of each test case is always 1.