East China University of Science and Technology Programming Championship 2026
A. Spotlights
time limit per test
5 seconds
memory limit per test
1024 megabytes
input
standard input
output
standard output

A wonderful performance is about to begin on the stage set up by cold_beans...

The stage is a 2D Euclidean plane. Character A initially stands at the center of the stage, which is the origin $$$(0, 0)$$$. There are $$$n$$$ spotlights above the stage. Each spotlight projects a circular illuminated area on the plane with its center at $$$(x_i, y_i)$$$ and a radius of $$$\sqrt{x_i^2 + y_i^2}$$$. The boundary of the circle is included in the illuminated area.

To show the tension of the performance, A wants to always stay within the illuminated areas of all spotlights during the performance, and reach the farthest possible point from the center of the stage.

Please help A find the maximum distance from the center of the stage to a point she can reach under the above conditions.

Input

The first line contains a single integer $$$T$$$ ($$$1 \le T \le 10^5$$$) — the number of test cases.

For each test case:

The first line contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the number of spotlights.

Each of the following $$$n$$$ lines contains two integers $$$x_i$$$ and $$$y_i$$$ ($$$-10^5 \le x_i, y_i \le 10^5$$$, not both zero) — the coordinates of the center of the illuminated area of a spotlight.

It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$3 \times 10^5$$$.

It is guaranteed that no two spotlights share the same center, and no three spotlight centers are collinear.

Output

For each test case, output a single real number $$$d$$$ on a new line — the maximum distance from the center of the stage to a point A can reach.

Your answer will be considered correct if its absolute or relative error does not exceed $$$10^{-6}$$$.

Example
Input
2
2
0 1
1 0
4
1 1
-1 1
1 -1
-1 -1
Output
1.414213562
0.000000000
Note

The figure above illustrates the illuminated areas of the spotlights in the first testcase. In the first test case, the two spotlights project circles passing through the origin, centered at $$$(0, 1)$$$ and $$$(1, 0)$$$. It can be seen that the farthest point A can reach is $$$(1, 1)$$$, so the answer is $$$d = \sqrt{2} \approx 1.41421356$$$.

In the second testcase, there are no intersecting areas for the illuminated regions other than the origin. Therefore, the farthest point A can reach is the origin $$$(0, 0)$$$, and the answer is $$$0$$$.

B. Echo Form
time limit per test
1 second
memory limit per test
1024 megabytes
input
standard input
output
standard output

Constructive Kingdom is still chasing Little T...

Define the prefix $$$\max$$$ sequence $$$x$$$ of a sequence $$$a$$$ of length $$$n$$$ as $$$x_i = \max\{a_j : 1 \le j \le i\}$$$, and the prefix $$$\min$$$ sequence $$$y$$$ as $$$y_i = \min\{a_j : 1 \le j \le i\}$$$.

Given $$$n$$$ and $$$k$$$, please construct two sequences $$$a$$$ and $$$b$$$, both of length $$$n$$$, that satisfy the following conditions. If no such two sequences exist, report that there is no solution:

  • $$$a$$$ and $$$b$$$ are permutations of $$$1$$$ to $$$n$$$.
  • $$$a$$$ and $$$b$$$ differ in at least one position, i.e., there exists an $$$i$$$ such that $$$a_i \ne b_i$$$.
  • For each of $$$a$$$ and $$$b$$$, let its prefix $$$\max$$$ sequence be $$$x$$$ and its prefix $$$\min$$$ sequence be $$$y$$$. It must satisfy: $$$$$$ \sum_{i=1}^n (x_i - y_i) = k. $$$$$$
Input

The first line contains an integer $$$T$$$ ($$$1 \le T \le 10^5$$$), indicating the number of test cases.

Each test case consists of a single line containing two integers $$$n$$$ and $$$k$$$ ($$$2 \le n \le 10^5, 0 \le k \le 10^{10}$$$), representing the length of the sequences and the required sum of differences between the prefix $$$\max$$$ and $$$\min$$$.

It is guaranteed that $$$\sum n \le 3 \times 10^5$$$ over all test cases.

Output

For each test case, if there exist two sequences satisfying the conditions, output two lines.

The first line should contain $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$, representing your constructed sequence $$$a$$$.

The second line should contain $$$n$$$ integers $$$b_1, b_2, \ldots, b_n$$$, representing your constructed sequence $$$b$$$.

If no such two sequences exist, output a single line containing the integer $$$-1$$$.

If there are multiple valid answers, you may output any of them.

Example
Input
3
2 1
2 0
5 12
Output
1 2
2 1
-1
1 3 4 2 5
1 2 4 5 3
Note

For the 3rd testcase, let's verify if a hypothetical sequence $$$b = \{1, 2, 4, 5, 3\}$$$ satisfies the conditions:

  • $$$\{1, 2, 4, 5, 3\}$$$ is a permutation of $$$1$$$ to $$$5$$$ because each integer from $$$1$$$ to $$$5$$$ appears exactly once.
  • Suppose the second element of sequence $$$a$$$ is $$$a_2 = 3$$$, and $$$b_2 = 2$$$, then $$$a_2 \ne b_2$$$.
  • Its prefix $$$\max$$$ sequence is $$$x = \{1, 2, 4, 5, 5\}$$$, and its prefix $$$\min$$$ sequence is $$$y = \{1, 1, 1, 1, 1\}$$$. Thus, we have $$$$$$ \sum_{i=1}^n (x_i - y_i) = (1 - 1) + (2 - 1) + (4 - 1) + (5 - 1) + (5 - 1) = 0 + 1 + 3 + 4 + 4 = 12 = k. $$$$$$

