Domino (hard version)
Contest: Codeforces Round 734 (Div. 3)
Difficulty rating: 2100
Tags: constructive algorithms, implementation, math

The only difference between this problem and D1 is that you don't have to provide the way to construct the answer in D1, but you have to do it in this problem.

There's a table of $$$n \times m$$$ cells ($$$n$$$ rows and $$$m$$$ columns). The value of $$$n \cdot m$$$ is even.

A domino is a figure that consists of two cells having a common side. It may be horizontal (one of the cells is to the right of the other) or vertical (one of the cells is above the other).

You need to place $$$\frac{nm}{2}$$$ dominoes on the table so that exactly $$$k$$$ of them are horizontal and all the other dominoes are vertical. The dominoes cannot overlap and must fill the whole table.

Input format:
The first line contains one integer $$$t$$$ ($$$1 \le t \le 10$$$) — the number of test cases. Then $$$t$$$ test cases follow.

Each test case consists of a single line. The line contains three integers $$$n$$$, $$$m$$$, $$$k$$$ ($$$1 \le n,m \le 100$$$, $$$0 \le k \le \frac{nm}{2}$$$, $$$n \cdot m$$$ is even) — the count of rows, columns and horizontal dominoes, respectively.

Output format:
For each test case:

- print "NO" if it's not possible to place the dominoes on the table in the described way;
- otherwise, print "YES" on a separate line, then print $$$n$$$ lines so that each of them contains $$$m$$$ lowercase letters of the Latin alphabet — the layout of the dominoes on the table. Each cell of the table must be marked by the letter so that for every two cells having a common side, they are marked by the same letters if and only if they are occupied by the same domino. I.e. both cells of the same domino must be marked with the same letter, but two dominoes that share a side must be marked with different letters. If there are multiple solutions, print any of them.

Constraints:
Time limit: 1.0 s
Memory limit: 256.0 MB

Example input:
8
4 4 2
2 3 0
3 2 3
1 2 0
2 4 2
5 2 2
2 17 16
2 1 1

Example output:
YES
accx
aegx
bega
bdda
YES
aha
aha
YES
zz
aa
zz
NO
YES
aaza
bbza
NO
YES
bbaabbaabbaabbaay
ddccddccddccddccy
NO

Editorial:
Suppose $$$n$$$ and $$$m$$$ are even. A necessary and sufficient condition of existence of the answer is that $$$k$$$ is even.
Let's prove the sufficient condition. If the count of the horizontal dominoes is even, then we can combine them and vertical dominoes to blocks of size $$$2 \times 2$$$ (the number of the vertical dominoes is even, too, if $$$k$$$ is even). If $$$n$$$ and $$$m$$$ are even, we can fill the table with these blocks. The description of the locations of the dominoes may be printed as follows: consider the table is a chessboard where a cell is a block of two dominoes. Consider the leftmost topmost cell of the board is black. If a cell of the board is black, let's mark one of the dominoes of the block with the letter "a" and the other one with the letter "b". If a cell of the board is white, let's mark one of the dominoes of the block with the letter "c" and the other one with the letter "d". There will be no situation that some two cells of the table are marked with one letter but belong to different dominoes.
Let's prove the necessary condition. The number of cells in a column ($$$n$$$) is even, so the number of cells that belong to vertical dominoes is even because cells of each vertical domino may be either belong to the column or not belong at the same time. So the number of cells that belong to horizontal dominoes is even. Let's cross out all cells that belong to vertical dominoes and let's find the leftmost column having cells that haven't been crossed out. It's the leftmost column with such cells so the pairwise cells of the non-crossed out cells belong to the column to the right of the found one. The number of such cells in the right column is equal to the number of found cells so it's even and the number of found horizontal dominoes is even, too. Let's cross out the found cells and the pairwise cells. The number of non-crossed out cells in the right column will be even. The number of crossed-out horizontal dominoes will be even, too. Let's repeat this procedure until all the dominoes will be crossed out. In every step, we have crossed out the even number of horizontal dominoes, hence the total count of horizontal dominoes is even.
Suppose $$$n$$$ is odd, hence $$$m$$$ is even. In this case, every column contains an odd number of cells, whereas the number of cells that belong to vertical dominoes is even. So the number of cells that belong to horizontal dominoes is odd. Consider the leftmost column and find a cell of it that belongs to a horizontal domino (it must be found because the number of such cells is odd so it isn't equal to $$$0$$$). Let's find the pairwise cell and cross out both cells. Currently, the two columns will have an even number of non-crossed-out cells. Let's repeat the procedure until all columns will have even non-crossed-out cells. We will cross out $$$m$$$ cells and $$$\frac{m}{2}$$$ dominoes. So the necessary condition is that the number of horizontal dominoes ($$$k$$$) is at least $$$\frac{m}{2}$$$.
Let's extend the necessary condition with the following condition: the value of $$$k - \frac{m}{2}$$$ is even. Consider the table that we've become after the previous procedure where each column has exactly one crossed-out cell. Let's start the procedure we've done in the case of even both $$$n$$$ and $$$m$$$. The procedure can be started on our table because each column of the table has an even number of non-crossed-out cells. As a result of the procedure, we will cross out an even count of horizontal dominoes, so the value of $$$k - \frac{m}{2}$$$ is even.
Let's build an answer if the conditions $$$k \ge \frac{m}{2}$$$ and $$$k - \frac{m}{2}$$$ is even are met. Let's place in the topmost row $$$\frac{m}{2}$$$ horizontal dominoes and mark their cells as follows: the first domino will be marked with "x", the second one — with "y", the third one — with "x", and so on. As the result, the region of $$$n - 1$$$ rows and $$$m$$$ columns will be unfilled. Both values are even, and the value of $$$k - \frac{m}{2}$$$ is even, too. So let's fill the region as if it's a separate table having even numbers of rows and columns. As it was proved above, it's possible to do. The set of letters used for the region and set of the letters used for the topmost row don't have common elements, so there will be no cells that are marked with one letter but belong to different dominoes.
The case of odd $$$m$$$ (hence, $$$n$$$ is even) is similar to the previous one — let's transpose the table (it will have $$$m$$$ rows and $$$n$$$ columns), swap the values of $$$k$$$ and $$$\frac{nm}{2} - k$$$, solve the case above and transpose the table back to have $$$n$$$ rows and $$$m$$$ columns.