BSUIR Open XIII: Student final
A. Tokens on a Graph
time limit per test
4 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given a graph consisting of $$$n$$$ vertices numbered with integers from $$$1$$$ to $$$n$$$, connected by $$$n+m$$$ edges. The first $$$n$$$ edges connect neighboring vertices, meaning the $$$i$$$-th edge connects the $$$i$$$-th and $$$(i+1)$$$-th vertices, and the $$$n$$$-th edge connects the $$$n$$$-th and $$$1$$$-st vertices. In some vertices, there are $$$n-2$$$ tokens numbered with integers from $$$1$$$ to $$$n-2$$$, and no vertex can have more than one token.

In one move, you are allowed to move any token to a vertex connected by an edge to its current vertex, provided that there is currently no token in that vertex. For each token, you know which vertex it should end up in after all moves are completed.

Your task is to determine the sequence of moves so that the tokens end up in the required positions. Note that minimizing the number of moves is not required.

Input

Each test consists of several sets of input data. The first line contains a single integer $$$t$$$ ($$$1 \le t \le 20$$$) — the number of sets of input data. The description of the sets of input data follows.

In the first line of each set of input data, two integers $$$n$$$ and $$$m$$$ ($$$4 \le n \le 100$$$, $$$1 \le m \le 2\,000$$$) are given — the number of vertices and additional edges, respectively.

The second line contains $$$n-2$$$ integers $$$a_i$$$ ($$$1 \le a_i \le n$$$, $$$a_i \ne a_j$$$ for $$$i \ne j$$$), where the $$$i$$$-th number indicates the initial position of the $$$i$$$-th token.

The third line contains $$$n-2$$$ integers $$$b_i$$$ ($$$1 \le b_i \le n$$$, $$$b_i \ne b_j$$$ for $$$i \ne j$$$), where the $$$i$$$-th number indicates the required position of the $$$i$$$-th token.

In the following $$$m$$$ lines, two integers $$$u_i$$$ and $$$v_i$$$ ($$$1 \le u_i, v_i \le n$$$, $$$u_i \ne v_i$$$) are given — the numbers of the vertices connected by an edge. It is guaranteed that the resulting graph does not contain multiple edges or self-loops.

It is guaranteed that the sum of $$$n$$$ across all sets of input data does not exceed $$$100$$$.

Output

For each set of input data, output a single integer $$$k$$$ ($$$k \ge 0$$$) in the first line — the number of moves. In the following $$$k$$$ lines, output two integers $$$c_i$$$ and $$$p_i$$$ ($$$1 \le c_i \le n - 2$$$, $$$1 \le p_i \le n$$$) — the token number and the vertex to which it should be moved on the $$$i$$$-th move, respectively.

The total number of moves across all sets of input data must not exceed $$$2 \cdot 10^6$$$. It is guaranteed that such an answer always exists.

Example
Input
1
4 1
1 3
2 1
2 4
Output
3
2 4
1 2
2 1

B. Lost Luggage
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

As is known, the airline "Trouble" often loses luggage, and concerned journalists decided to calculate the maximum number of luggage pieces that may not return to travelers.

The airline "Trouble" operates flights between $$$n$$$ airports, numbered from $$$1$$$ to $$$n$$$. The journalists' experiment will last for $$$m$$$ days. It is known that at midnight before the first day of the experiment, there were $$$s_j$$$ lost pieces of luggage in the $$$j$$$-th airport. On the $$$i$$$-th day, the following occurs:

  • In the morning, $$$2n$$$ flights take off simultaneously, including $$$n$$$ flights of the first type and $$$n$$$ flights of the second type.
    • The $$$j$$$-th flight of the first type flies from airport $$$j$$$ to airport $$$(((j-2) \bmod n )+ 1)$$$ (the previous airport, with the first airport being the last), and it can carry no more than $$$a_{i,j}$$$ lost pieces of luggage.
    • The $$$j$$$-th flight of the second type flies from airport $$$j$$$ to airport $$$((j \bmod n) + 1)$$$ (the next airport, with the last airport being the first), and it can carry no more than $$$c_{i,j}$$$ lost pieces of luggage.
  • In the afternoon, a check of lost luggage is conducted at the airports. If after the flights have departed on that day, there are $$$x$$$ pieces of luggage remaining in the $$$j$$$-th airport and $$$x \ge b_{i, j}$$$, then at least $$$x - b_{i, j}$$$ pieces of luggage are found, and they cease to be lost.
  • In the evening, all $$$2n$$$ flights conclude, and the lost luggage transported that day arrives at the corresponding airports.

