#P7485. 「Stoi2031」枫

「Stoi2031」枫

Description

Dong really likes maple leaves. There is a maple tree in front of her house, and nn leaves have fallen from it. Dong numbers them from 11 to nn. She does not want these maple leaves to be stepped on and rot on the ground, so she decides to pick them up.

She calls the following process a retrieval: sort the remaining unpicked maple leaves by their numbers in increasing order or decreasing order, pick up the first leaf, then pick up one leaf every kk leaves. She will keep performing retrieval. The first retrieval is from small to large; after that, each retrieval uses the opposite order of the previous one (i.e., if last time was from small to large, this time is from large to small, and vice versa), until the last leaf is also picked up.

She believes the last leaf picked up represents longing, which can bring happiness. She wants more happiness, so she will ask you many times: for given values of nn and kk, what is the number of the longing leaf.

Input Format

The first line contains the number of test groups tt, satisfying t=it=i, where ii is the testdata point ID.

Lines 22 to t+1t+1: each line starts with two integers q,mq,m, meaning she will take qq queries, and in each query the number of leaves nn does not exceed mm. Then qq integers follow, each being the value of nn for one query. For all queries on line xx, k=x1k=x-1.

Output Format

For each line, output qq integers, representing the answers to the queries on that line.

1
2 3 1 3

1 2

2
2 3 1 3
3 7 2 4 7

1 2
2 2 5

3
2 3 1 3
3 7 2 4 7
7 10 1 2 3 6 7 8 10

1 2
2 2 5
1 2 2 3 4 6 6

Hint

Brief statement:

Given n,kn,k, repeatedly operate on 1,2,,n1,2,\dots,n. In each operation, alternately take numbers in increasing order or decreasing order, and remove the current (k+1)x+1(k+1)x+1-th number (where xZ0x \in \mathbb{Z_{\ge 0}} and (k+1)x+1(k+1)x+1 does not exceed the total count of remaining numbers). Find the number removed last. There are multiple queries.

Sample explanation:

Due to space limits, only sample 22 is explained.

For line 22:

For the first query, there is only 11 maple leaf on the ground, which is the longing.

For the second query, in Dong's first retrieval, she picks up leaves 1,31,3 in order; after turning around, only 22 remains, which is the longing.

For line 33:

For the first query, in Dong's first retrieval, she picks up leaf 11; after turning around, 22 remains, which is the longing.

For the second query, in Dong's first retrieval, she picks up leaves 1,41,4; in the second retrieval, she picks up 33; 22 remains, which is the longing.

For the third query, Dong first picks up 1,4,71,4,7, then picks up 6,26,2, then picks up 33; now 55 remains, which is the longing.

Constraints:

For each testdata point of this problem (except the 11st), the input data is exactly the same as the previous testdata point, except for the number of test groups tt and the last line (line t+1t+1). The constraints and special limits of each testdata point are as follows.

Testdata No. qq \le mm \le Special limit Score
11 22 33 Sample 11 33
22 33 77 Sample 22 77
33 77 1010 Sample 33 33
44 1010 3030 None
55 3030 7070 77
66 7070 100100
77 100100 300300
88 300300 700700 1010
99 700700 10310^3 33
1010 10310^3 3×1033 \times 10^3
1111 3×1033 \times 10^3 7×1037 \times 10^3 11
1212 7×1037 \times 10^3 10410^4 1313
1313 10410^4 3×1043 \times 10^4 33
1414 3×1043 \times 10^4 7×1047 \times 10^4
1515 7×1047 \times 10^4 10510^5 1010
1616 10510^5 3×1053 \times 10^5 1313
1717 3×1053 \times 10^5 7×1057 \times 10^5 11
1818 7×1057 \times 10^5 10610^6 33

The input size of this problem is large. You may choose to use the fast input template provided in the contest statement to speed up reading.

Translated by ChatGPT 5