QOJ.ac

QOJ

時間限制: 1 s 記憶體限制: 512 MB 總分: 100 可 Hack ✓

#17358. Mex Grid

统计

Yuki has a table with $n$ rows and $m$ columns.

She wants to fill each cell with a non-negative integer no greater than $n \cdot m$ such that the sum of the $\operatorname{mex}^\ast$ of each row and the $\operatorname{mex}$ of each column is maximized.

Formally, let $a_{i,j}$ denote the number in the $i$-th row and $j$-th column of the table. She wants to maximize:

$$ \sum\limits_{i = 1}^{n} \operatorname{mex}\{a_{i,1}, \ldots, a_{i,m}\} + \sum\limits_{j = 1}^{m} \operatorname{mex}\{a_{1,j}, \ldots, a_{n,j}\} $$

Unfortunately, Yuki does not know how to construct this table. Can you help her?

$^\ast$: The $\operatorname{mex}$ of a sequence is the smallest non-negative integer that does not appear in the sequence. For example, $\operatorname{mex}\{0,1,2\}=3$, $\operatorname{mex}\{1,0,3,1\}=2$, and $\operatorname{mex} \varnothing=0$.

Input

This problem contains multiple test cases.

The first line contains a positive integer $t\ (1 \le t \le 10^5)$, representing the number of test cases.

For each test case:

  • One line containing two positive integers $n, m\ (1 \le n \cdot m \le 5\cdot10^5)$.

It is guaranteed that the sum of $n \cdot m$ over all test cases does not exceed $5\cdot10^5$.

Output

For each test case, output $n$ lines, each containing $m$ non-negative integers no greater than $n \cdot m$, representing the numbers in your constructed table.

Examples

Input 1

3
2 4
1 1
3 4

Output 1

1 2 0 3
0 3 1 2
0
1 2 0 1
0 1 2 0
2 0 1 3

Note

For the first test case:

  • The $\operatorname{mex}$ of the first and second rows of the given table is $4$, the $\operatorname{mex}$ of the first and third columns is $2$, and the $\operatorname{mex}$ of the second and fourth columns is $0$. The sum of the $\operatorname{mex}$ of each row and each column is $12$.
  • It can be proven that $12$ is the maximum possible sum of the $\operatorname{mex}$ of each row and each column among all possible constructions.

For the second test case:

  • Clearly, $a_{1,1}=0$ maximizes the sum of the $\operatorname{mex}$ of each row and each column.

For the third test case:

  • It can be proven that $21$ is the maximum possible sum of the $\operatorname{mex}$ of each row and each column among all possible constructions.

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.