For each $$$k$$$ from $$$1$$$ to $$$m$$$, the journalists want to know the maximum number of lost pieces of luggage that may be unfound during the checks over the first $$$k$$$ days. Note that for each $$$k$$$, these values are calculated independently.

Input

Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). The description of the test cases follows.

The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$3 \le n \le 12$$$, $$$1 \le m \le 2000$$$) — the number of airports and the number of days of the experiment.

The second line of each test case contains $$$n$$$ integers $$$s_1, s_2, \ldots, s_n$$$ ($$$0 \le s_i \le 10^8$$$) — the initial number of lost pieces of luggage in each airport.

Next, the descriptions for each of the $$$m$$$ days follow in order.

The first line of the description of the $$$i$$$-th day contains $$$n$$$ integers $$$a_{i,1}, a_{i,2}, \ldots, a_{i,n}$$$ ($$$0 \le a_{i, j} \le 10^8$$$) — the maximum number of lost pieces of luggage that can be transported to the previous airport for each airport.

The second line of the description of the $$$i$$$-th day contains $$$n$$$ integers $$$b_{i,1}, \ldots, b_{i,n}$$$ ($$$0 \le b_{i, j} \le 10^8$$$) — the minimum number of lost pieces of luggage that will be found on the $$$i$$$-th day in each airport.

The third line of the description of the $$$i$$$-th day contains $$$n$$$ integers $$$c_{i,1}, \ldots, c_{i,n}$$$ ($$$0 \le c_{i, j} \le 10^8$$$) — the maximum number of lost pieces of luggage that can be transported to the next airport for each airport.

It is guaranteed that the sum of $$$m$$$ over all test cases does not exceed $$$2000$$$.

Output

For each test case, output $$$m$$$ integers — the maximum number of unfound pieces of luggage for each number of days from $$$1$$$ to $$$m$$$.

Example
Input
2
5 3
1 1 1 1 1
0 0 1 0 0
0 1 0 0 1
1 0 0 1 0
0 1 0 0 0
9 0 9 9 9
0 1 0 0 0
0 0 0 0 0
9 0 9 0 0
0 0 0 0 0
3 1
0 100000000 5
0 100000000 5
0 100000000 5
0 100000000 5
Output
5
4
2
100000005
Note

In the first test case:

  • On the first day, all $$$5$$$ pieces of luggage may not be found, as the lost luggage can be sent on flights from each airport.
  • In the morning of the second day, there may be no more than $$$3$$$ pieces of luggage in the $$$2$$$-nd airport, no more than $$$2$$$ pieces in the $$$5$$$-th airport, and no luggage in the other airports. All luggage from the $$$5$$$-th airport may remain there. In the $$$2$$$-nd airport, no more than $$$2$$$ pieces of luggage can be sent on flights to neighboring airports. Thus, at least $$$1$$$ piece of luggage will be found.
  • By the end of the third day, lost luggage may only be in the $$$1$$$-st and $$$2$$$-nd airports. There can be no more than one piece in each, meaning that at most $$$2$$$ pieces of luggage will remain unfound in total.
The found luggage is marked in green.

In the second test case, all pieces of luggage may remain in their original airports, and the inspection won't find any lost suitcases. Therefore, the answer is $$$10^9 + 5$$$.

C. Clearing the Snowdrift
time limit per test
2 seconds
memory limit per test
1024 megabytes
input
standard input
output
standard output

Boy Vasya loves to travel very much. In particular, flying in airplanes brings him extraordinary pleasure. He was about to fly to another city, but the runway was heavily covered with snow and needed to be cleared.