Additional Notes

A permutation of $$$1$$$ to $$$n$$$ is a sequence of length $$$n$$$ in which every integer from $$$1$$$ to $$$n$$$ appears exactly once.

C. Star Farming
time limit per test
1 second
memory limit per test
1024 megabytes
input
standard input
output
standard output

Pheonix has arrived in outer space. There are $$$n$$$ galaxies, numbered $$$1$$$, $$$2$$$, $$$\ldots$$$, $$$n$$$. These galaxies are connected by $$$n - 1$$$ bidirectional wormhole channels, and any galaxy can be reached from any other galaxy via these wormholes.

Each galaxy contains some stars, and the number of stars in the $$$i$$$-th galaxy is $$$s_i$$$.

Pheonix will make $$$q$$$ wormhole trips between galaxies. During a single trip, Pheonix will not visit any galaxy more than once.

Pheonix has limited knowledge of astronomy, but his mathematical observation skills are keen. For each trip, he wants to ask Little T the following questions:

  • Put the numbers of stars of the galaxies visited (including the starting and ending ones) during the wormhole trip from galaxy $$$x$$$ to galaxy $$$y$$$ into a multiset $$$S$$$, i.e., $$$S = \{s_i : i\ \text{is on the path from}\ x \ \text{to}\ y\}$$$.
  • Are there any elements in $$$S$$$ whose frequency of occurrence is strictly greater than $$$\frac{|S|}{k}$$$? Here, $$$k$$$ is an integer specified by Pheonix in each query. If there are, find out what they are.

Please help Little T answer these questions.

Input

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

The first line of each test case contains two integers $$$n$$$ and $$$q$$$ ($$$2 \le n \le 10^5, 1 \le q \le 10^5$$$), representing the number of galaxies and the number of trips Pheonix makes.

The next line contains $$$n$$$ integers $$$s_1$$$, $$$s_2$$$, $$$\ldots$$$, $$$s_n$$$ ($$$1 \le s_i \le n$$$), representing the number of stars each galaxy has respectively.

Each of the following $$$n - 1$$$ lines contains two integers $$$u$$$ and $$$v$$$ ($$$1 \le u, v \le n, u \ne v$$$), representing a wormhole channel connecting $$$u$$$ and $$$v$$$.

Each of the next $$$q$$$ lines contains $$$3$$$ integers $$$x$$$, $$$y$$$, and $$$k$$$ ($$$1 \le x, y \le n, 2 \le k \le n, x \ne y$$$), representing the starting and ending galaxies of a trip, and the query parameter.

It is guaranteed that $$$\sum n, \sum q, \sum k \le 3 \times 10^5$$$ over all test cases. It is also guaranteed that the wormhole channels in each test case allow reaching any galaxy from any other galaxy.

Output

For each test case, output $$$q$$$ lines. The $$$i$$$-th line should contain the answer to the $$$i$$$-th query:

  • If there are elements in $$$S$$$ whose frequency is strictly greater than $$$\frac{|S|}{k}$$$, output these star numbers in strictly increasing order of their values (not by their frequencies).
  • Otherwise, output a single integer $$$-1$$$.
Example
Input
2
6 4
1 2 2 1 2 3
1 2
2 3
2 4
4 5
5 6
3 6 2
1 4 3
1 6 2
1 5 3
7 5
1 2 1 3 2 2 4
1 2
1 3
2 4
2 5
3 6
6 7
4 5 2
4 7 3
5 6 4
3 1 2
7 5 2
Output
2
1
-1
1 2
2
-1
1 2
1
-1
Note

The figure above shows the wormhole connections and the number of stars for the 1st test case.

The 1st query asks about the path from galaxy $$$3$$$ to galaxy $$$6$$$, with the parameter $$$2$$$. The visited galaxies are $$$3 \rightarrow 2 \rightarrow 4 \rightarrow 5 \rightarrow 6$$$, and the numbers of stars are $$$S = \{2, 2, 1, 2, 3\}$$$. The star number that satisfies having a frequency strictly greater than the threshold $$$\frac{|S|}{k} = 2.5$$$ is $$$2$$$.

D. Left & Right
time limit per test
1 second
memory limit per test
1024 megabytes
input
standard input
output
standard output

This problem shares the same background as Problem E Right & Left, and the two problems are closely related. It is recommended to read the text of the other problem first before attempting either of them.

This is an interactive problem.

Little T is having trouble with the subway map of City T...

Specifically, this city is located on both sides of River E. The north bank has $$$n$$$ subway stations $$$N_1$$$, $$$N_2$$$, $$$\ldots$$$, $$$N_n$$$, and the south bank has $$$n$$$ subway stations $$$S_1$$$, $$$S_2$$$, $$$\ldots$$$, $$$S_n$$$.

City T has a total of $$$n - 1$$$ subway lines, each of which is a loop line. The $$$i$$$-th loop line connects the subway stations $$$N_i - N_{i + 1} - S_{i + 1} - S_i - N_i$$$. However, these subway loop lines operate in a single direction. It might be $$$N_i \leftarrow N_{i + 1} \leftarrow S_{i + 1} \leftarrow S_i \leftarrow N_i$$$ or $$$N_i \rightarrow N_{i + 1} \rightarrow S_{i + 1} \rightarrow S_i \rightarrow N_i$$$.

