#P2552. [AHOI2001] 团体操队形

[AHOI2001] 团体操队形

Description

There are nn group gymnastics members labeled 1n1 \sim n. Their formation (either continuous formation or "Meihuazhuang" formation) can be arranged by rows or by columns. When arranged by rows (or columns), each row (or column) has rr positions.

Example 1: n=16n=16, r=6r=6, continuous formation arranged by rows. |1|2|3|4|5|6| |:-:|:-:|:-:|-|-|-| |7|8|9|10|11|12| |13|14|15|16|

Example 2: n=16n=16, r=3r=3, continuous formation arranged by columns. | 1 | 4 | 7 | 10 | 13 |16| | :-: | :-: | :-: | :-: | :-: |- | |2|5|8|11|14| |3|6|9|12|15|

Example 3: n=16n=16, r=7r=7, "Meihuazhuang" formation arranged by rows. | 1 | | 2 | | 3 | | 4 | | :-: | :-: | :-: | :-: | :-: | :-: | :-: | | |5 | |6 | |7 | | |8 | | 9| |10| |11| | |12| |13| |14| | |15| |16| | | | |

Example 4: n=16n=16, r=5r=5, "Meihuazhuang" formation arranged by columns. | 1 | | 6 | | 11 | |16 | | :-: | :-: | :-: | :-: | :-: | :-: |:-:| | |4| |9 | |14| | |2| |7| |12| | | | |5| |10| |15| | |3| |8| |13| | |

Please write a program to output the row index and column index of the gymnast labeled mm.

Input Format

This problem contains multiple testcases. The first line has a single natural number kk, indicating that the following kk lines each describe one testcase. Each of the next kk lines contains five integers, denoting respectively: the number of people in the team, the formation code (11 means continuous formation, and 22 means "Meihuazhuang" formation), the arrangement method (11 means arranged by rows, and 22 means arranged by columns), the number of positions per row (or column), and the label of the gymnast being queried.

Output Format

Output 2k2k integers. The ii-th pair of integers denotes the row index and column index for the ii-th query (no newline).

2
16 1 1 6 11
16 2 2 5 11
2 5 1 5

Hint

Translated by ChatGPT 5