The runway can be represented as $$$n$$$ consecutive sections numbered from $$$1$$$ to $$$n$$$. The snowstorm was quite strong, but it has already stopped, so Vasya managed to calculate that the $$$i$$$-th section is covered with $$$a_i$$$ meters of snow. For such situations, the airport has a snowplow that works in a rather unusual way. In one minute, the snowplow can do the following:

  • Choose a consecutive segment of length no more than $$$d$$$ and remove one meter of snow from the most snow-covered sections.

    Formally, one can choose $$$1 \le l \le r \le n$$$ ($$$r - l + 1 \le d$$$). After that, $$$c = \max \{ a_l, a_{l + 1}, \ldots , a_r \}$$$ is calculated, and if $$$c \gt 0$$$, then for all $$$i \colon l \le i \le r$$$ such that $$$a_i = c$$$, the value of $$$a_i$$$ is decreased by one.

Vasya has been preparing for the flight for a long time and wants to understand how much time he has left to wait until all sections are completely cleared of snow. In other words, it is required to calculate the minimum number of minutes that the snowplow will need to achieve $$$a_i = 0$$$ for all $$$i$$$ from $$$1$$$ to $$$n$$$.

Input

Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 2 \cdot 10^5$$$). The description of the test cases follows.

The first line of each test case contains two integers $$$n$$$ and $$$d$$$ ($$$1 \le n \le 5 \cdot 10^5, 1 \le d \le n$$$) — the number of sections on the runway and the maximum length of the segment that the snowplow can choose.

The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 10^9$$$), where $$$a_i$$$ is the number of meters of snow on the $$$i$$$-th section.

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

Output

For each test case, output a single integer — the minimum number of minutes required for the snowplow to achieve $$$a_i = 0$$$ for all $$$i$$$ from $$$1$$$ to $$$n$$$.

Example
Input
2
5 2
1 5 2 1 2
3 1
1000000000 1000000000 1000000000
Output
8
3000000000
Note

In the first test case, there is an optimal sequence of operations. First, select the segment $$$[2, 3]$$$ four times. After three operations, $$$a_2$$$ will turn into $$$2$$$, and the array $$$a$$$ will look like $$$[1, 2, 2, 1, 2]$$$. After the fourth operation, the array $$$a$$$ will become $$$[1, 1, 1, 1, 2]$$$. Next, the array can be transformed into zeros by selecting the segments $$$[1, 2]$$$, $$$[3, 3]$$$, $$$[5, 5]$$$, and $$$[4, 5]$$$ (in that exact order).

In the second test case, $$$d = 1$$$, which means that each section is cleared independently of the others, and the answer is equal to the sum of all $$$a_i$$$.

D. Sasha and the Apartment Purchase
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Sasha wants to buy an apartment on a street where the houses are numbered from $$$1$$$ to $$$10^9$$$ from left to right.

There are $$$n$$$ bars on this street, located in houses with numbers $$$a_1, a_2, \ldots, a_n$$$. Note that there might be multiple bars in the same house, and in this case, these bars are considered distinct.

Sasha is afraid that by the time he buys the apartment, some bars may close, but no more than $$$k$$$ bars can close.

For any house with number $$$x$$$, define $$$f(x)$$$ as the sum of $$$|x - y|$$$ over all open bars $$$y$$$ (that is, after closing some bars).

Sasha can potentially buy an apartment in a house with number $$$x$$$ (where $$$1 \le x \le 10^9$$$) if and only if it is possible to close at most $$$k$$$ bars so that after that $$$f(x)$$$ becomes minimal among all houses.

Determine how many different houses Sasha can potentially buy an apartment in.

Input

Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). The description of the test cases follows.

The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \leq n \leq 10^5, 0 \leq k \lt n$$$) — the number of bars and the maximum number of bars that can close.

The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 10^9$$$) — the house numbers where the bars are located.

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

Output

For each test case, output a single integer — the number of houses where Sasha can buy an apartment.

Example
Input
4
4 0
1 2 3 4
5 2
7 6 6 7 1
3 1
6 7 9
6 2
5 1 9 10 13 2
Output
2
2
4
9
Note

In the first test case, none of the bars can close, so only houses numbered $$$2$$$ and $$$3$$$ are suitable. For the house numbered $$$2$$$, the sum of distances is $$$|2 - 1| + |2 - 2| + |2 - 3| + |2 - 4| = 4$$$, and for the house numbered $$$3$$$, the sum of distances is $$$|3 - 1| + |3 - 2| + |3 - 3| + |3 - 4| = 4$$$. However, for the house numbered $$$1$$$, the sum of distances will be $$$|1 - 1| + |1 - 2| + |1 - 3| + |1 - 4| = 6$$$, so the house numbered $$$1$$$ is not suitable. It can also be proven that Sasha cannot buy apartments in other houses.