Each loop line has a price list $$$u_i$$$, $$$d_i$$$, $$$l_i$$$, $$$r_i$$$, respectively representing the price of traveling one subway station between the 4 adjacent stations ($$$N_i - N_{i + 1}$$$, $$$S_i - S_{i + 1}$$$, $$$N_i - S_i$$$, and $$$N_{i + 1} - S_{i + 1}$$$). Note that the direction of traveling one subway station depends on the operating direction of the subway, which might be $$$N_i \rightarrow N_{i + 1}$$$ or $$$N_{i} \leftarrow N_{i + 1}$$$.

Unfortunately, a space-time black hole swallowed the operating directions and prices of these loop lines on the price list. Little T plans to seek Big K's help to find out the operating directions of these subways.

Big K knows the true operating directions of the subways. However, Little T can arbitrarily re-assign a set of prices for the four adjacent station edges of each loop line; Big K will answer Little T's queries based on these assigned prices.

Little T can ask him up to $$$3\,500$$$ questions:

  • How much subway fare must be paid at least to travel from subway station $$$x$$$ (can be $$$N_i$$$ or $$$S_i$$$, same below) to subway station $$$y$$$ under the subway price list assigned by Little T?
  • If different loop lines connect the same pair of adjacent stations, these edges exist simultaneously and can all be used. The cost of a trip is the sum of the weights of the edges passed, and there is no extra charge for transfers between stations.

After asking these questions, Little T needs to confirm the operating direction of each subway line with Big K.

Please help Little T solve this problem.

Please note that the directions of the subway lines are predetermined and they will not change during the interaction process. In other words, the interactor is not adaptive.

Interaction

Each test case in this problem contains only one set of test data.

The first line of input contains a single integer $$$n$$$ ($$$2 \le n \le 10^5$$$), representing the parameter, where there are $$$2n$$$ subway stations and $$$n - 1$$$ loop lines in total.

You first need to output $$$n - 1$$$ lines, where the $$$i$$$-th line outputs $$$4$$$ integers $$$u_i$$$, $$$d_i$$$, $$$l_i$$$, $$$r_i$$$, respectively representing the moving prices between the four adjacent stations $$$N_i - N_{i + 1}$$$, $$$S_i - S_{i + 1}$$$, $$$N_i - S_i$$$, and $$$N_{i + 1} - S_{i + 1}$$$ on the $$$i$$$-th loop line. You need to ensure that these four integers are between $$$[0, 10^9]$$$.

Then you will directly start the interaction. For each interaction, you can ask the interactor questions in the following format:

  • Output a line. First, output a character ? indicating you are making a query. Then output $$$4$$$ elements $$$S_x$$$, $$$id_x$$$, $$$S_y$$$, $$$id_y$$$, where you need to ensure $$$S_x, S_y \in \{\mathtt{N}, \mathtt{S}\}$$$, $$$id_x, id_y \in \{m \in \mathbb{N} : 1 \le m \le n\}$$$. This means Little T needs to ask Big K how much subway fare must be paid at least between the $$$id_x$$$-th subway station $$$(S_x)_{id_x}$$$ located on the $$$S_x$$$ bank and the $$$id_y$$$-th subway station $$$(S_y)_{id_y}$$$ located on the $$$S_y$$$ bank.
  • If your input is valid and does not exceed the query limit, the interactor will provide a line with a single integer $$$d$$$ to input, representing the minimum required subway fare between these two stations.
  • If the question you asked exceeds the corresponding query limit, the interactor will output an integer $$$-1$$$ and terminate your interaction process. At this point, you will receive a Wrong Answer verdict.

If you have already obtained the subway directions, please output the answer in the following format:

  • Output a line. First, output a character !. Then output a string $$$S$$$ of length $$$n - 1$$$, where the $$$i$$$-th character $$$S_i \in \{\mathtt{I}, \mathtt{O}\}$$$. If $$$S_i = \mathtt{I}$$$, it indicates the direction of the $$$i$$$-th loop line is $$$N_i \leftarrow N_{i + 1} \leftarrow S_{i + 1} \leftarrow S_i \leftarrow N_i$$$. If $$$S_i = \mathtt{O}$$$, it indicates the direction of the $$$i$$$-th loop line is $$$N_i \rightarrow N_{i + 1} \rightarrow S_{i + 1} \rightarrow S_i \rightarrow N_i$$$.

After completing the output of the answer, you should exit safely.

Every output needs to include a newline and flush the buffer, otherwise you might get unexpected results.

To flush the buffer, you can:

  • For C or C++, use fflush(stdout) or cout.flush().
  • For Java or Kotlin, use System.out.flush().
  • For Python, use stdout.flush().
Example
Input
3
10
12
Output
10 3 1 2
10 10 1 1
? N 1 N 2
? N 2 N 3
! OI
Note

The figure above shows the operating directions of the subways and Little T's price labels.

Please note that the interaction process in the sample is for reference only. The actual interaction process is not unique, and the interaction process in this example is not necessarily feasible or optimal. The sample for this problem will not appear in the additional files.

E. Right & Left
time limit per test
1 second
memory limit per test
1024 megabytes
input
standard input
output
standard output

This problem shares the same background as Problem D Left & Right, and the two problems are closely related. It is recommended to read the text of the other problem first before attempting either of them.

This is not an interactive problem.

Little T is having trouble with the subway map of City T...

Specifically, this city is located on both sides of River E. The north bank has $$$n$$$ subway stations $$$N_1$$$, $$$N_2$$$, $$$\ldots$$$, $$$N_n$$$, and the south bank has $$$n$$$ subway stations $$$S_1$$$, $$$S_2$$$, $$$\ldots$$$, $$$S_n$$$.