In the second test case, the suitable houses are numbered $$$6$$$ and $$$7$$$. For Sasha to choose the house numbered $$$6$$$, it is sufficient that none of the bars close. For Sasha to choose the house numbered $$$7$$$, the bars in houses $$$1$$$ and $$$6$$$ can close. Then the bars will be located in houses numbered $$$6$$$, $$$7$$$, and $$$7$$$.

E. Baggage Claim
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Every airport has a baggage claim area, and Balbesovo Airport is no exception. At some point, one of the administrators at Sheremetyevo came up with an unusual idea: to change the traditional shape of the baggage claim conveyor from a carousel to a more complex form.

Suppose that the baggage claim area is represented as a rectangular grid of size $$$n \times m$$$. The administration proposed that the path of the conveyor should pass through the cells $$$p_1, p_2, \ldots, p_{2k+1}$$$, where $$$p_i = (x_i, y_i)$$$.

For each cell $$$p_i$$$ and the next cell $$$p_{i+1}$$$ (where $$$1 \leq i \leq 2k$$$), these cells must share a common side. Additionally, the path must be simple, meaning that for no pair of indices $$$i \neq j$$$ should the cells $$$p_i$$$ and $$$p_j$$$ coincide.

Unfortunately, the route plan was accidentally spoiled by spilled coffee, and only the cells with odd indices of the path were preserved: $$$p_1, p_3, p_5, \ldots, p_{2k+1}$$$. Your task is to determine the number of ways to restore the original complete path $$$p_1, p_2, \ldots, p_{2k+1}$$$ given these $$$k+1$$$ cells.

Since the answer can be very large, output it modulo $$$10^9+7$$$.

Input

Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 3 \cdot 10^4$$$). The description of the test cases follows.

The first line of each test case contains three integers $$$n$$$, $$$m$$$, and $$$k$$$ ($$$1 \le n, m \le 1000$$$, $$$n \cdot m \ge 3$$$, $$$1 \le k \le \left\lfloor \frac12 (n m - 1) \right\rfloor$$$) — the dimensions of the grid and a parameter defining the length of the path.

Next, there are $$$k+1$$$ lines, the $$$i$$$-th of which contains two integers $$$x_{2i-1}$$$ and $$$y_{2i-1}$$$ ($$$1 \le x_{2i-1} \le n$$$, $$$1 \le y_{2i-1} \le m$$$) — the coordinates of the cell $$$p_{2i-1}$$$ that lies on the path.

It is guaranteed that all pairs $$$(x_{2i-1}, y_{2i-1})$$$ are distinct.

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

Output

For each test case, output a single integer — the number of ways to restore the original complete path modulo $$$10^9+7$$$.

Example
Input
5
2 4 2
1 1
2 2
2 4
1 4 1
1 1
1 4
5 5 11
2 5
3 4
4 5
5 4
4 3
5 2
4 1
3 2
2 1
1 2
2 3
1 4
3 4 4
1 2
2 1
3 2
2 3
3 4
3 3 2
2 2
1 1
1 3
Output
2
0
2
5
1
Note

In the first test case, there are two possible paths:

  • $$$(1,1) \to (2,1) \to (2, 2) \to (2, 3) \to (2, 4)$$$
  • $$$(1,1) \to (1,2) \to (2, 2) \to (2, 3) \to (2, 4)$$$

In the second test case, there is no suitable path, as the cells $$$(1,1)$$$ and $$$(1,4)$$$ do not have a common neighboring cell.

F. Sports Betting
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

The boarding process for various flights can occur in different ways: either by bus or through a telescopic jet bridge. Every day, exactly one flight is made from St. Petersburg to Minsk, and Vadim decided to demonstrate to the students that he always knows in advance how the boarding will take place.

Vadim made a bet with $$$n$$$ students, and with the $$$i$$$-th student, he made a bet on day $$$a_i$$$. Vadim wins the bet if he correctly predicts the boarding process on both day $$$a_i+1$$$ and day $$$a_i+2$$$.

Although Vadim does not know in advance how the boarding will occur, he really wants to win the bet at least with one student and convince him of his predictive abilities. Check if there exists a strategy for Vadim that allows him to guarantee success.

Input

Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). The description of the test cases follows.

The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the number of students Vadim made bets with.

The second line of each test case contains $$$n$$$ integers $$$a_1, \ldots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the days on which Vadim made bets with the students.

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

Output

For each test case, output "Yes" (without quotes) if Vadim can guarantee convincing at least one student, and "No" otherwise.

You can output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses.

Example
Input
5
4
1 1 1 1
3
2 2 2
5
2 4 3 2 4
8
6 3 1 1 5 1 2 6
1
1000000000
Output
Yes
No
Yes
No
No
Note

In the first test case, Vadim needs to make at least one correct prediction about the boarding process on the second and third days. There are a total of $$$4$$$ possible boarding scenarios for these days, so Vadim can give all $$$4$$$ students different predictions and guarantee that at least one of them will be correct.

In the second test case, Vadim only made bets with three students and cannot guarantee that he will provide at least one of them with a correct prediction.

G. Homework
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Some teachers work at the educational center "Sirius" while simultaneously studying at the university. In this case, the trip does not exempt them from completing their homework, so they do their homework right on the plane. Artem is one of those teachers, and he was assigned the following homework at the university.

With an arbitrary string $$$a$$$ of even length $$$m$$$, he can perform the following operation. Artem splits the string $$$a$$$ into two halves $$$x$$$ and $$$y$$$ of equal length, after which he performs exactly one of three actions:

  • For each $$$i \in \left\{ 1, 2, \ldots, \frac{m}{2}\right\}$$$ assign $$$x_i = (x_i + y_i) \bmod 2$$$;
  • For each $$$i \in \left\{ 1, 2, \ldots, \frac{m}{2}\right\}$$$ assign $$$y_i = (x_i + y_i) \bmod 2$$$;
  • Perform an arbitrary number of operations (the same operations defined above, applied recursively) on the strings $$$x$$$ and $$$y$$$, independently of each other. Note that in this case, the strings $$$x$$$ and $$$y$$$ must be of even length.
After that, the string $$$a$$$ is replaced by the strings $$$x$$$ and $$$y$$$, concatenated in the same order.

Unfortunately, Artem fell asleep on the plane, so you will have to complete his homework. Artem has two binary strings $$$s$$$ and $$$t$$$ of length $$$n$$$, each consisting of $$$n$$$ characters 0 or 1. Determine whether it is possible to make string $$$s$$$ equal to string $$$t$$$ with an arbitrary number of operations.

Input

Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^5$$$). The description of the test cases follows.

The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 10^6$$$) — the length of the strings $$$s$$$ and $$$t$$$.

The second line of each test case contains the string $$$s$$$ of length $$$n$$$, consisting only of characters 0 and 1.

The third line of each test case contains the string $$$t$$$ of length $$$n$$$, consisting only of characters 0 and 1.

It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^6$$$.

Output

For each test case, output "Yes" (without quotes) if it is possible to make string $$$s$$$ equal to string $$$t$$$, and "No" otherwise.

You can output each letter in any case (lowercase or uppercase). For example, the strings "yEs", "yes", "Yes", and "YES" will be accepted as a positive answer.

Example
Input
3
8
00001001
10101001
8
00000000
00001001
6
010110
100010
Output
Yes
No
Yes
Note

In the first test case, the string 00001001 can be transformed into the string 10101001 in two operations. The sequence of actions is illustrated in the figure below:

In the second test case, the string 00000000 cannot be transformed into any string other than 00000000, as no non-zero elements can be formed during any operation.

H. Vadim's Collection
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

We call a phone number a beautiful if it is a string of $$$10$$$ digits, where the $$$i$$$-th digit from the left is at least $$$10 - i$$$. That is, the first digit must be at least $$$9$$$, the second at least $$$8$$$, $$$\ldots$$$, with the last digit being at least $$$0$$$.

For example, 9988776655 is a beautiful phone number, while 9099999999 is not, since the second digit, which is $$$0$$$, is less than $$$8$$$.