City T has a total of $$$n - 1$$$ subway lines, each of which is a loop line. The $$$i$$$-th loop line connects the subway stations $$$N_i - N_{i + 1} - S_{i + 1} - S_i - N_i$$$. However, these subway loop lines operate in a single direction. It might be $$$N_i \leftarrow N_{i + 1} \leftarrow S_{i + 1} \leftarrow S_i \leftarrow N_i$$$ or $$$N_i \rightarrow N_{i + 1} \rightarrow S_{i + 1} \rightarrow S_i \rightarrow N_i$$$.

Each loop line has a price list $$$u_i$$$, $$$d_i$$$, $$$l_i$$$, $$$r_i$$$, respectively representing the price of traveling one subway station between the 4 adjacent stations ($$$N_i - N_{i + 1}$$$, $$$S_i - S_{i + 1}$$$, $$$N_i - S_i$$$, and $$$N_{i + 1} - S_{i + 1}$$$). Note that the direction of traveling one subway station depends on the operating direction of the subway, which might be $$$N_i \rightarrow N_{i + 1}$$$ or $$$N_{i} \leftarrow N_{i + 1}$$$.

Fortunately, a space-time white hole spat out the operating directions and prices of these loop lines on the price lists. Big K plans to seek Little T's help to get the prices for some subway trips.

As a person from another space-time, Big K will tell Little T the operating directions and price lists of these loop lines. Then Little T needs to answer $$$q$$$ questions:

  • How much subway fare must be paid at least to travel by subway from city $$$x$$$ (can be $$$N_i$$$ or $$$S_i$$$, same below) to city $$$y$$$ under the given subway price list?

Please help Little T solve this problem.

Input

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

The first line of each test case contains two integers $$$n$$$ and $$$q$$$ ($$$2 \le n \le 10^5, 1 \le q \le 10^5$$$), representing the parameter for the number of cities and subway lines, and the number of queries.

Each of the following $$$n - 1$$$ lines contains $$$4$$$ integers $$$u_i$$$, $$$d_i$$$, $$$l_i$$$, $$$r_i$$$ ($$$0 \le u_i, d_i, l_i, r_i \le 10^9$$$), respectively representing the moving prices between the four adjacent stations $$$N_i - N_{i + 1}$$$, $$$S_i - S_{i + 1}$$$, $$$N_i - S_i$$$, and $$$N_{i + 1} - S_{i + 1}$$$ on the $$$i$$$-th loop line.

Then, a line containing a string $$$S$$$ of length $$$n - 1$$$ is given, where the $$$i$$$-th character $$$S_i \in \{\mathtt{I}, \mathtt{O}\}$$$. If $$$S_i = \mathtt{I}$$$, it indicates the direction of the $$$i$$$-th loop line is $$$N_i \leftarrow N_{i + 1} \leftarrow S_{i + 1} \leftarrow S_i \leftarrow N_i$$$. If $$$S_i = \mathtt{O}$$$, it indicates the direction of the $$$i$$$-th loop line is $$$N_i \rightarrow N_{i + 1} \rightarrow S_{i + 1} \rightarrow S_i \rightarrow N_i$$$.

Each of the next $$$q$$$ lines contains $$$5$$$ elements $$$S_x$$$, $$$id_x$$$, $$$S_y$$$, $$$id_y$$$ ($$$S_x, S_y \in \{\mathtt{N}, \mathtt{S}\}$$$, $$$id_x, id_y \in \{m \in \mathbb{N} : 1 \le m \le n\}$$$), indicating that Big K asks Little T how much subway fare must be paid at least between the $$$id_x$$$-th city $$$(S_x)_{id_x}$$$ located on the $$$S_x$$$ bank and the $$$id_y$$$-th city $$$(S_y)_{id_y}$$$ located on the $$$S_y$$$ bank.

It is guaranteed that $$$\sum n \le 3 \times 10^5$$$ and $$$\sum q \le 3 \times 10^5$$$ over all test cases.

Output

For each test case, output $$$q$$$ lines. The $$$i$$$-th line should contain an integer $$$d$$$ representing the answer to the $$$i$$$-th query received by Little T, i.e., the minimum required subway fare from $$$(S_x)_{id_x}$$$ to $$$(S_y)_{id_y}$$$.

Example
Input
3
3 5
10 3 1 2
10 10 1 1
OI
N 1 N 2
N 2 N 3
N 2 S 2
S 2 N 2
N 1 N 1
6 1
5 1 7 3
2 6 3 4
6 8 1 7
10 0 7 8
4 5 4 9
IOIOI
N 4 S 4
6 1
4 5 7 6
5 0 9 1
1 5 1 5
10 4 7 1
2 1 3 3
OIOIO
S 6 S 4
Output
10
12
1
14
0
14
17
Note

The figure above shows the operating directions of the subways and the price lists.

F. Melody
time limit per test
1 second
memory limit per test
1024 megabytes
input
standard input
output
standard output

Little T came to City T, and she is going to a Livehouse for a performance.

City T can be represented by an infinite Euclidean 2D plane. The positive direction of the $$$y$$$-axis represents due north (N), the positive direction of the $$$x$$$-axis represents due east (E), the negative direction of the $$$y$$$-axis represents due south (S), and the negative direction of the $$$x$$$-axis represents due west (W).

Little T is initially standing at position $$$(x_{t}, y_{t})$$$, initially facing one of the four directions: due east, south, west, or north. The Livehouse she wants to go to is located at $$$(x_l, y_l)$$$.

For each move, she can choose one of the following two options:

  • Go straight, walk one unit length along the current direction.
  • Turn right, rotate the direction Little T is facing by $$$90^\circ$$$ clockwise (North turns to East, East turns to South, South turns to West, West turns to North).
  • Note that Little T cannot turn left or turn around.