Vadim has a beautiful phone number. He wants to rearrange its digits in such a way that the result is the smallest possible beautiful phone number. Help Vadim solve this problem.

Please note that the phone numbers are compared as integers.

Input

Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). The description of the test cases follows.

The only line of each test case contains a single string $$$s$$$ of length $$$10$$$, consisting of digits. It is guaranteed that $$$s$$$ is a beautiful phone number.

Output

For each test case, output a single string of length $$$10$$$ — the smallest possible beautiful phone number that Vadim can obtain.

Example
Input
4
9999999999
9988776655
9988776650
9899999999
Output
9999999999
9876556789
9876567890
9899999999
Note

In the first test case, for the first phone number 9999999999, regardless of the rearrangement of digits, the same phone number is obtained.

In the second test case, for the phone number 9988776655, it can be proven that 9876556789 is the smallest phone number that can be obtained by rearranging the digits.

I. B'obr
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

You were flying in an airplane over the Vitebsk region and suddenly realized that you missed the beaver. You jumped out of the plane with a parachute and landed at point $$$A = (x_1, y_1)$$$, knowing for sure that your faithful friend the beaver is at point $$$B = (x_2, y_2)$$$.

There is also a lake in the forest, which is a circle with radius $$$r$$$, centered at the origin $$$(0, 0)$$$. You can move to any point in the forest (including swimming across the lake) at a speed of $$$1$$$, while along the lake, the beaver has built a dam, which allows you to move at a speed of $$$v$$$.

You are interested in the minimum amount of time it will take you to reach the beaver.

Input

The first line contains the number $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases.

Each test case is described by a single line containing the integers $$$x_1$$$, $$$y_1$$$, $$$x_2$$$, $$$y_2$$$, $$$r$$$, and $$$v$$$ ($$$|x_i|, |y_i| \le 10^9$$$, $$$1 \le r, v \le 10^9$$$). It is guaranteed that $$$x_1^2 + y_1^2 \ge r^2$$$ and $$$x_2^2 + y_2^2 \ge r^2$$$.

Output

For each test case, output a single number — the minimum time it takes to reach the beaver.

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

Example
Input
3
-4 0 4 0 3 3
-4 0 4 0 3 1000000000
-1 10 1 10 8 1000
Output
5.056391787888
2.000000009425
2.000000000000

J. Problematic Paths
time limit per test
2 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

There is a directed graph with $$$n$$$ vertices and $$$m$$$ edges, as well as $$$k$$$ arrays $$$a_1, a_2, \ldots, a_k$$$.

A good path from vertex $$$1$$$ to vertex $$$n$$$ of length $$$s$$$ is defined as a sequence of vertices $$$p_1, p_2, \ldots, p_s$$$ ($$$1 \le s \le n$$$) that satisfies the following conditions:

  • $$$1 = p_1 \lt p_2 \lt \ldots \lt p_{s-1} \lt p_s = n$$$; for any $$$i \in \{1,2,\ldots,s-1\}$$$, there is an edge from vertex $$$p_i$$$ to vertex $$$p_{i+1}$$$.
  • For no pair $$$1 \le l \le r \le s$$$ and index $$$j \in \{1,2,\ldots,k\}$$$, the array $$$p_l, p_{l+1}, \ldots, p_r$$$ isn't the same as the array $$$a_j$$$.

Your task is to compute the number of good paths modulo $$$10^9+7$$$.

Input

Each test consists of several test cases. The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^5$$$) — the number of test cases. The following describes the test cases.

The first line of each test case contains three integers $$$n$$$, $$$m$$$, and $$$k$$$ ($$$2 \le n \le 3 \cdot 10^5$$$, $$$1 \le m, k \le 3 \cdot 10^5$$$) — the number of vertices and edges in the graph and the number of arrays.

Next, there are $$$m$$$ lines, the $$$i$$$-th of which contains two integers $$$u_i$$$ and $$$v_i$$$ ($$$1 \le u_i \lt v_i \le n$$$) — the start and end of the $$$i$$$-th edge.

Next, there are $$$k$$$ lines, the $$$i$$$-th of which contains an integer $$$l_i$$$ and $$$l_i$$$ integers $$$a_{i,1}, \ldots, a_{i,l_i}$$$ ($$$1 \le l_i \le n$$$, $$$1 \le a_1 \lt a_2 \lt \ldots \lt a_{l_i} \le n$$$) — the length of the array $$$a_i$$$ and its elements.

It is guaranteed that the sum of $$$n$$$, the sum of $$$m$$$, and the sum of $$$k$$$ across all test cases do not exceed $$$3 \cdot 10^5$$$.

It is guaranteed that the sum of $$$l_i$$$ across all test cases does not exceed $$$3 \cdot 10^5$$$.

Output

For each test case, output a single integer — the number of good paths modulo $$$10^9 + 7$$$.

Example
Input
3
5 7 4
1 2
2 3
3 4
4 5
1 3
2 4
3 5
2 1 2
2 2 3
2 3 4
2 4 5
5 7 3
1 2
2 3
3 4
4 5
1 3
2 4
3 5
3 1 2 3
3 2 3 4
3 3 4 5
6 9 4
1 2
2 3
3 4
4 5
5 6
1 3
2 4
3 5
4 6
3 1 2 3
3 2 3 4
3 3 4 5
3 4 5 6
Output
1
2
3
Note

In the first example, the only good path is $$$1 \to 3 \to 5$$$.

In the second example, the only good paths are $$$1 \to 2 \to 4 \to 5$$$ and $$$1 \to 3 \to 5$$$.

In the third example, the good paths are $$$1 \to 2 \to 4 \to 6$$$, $$$1 \to 3 \to 4 \to 6$$$, and $$$1 \to 3 \to 5 \to 6$$$.

K. Test Task
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Vadim has long dreamed of finding a well-paid job, and then he learned that the airline "Beda" is looking for a web development engineer. However, it is rumored that this company gives very difficult tasks during interviews, so Vadim decided to send his student on a reconnaissance mission.

It turned out that during the interview, candidates are offered to solve a puzzle. On one table of size $$$n_1 \times m_1$$$, there are holes drilled, and on another table of size $$$n_2 \times m_2$$$, there are figures that represent prisms with a special sensor on the bottom.

The candidate's task is to fill each hole with one of the figures lying on the table. The additional complexity is that each hole must be completely filled, meaning it must match the shape of the figure placed in it. The correctness of the candidate's solution is checked automatically using special sensors, so the figures cannot be turned upside down. It is allowed not to use some of the figures.

The student was unable to complete the task on his own, but he managed to take two photographs of the tables from the task. Now Vadim has photographs of the task, but he, like his student, also could not complete the task. Therefore, Vadim began to suspect whether the task can be solved at all.

To verify his hypothesis, he sent you the photographs of the task and asked for your help.

Input

The first line contains an integer $$$t$$$ ($$$1 \le t \le 1\,000$$$) — the number of test cases. The description of the test cases follows.

The first line of each test case contains a pair of numbers $$$n_1$$$ and $$$m_1$$$ ($$$1 \le n_1, m_1 \le 1\,000$$$) — the height and width of the table with holes.

The next $$$n_1$$$ lines consist of $$$m_1$$$ characters "." or "N" and describe the configuration of the first table. A hole is considered to be a connected set of cells containing the symbol "N".

Next, in the test case is a pair of numbers $$$n_2$$$ and $$$m_2$$$ ($$$1 \le n_2, m_2 \le 1\,000$$$) — the height and width of the table with figures.

The next $$$n_2$$$ lines consist of $$$m_2$$$ characters "." or "F" and describe the configuration of the second table. A figure is considered to be a connected set of cells containing the symbol "F".

It is guaranteed that the sum of $$$n_1 \cdot m_1$$$ and the sum of $$$n_2 \cdot m_2$$$ across all test cases does not exceed $$$1\,000$$$. It is also guaranteed that each figure and each hole fit within a $$$7 \times 7$$$ square.

Output

For each test case, output "Yes" on a separate line if the task can be solved, and "No" otherwise. The letters of the words "Yes" and "No" can be output in any case.

Example
Input
3
3 3
N.N
N..
NN.
3 4
F.F.
...F
.FFF
3 3
N.N
N..
NN.
3 4
FFF.
..F.
F..F
3 1
N
.
N
1 1
F
Output
Yes
No
No