Please calculate, under the above conditions, what is the minimum number of right turns Little T needs to make to reach the Livehouse.

Input

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

For each test case, one line contains 5 elements $$$x_t$$$, $$$y_t$$$, $$$dir$$$, $$$x_l$$$, $$$y_l$$$ ($$$x_t, y_t, x_l, y_l \in \{n \in \mathbb{Z} : -10^9 \le n\le 10^9\}$$$, $$$dir \in \{\text{N}, \text{E}, \text{S}, \text{W}\}$$$), respectively representing Little T's location, the initial direction Little T is facing, and the location of the Livehouse.

Output

For each test case, output a single line with an integer representing the minimum number of right turns Little T needs to make to reach the Livehouse under the above conditions.

Example
Input
8
0 0 E 3 -2
1 1 N 0 1
10 10 E 10 10
-9 2 E 3 2
0 -7 E -1 -4
0 6 E -4 6
10 -9 S 10 9
1 -3 N -1 0
Output
1
3
0
0
3
2
2
3
Note

For the 1st test case, first walk $$$3$$$ steps east to reach $$$(3,0)$$$, then turn right to face south, and walk $$$2$$$ steps south to $$$(3, -2)$$$.

For the 2nd test case, first turn right three times to face west, then walk $$$1$$$ step to reach $$$(0, 1)$$$.

For the 3rd test case, the starting point and the ending point coincide, so no right turn is needed.

G. CS:Go Over
time limit per test
1 second
memory limit per test
1024 megabytes
input
standard input
output
standard output

This problem has no relation with Problem H Morrow by Morrow.

TSUCE (Time-Space Union of Coding Experts) is preparing to host the TSUCE Programming Duel Cup, an algorithmic programming 1v1 duel between two players!

The competition format is as follows. Please note the meanings of $$$n$$$ and $$$m$$$:

  • Each match is played between two players.
  • A match consists of a regular time + several overtime periods (possibly $$$0$$$ overtime periods). The regular time contains $$$2n$$$ rounds, and one overtime period contains $$$2m$$$ rounds. There is exactly one winner in a single round.
  • The player who wins $$$n + 1$$$ rounds during regular time will win the whole match.
  • If there is a tie after the $$$2n$$$ rounds of regular time (score $$$n:n$$$), the match will enter overtime. The player who wins $$$m + 1$$$ rounds in a single overtime period will win the whole match.
  • If there is still a tie after one overtime period, the match will enter the next overtime period.
  • The final score is the number of rounds won by each player.

Unfortunately, the scorekeeper Little T's database broke down. He only remembers the scores of all $$$k$$$ matches, but he forgot $$$n$$$ and $$$m$$$ related to the format.

Please help him determine whether there exists a valid pair of positive integers $$$n$$$ and $$$m$$$ such that all these scores are valid under this format.

Input

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

The first line of each test case contains an integer $$$k$$$ ($$$1 \le k \le 10^5$$$), representing the number of recorded matches.

Each of the following $$$k$$$ lines contains two integers $$$a$$$ and $$$b$$$ ($$$0 \le a, b \le 10^{18}, |a - b| \ge 2$$$), representing the score of a match recorded in Little T's database.

It is guaranteed that $$$\sum k \le 3 \times 10^5$$$ over all test cases.

Output

For each test case, if there exists a valid pair of positive integers $$$n$$$ and $$$m$$$ such that all these scores are valid under this format, output a string YES on a single line; otherwise, output a string NO on a single line.

Note that the evaluation is case-insensitive for YES and NO. In other words, when the answer is positive, outputs like yes, YES, Yes, YeS will all be considered correct.

Example
Input
6
3
16 14
10 16
19 22
2
11 16
19 22
5
2 13
16 12
9 13
13 10
20 22
2
5 7
5 9
3
0 5
5 2
11 7
2
6 9
9 6
Output
YES
YES
YES
NO
YES
YES
Note

For the 3rd test case, we can find that $$$n = 12$$$, $$$m = 3$$$ is a valid solution. The matches will go through the following processes:

  • Match 1 is $$$2:13$$$. Player B takes $$$n + 1 = 13$$$ wins first in regular time to end the match.
  • Match 2 is $$$16:12$$$. The regular time ends in a $$$12:12$$$ tie. In the first overtime period, Player A sweeps the opponent $$$4:0$$$, taking $$$m + 1 = 4$$$ wins.
  • Matches 3 and 4 end in regular time with scores of $$$9:13$$$ and $$$13:10$$$, respectively.
  • Match 5 ends in a $$$12:12$$$ tie in regular time. Both the first and second overtime periods end in a $$$3:3$$$ tie, bringing the match to the third overtime with a score of $$$18:18$$$. Finally, Player B defeats the opponent $$$4:2$$$ in the third overtime, resulting in a final score of A $$$20:22$$$ B.

H. Morrow by Morrow
time limit per test
2 seconds
memory limit per test
1024 megabytes
input
standard input
output
standard output

This problem has no relation with Problem G CS:Go Over.

Little T is preparing some classic problems $$$\ldots$$$

Given a permutation $$$p = \{p_1, p_2, \ldots, p_n\}$$$ of length $$$n$$$, Little T will perform exactly one operation $$$(i, j)$$$:

  • Choose a pair of indices $$$1 \le i \lt j \le n$$$, and swap $$$p_i$$$ and $$$p_j$$$ in $$$p$$$.

For all different valid operations $$$(i, j)$$$, Little T needs to calculate the sum of the orders of the permutations represented by the permutation after the swap.

Since the answer can be very large, Little T needs to calculate the answer modulo $$$998\,244\,353$$$.

Please help Little T calculate this problem.

The hints section of the problem provides related information about permutations.

Input

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

The first line of each test case contains an integer $$$n$$$ ($$$2 \le n \le 10^5$$$), representing the length of the permutation.

The next line contains $$$n$$$ integers $$$p_1$$$, $$$p_2$$$, $$$\ldots$$$, $$$p_n$$$ ($$$1 \le p_i \le n$$$), representing the given permutation.

It is guaranteed that $$$\sum n \le 3 \times 10^5$$$ over all test cases, and the input in each test case is guaranteed to be a valid permutation.

Output

For each test case, output a single line with an integer representing the sum of the orders of the permutations represented by the permutation after the swap for all different valid operations $$$(i, j)$$$, modulo $$$998\,244\,353$$$.

Example
Input
2
3
2 3 1
4
2 1 4 3
Output
6
20
Note

For the 1st test case, there are $$$3$$$ different ways to swap:

  • Swap $$$p_1$$$ and $$$p_2$$$, the permutation becomes $$$\{3, 2, 1\}$$$. Note that $$$p^2 = \text{id}$$$, because $$$p(p(1)) = p(3) = 1$$$, $$$p(p(2)) = p(2) = 2$$$, $$$p(p(3)) = p(1) = 3$$$, so the order is $$$2$$$.
  • Swap $$$p_1$$$ and $$$p_3$$$, the permutation becomes $$$\{1, 3, 2\}$$$. Note that $$$p^2 = \text{id}$$$, so the order is $$$2$$$.
  • Swap $$$p_2$$$ and $$$p_3$$$, the permutation becomes $$$\{2, 1, 3\}$$$. Note that $$$p^2 = \text{id}$$$, so the order is $$$2$$$.

Therefore, the answer is $$$6$$$.

Additional notes

A permutation sequence of $$$1$$$ to $$$n$$$ is a sequence of length $$$n$$$ in which each integer from $$$1$$$ to $$$n$$$ appears exactly once. The length of this permutation sequence is also $$$n$$$.

Let $$$S = \{1, 2, \ldots, n\}$$$. A permutation mapping from $$$1$$$ to $$$n$$$ refers to a bijection from $$$S$$$ to $$$S$$$.

A permutation sequence $$$p = \{p_1, p_2, \ldots, p_n \}$$$ of length $$$n$$$ can represent a permutation mapping $$$p(n)$$$, that is, $$$p(i) = p_i$$$.

For example, $$$p = \{2, 3, 1\}$$$ in the 1st test case represents the permutation mapping $$$p(n): \{1, 2, 3\} \rightarrow \{1, 2, 3\}$$$, where $$$$$$ p(1) = 2, p(2) = 3, p(3) = 1. $$$$$$

The identity permutation of length $$$n$$$, $$$\text{id}_n: \{1, 2, \ldots, n\} \rightarrow \{1, 2, \ldots, n\}$$$, is defined as $$$\text{id}_n(i) = i$$$. For example, an identity permutation of length 4, $$$\text{id}_4$$$, has: $$$$$$ \text{id}_4(1) = 1, \text{id}_4(2) = 2, \text{id}_4(3) = 3, \text{id}_4(4) = 4. $$$$$$

The composition $$$p \circ q$$$ of two permutations $$$p$$$ and $$$q$$$ represents the permutation where $$$p \circ q(i) = p(q(i))$$$.

The power $$$p^m$$$ (where $$$m$$$ is a non-negative integer) of a permutation of length $$$n$$$ is defined as $$$$$$ p^m = \begin{cases} p \circ p^{m - 1}, &m \ge 1;\\ \text{id}_n, & m = 0. \end{cases} $$$$$$

The order $$$\text{ord}(p)$$$ of a permutation of length $$$n$$$ is defined as the smallest positive integer $$$k$$$ such that $$$p^{k} = \text{id}_n$$$.

I. Stardew Valley
time limit per test
1 second
memory limit per test
1024 megabytes
input
standard input
output
standard output

Nailong is addicted to Stardew Valley during his holidays. To complete all achievements, he needs a lot of gold coins to purchase the Gold Clock, so he is currently in desperate need of gold coins.

In the game Stardew Valley, players can grow different crops. By growing crops, the player Nailong can earn the gold coins he needs.

It is now the morning of the $$$1$$$st day of Spring, and Spring lasts for $$$k$$$ days in total. Nailong has some arable plots of land, which are initially empty. He has $$$10^{10^{100}}$$$ gold coins on hand, and now he starts his farm work for the Spring.

Pierre's General Store sells $$$n$$$ types of single-harvest crop seeds and $$$m$$$ types of multi-harvest crop seeds. Their definitions are as follows:

  • Single-harvest crop: the seed price is $$$p$$$ gold coins per unit, the selling price of the mature crop is $$$q$$$ gold coins per unit, and the maturation time is $$$t$$$ days. That is, a crop planted on day $$$i$$$ will become harvestable on day $$$i + t$$$. After harvesting, the crop will be removed, and the plot will become empty.
  • Multi-harvest crop: the seed price is $$$p$$$ gold coins per unit, the selling price of the mature crop is $$$q$$$ gold coins per unit, and the maturation time is $$$t$$$ days. That is, a crop planted on day $$$i$$$ will become harvestable on day $$$i + t$$$. At the same time, this crop has a harvest cycle of $$$s$$$ days. After harvesting, the crop will not be removed, and a crop harvested on day $$$j$$$ will become harvestable again on day $$$j + s$$$.
  • On day $$$k + 1$$$, Spring comes to an end and Summer arrives. All crops in the plots will wither and cannot be harvested, even if they were supposed to mature on that day.