L. Bermuda Triangle
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

The Bermuda Triangle — a mysterious area in the Atlantic Ocean where, according to rumors, ships and airplanes disappear without a trace. Some blame magnetic anomalies, others — portals to other worlds, but the truth remains hidden in a fog of mysteries.

A regular passenger flight 814 was traveling from Miami to Nassau on a clear sunny day. Nothing foreshadowed trouble until the plane entered a zone of strange flickering fog. Radio communication was interrupted, the instruments spun wildly, and flashes of unearthly light flickered outside the windows.

For simplicity, we will assume that the Bermuda Triangle and the airplane are on a plane, and the vertices of the triangle have coordinates $$$(0, 0)$$$, $$$(0, n)$$$, and $$$(n, 0)$$$. Initially, the airplane is located at the point $$$(x, y)$$$ strictly inside the Bermuda Triangle and is moving with a velocity vector $$$(v_x, v_y)$$$. All instruments have failed, so the crew cannot control the airplane.

The airplane can escape from the triangle if it ever reaches exactly one of the vertices of the triangle. However, if at any moment (possibly non-integer) the airplane hits the boundary of the triangle (but not at a vertex), its velocity vector is immediately reflected relative to that side$$$^\dagger$$$, and the airplane continues to move in the new direction.

Determine whether the airplane can ever escape from the Bermuda Triangle (i.e., reach exactly one of its vertices). If this is possible, also calculate how many times before that moment the airplane will hit the boundary of the triangle (each touch of the boundary, even at the same point, counts; crossing a vertex does not count).

$$$^\dagger$$$ Reflection occurs according to the usual laws of physics. The angle of incidence equals the angle of reflection.

Input

Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). The description of the test cases follows.

The only line of each test case contains five integers $$$n$$$, $$$x$$$, $$$y$$$, $$$v_x$$$, and $$$v_y$$$ ($$$3 \le n \le 10^9$$$, $$$1 \le x, y$$$, $$$x+y \lt n$$$, $$$1 \le v_x, v_y \le 10^9$$$) — numbers describing the vertices of the triangle, the initial coordinates of the airplane, and the initial velocity vector.

Output

For each test case, output a single integer — the number of times the airplane will hit the boundary of the triangle before it escapes. If the airplane never escapes from the triangle, output $$$-1$$$.

Example
Input
6
6 2 2 5 2
6 2 2 20 8
4 1 2 1 1
4 1 1 1 2
4 1 1 2 1
6 2 3 2 3
Output
2
2
-1
-1
-1
5
Note

An illustration for the first test case is provided below:

In the second test case, the answer is the same as in the first, as all initial data, except for the speed, are the same, but the airplanes are initially moving in the same direction.

In the third test case, the answer is $$$-1$$$, as the airplane will move exclusively along the segments marked in green. An illustration is provided below:

M. ZP
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Airplane-airplane-airplane-airplane-airplane. How can I write the problem statement about you? How can I fit you into a problem where there are no airplanes in sight? Airplane-airplane-airplane.

It is not difficult to understand that any airplane can be represented as a triple of numbers $$$(a,b,c)$$$. We will say that the airplane satisfies the quality certificate $$$p$$$ if the following conditions are met:

  • $$$0 \le a, b, c \lt p$$$ and the numbers $$$a$$$, $$$b$$$, $$$c$$$ are pairwise distinct;
  • $$$a b c - 1$$$ and $$$a + b + c$$$ are divisible by $$$p$$$.

For a prime number $$$p$$$, you need to find any triple of numbers $$$(a, b, c)$$$ that satisfies the quality certificate $$$p$$$, or state that such a triple does not exist.

Input

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

Each test case is described by a single line containing a single integer prime number $$$p$$$ ($$$3 \le p \le 10^9$$$).

Output

For each test case, output either a suitable triple $$$(a, b, c)$$$ or $$$-1$$$ if such a triple does not exist.

Example
Input
9
3
7
11
13
223
3967
16127
1046527
16769023
Output
-1
4 2 1
8 4 10
3 1 9
140 16 67
228 3303 436
228 6453 9446
12228 402737 631562
2045228 1664119 13059676