Because Nailong is quite lazy, he will choose to plant at most one type of crop throughout the entire Spring.

Nailong can perform the following operations any number of times each day:

  • Buy seeds from Pierre's General Store.
  • Plant a unit of seed on an empty plot of land.
  • Harvest a unit of crop that is in the "harvestable" state and sell it.
  • Note that you cannot remove existing crops from the land unless it is a single-harvest crop being harvested.

Since all arable plots are equivalent, he wants to know the maximum profit in gold coins that each plot of land can bring him by the end of Spring.

Note that if it is impossible to make a profit no matter what, Nailong can choose to do nothing and gain a profit of $$$0$$$ gold coins by the end of Spring.

Input

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

The first line of each test case contains 3 integers $$$n$$$, $$$m$$$, and $$$k$$$ ($$$1 \le n + m \le 10^5, n \ge 0, m \ge 0, 2 \le k \le 10^9$$$), representing the number of types of the two kinds of seeds and the number of days Spring lasts.

Each of the following $$$n$$$ lines contains 3 integers $$$p$$$, $$$q$$$, and $$$t$$$ ($$$0 \le p , q\le 10^9, 1 \le t \le k - 1$$$), representing the seed price, mature crop selling price, and maturation time of a single-harvest crop.

Each of the following $$$m$$$ lines contains 4 integers $$$p$$$, $$$q$$$, $$$t$$$, and $$$s$$$ ($$$0 \le p , q\le 10^9, 1 \le t, s \le k - 1$$$), representing the seed price, mature crop selling price, maturation time, and harvest cycle of a multi-harvest crop.

It is guaranteed that $$$\sum (n + m) \le 3 \times 10^5$$$ over all test cases.

Output

For each test case, output a single line with an integer representing the maximum profit in gold coins that each plot of land can bring him by the end of Spring.

Example
Input
3
2 1 10
20 50 3
5 7 1
30 15 2 2
0 2 7
10 4 2 1
25 9 1 2
1 1 5
10 8 2
5 1 4 1
Output
90
10
0
Note

For the 1st test case, we have the following crops:

CropTypeSeed PriceCrop Selling PriceMaturation TimeHarvest Cycle
ASingle-harvest20503N/A
BSingle-harvest571N/A
CMulti-harvest301522

We consider choosing Crop A. A feasible plan is as follows:

DayActionDaily Balance
1Buy 3 units of Crop A seeds, and plant 1 unit of Crop A-60
4Harvest and sell 1 harvestable unit of Crop A, and plant 1 unit of Crop A50
7Harvest and sell 1 harvestable unit of Crop A, and plant 1 unit of Crop A50
10Harvest and sell 1 harvestable unit of Crop A50

The total profit is $$$-60+50+50+50 = 90$$$ gold coins.

J. Flame Strike 2
time limit per test
1 second
memory limit per test
1024 megabytes
input
standard input
output
standard output

Nailong is killing monsters in the game. This is a turn-based game, and he encounters $$$n$$$ monsters with health points of $$$a_1$$$, $$$a_2$$$, $$$\ldots$$$, $$$a_n$$$ respectively.

Each turn, Nailong can cast one of the following skills once:

  • Fireball: Select a monster, deal $$$x$$$ damage to it, and consume $$$a$$$ mana points.
  • Flamestrike: Deal $$$y$$$ damage to all monsters, and consume $$$b$$$ mana points.

When a monster takes damage, an equal amount of health points will be deducted. If its health points are less than or equal to $$$0$$$, the monster will be killed. Nailong wins the battle when all $$$n$$$ monsters are killed.

Please help Nailong plan his skill usage to minimize the mana consumed while winning the battle, and calculate the minimum mana required.

Input

Each test case in this problem contains only one set of test data.

The first line contains 5 integers $$$n$$$, $$$x$$$, $$$y$$$, $$$a$$$, $$$b$$$ ($$$1 \le n, x, y, a, b \le 2 \times 10^6$$$), respectively representing the number of monsters, the damage of Fireball and Flame Strike, and their mana consumption.

The next line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, $$$\ldots$$$, $$$a_n$$$ ($$$1 \le a_i \le 10^7$$$), representing the health points of the monsters.

Output

Output a single line with an integer, representing the minimum mana Nailong needs to consume to win the battle.

Examples
Input
4 10 8 5 7
9 17 23 40
Output
31
Input
5 3 1 2 5
1 2 3 4 10
Output
17
Note

For the first sample, A feasible skill usage plan for Nailong is to use Flamestrike 7 times, and then use Fireball once on the last monster.

This way, the other monsters take $$$7 \times 3 = 21$$$ damage, and the last monster takes $$$10 + 7 \times 3 = 31$$$ damage, so they are all killed.

The mana consumption is $$$7 \times 4 + 6 \times 1 = 34$$$ points, and it can be proven that this is optimal.

K. In Filtration 2
time limit per test
2 seconds
memory limit per test
1024 megabytes
input
standard input
output
standard output

Maddy is still purging Baddy's evil legion...

The game is played on an infinite Chinese Chess (Xiangqi) board, which is formed by the intersection of infinitely many horizontal and vertical lines. Each intersection can hold one chess piece. For ease of description, we abstract it as a 2D Cartesian coordinate system to represent the positions of the pieces.

Baddy controls $$$n$$$ black pawn pieces and one black general piece. These pieces are fixed at their initial positions during the game and will not move. The black pawns are deployed at the intersections $$$(x_1, y_1)$$$, $$$(x_2, y_2)$$$, $$$\ldots$$$, $$$(x_n, y_n)$$$, and the black general is located at $$$(x_\text{K}, y_\text{K})$$$.

Now Maddy controls a red cannon piece. She can deploy the red cannon anywhere on the board. The red cannon follows the movement rules of Chinese Chess. Specifically:

  • Move: Each move, it can travel any number of intersections along the horizontal or vertical line it is currently on. However, a single move must be strictly horizontal or strictly vertical; it cannot move in both directions at once. It cannot jump over other pieces on the same line during a normal move, nor can it capture a piece this way.
  • Capture: It can choose an enemy piece on the same line (horizontal or vertical) as the target, but there must be exactly one piece (of any color) between them. After capturing, the cannon moves to the position of the captured enemy piece, and the captured piece is removed from the board.

After deploying the red cannon, Maddy can continuously make an infinite number of moves following the rules above.

Maddy now wants to know: is there a valid deployment strategy and a sequence of moves for the red cannon that can capture the black general? Please help her.

Input

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

The first line of each test case contains 3 integers $$$n$$$, $$$x_\text{K}$$$, and $$$y_\text{K}$$$ ($$$0 \le n \le 10^5, -10^9 \le x_\text{K}, y_\text{K} \le 10^9$$$), representing the number of black pawns and the coordinates of the black general.

Each of the following $$$n$$$ lines contains two integers $$$x_i$$$ and $$$y_i$$$ ($$$-10^9 \le x_i, y_i \le 10^9$$$), representing the position of a black pawn.

It is guaranteed that $$$\sum n \le 3 \times 10^5$$$ over all test cases.

Output

For each test case, if there is a valid deployment strategy and a sequence of moves for the red cannon to capture the black general, output a string YES on a single line; otherwise, output a string NO on a single line.

Note that the evaluation is case-insensitive for YES and NO. In other words, when the answer is positive, outputs like yes, YES, Yes, YeS will all be considered correct.

Example
Input
3
0 0 0
1 1 1
0 1
4 1 0
0 1
2 1
1 1
1 2
Output
NO
YES
NO
Note

The figure below shows the positions of the black pieces for the 2nd test case and one possible deployment position for Maddy's red cannon. By deploying it at $$$(-2, -1)$$$, she can directly capture the black general.

The figure below shows the positions of the black pieces for the 3rd test case.

Please note that for demonstration purposes, the sample figures show the board boundaries, but in fact, the problem description has pointed out that the board is infinite.

Here in the picture, Chinese character "将" means general, "卒" means pawn, "炮" means cannon.

L. Haitei Raoyue
time limit per test
2 seconds
memory limit per test
1024 megabytes
input
standard input
output
standard output

TSUCE's final exams are over! The grading teacher, Big K, looked at Little T's horrible exam paper.

Therefore, he decided to use the following tricks to save Little T, who scored $$$x$$$ points:

  • Operation A, this trick can be used at most $$$a_1$$$ times. It takes the square root of Little T's current score $$$x$$$, multiplies it by 10, and rounds down, i.e., $$$x \leftarrow \lfloor 10 \sqrt{x} \rfloor$$$.
  • Operation B, this trick can be used at most $$$a_2$$$ times. It multiplies Little T's current score $$$x$$$ by $$$0.7$$$, adds 30, and rounds down, i.e., $$$x \leftarrow \lfloor 0.7x + 30 \rfloor$$$.
  • Operation C, this trick can be used at most $$$a_3$$$ times. It multiplies Little T's current score $$$x$$$ by $$$1.2$$$ and rounds down, i.e., $$$x \leftarrow \lfloor 1.2x \rfloor$$$.
  • Operation D, this trick can be used at most $$$a_4$$$ times. It adds $$$5$$$ to Little T's current score $$$x$$$, i.e., $$$x \leftarrow x + 5$$$.

Please help Big K find a valid way to use these tricks so that Little T can obtain the maximum possible score.

Input

The first line contains a single integer $$$T$$$ ($$$1 \le T \le 3 \times 10^5$$$), representing the number of test cases.

Each test case consists of a single line containing 5 integers $$$x$$$, $$$a_1$$$, $$$a_2$$$, $$$a_3$$$, $$$a_4$$$ ($$$1 \le x \le 100, 0 \le a_1, a_2, a_3, a_4 \le 400$$$), representing Little T's initial score and the maximum number of times Big K can use each trick.

Output

For each test case, output a single line with an integer representing the maximum score Little T can obtain.

Example
Input
7
1 1 1 1 1
50 2 1 1 2
99 0 1 2 1
10 2 0 0 3
100 400 400 400 400
75 1 7 3 0
1 3 7 4 3
Output
70
118
148
71
98703086085238798121046930830507710
168
232
Note

For the 1st test case, Little T initially scored $$$1$$$ point, and Big K can use each trick at most once. Big K can adopt the following strategy:

  • First, use Operation B. Little T's score becomes $$$\lfloor 0.7x+30 \rfloor = \lfloor 0.7 \times 1 +30 \rfloor = 30$$$ points.
  • Next, use Operation A,. Little T's score becomes $$$\lfloor 10\sqrt{x} \rfloor = \lfloor 10\sqrt{30} \rfloor = 54$$$ points.
  • Next, use Operation D. Little T's score becomes $$$54 + 5 = 59$$$ points.
  • Next, use Operation C. Little T's score becomes $$$\lfloor 1.2x \rfloor = \lfloor 1.2 \times 59 \rfloor = 70$$$ points.