Rutgers University Programming Contest Fall 2024
A. Anti-Closed Subsequences
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

We consider a set of integers $$$S$$$ to be anti-closed if there do not exist elements $$$x$$$, $$$y$$$ and $$$z$$$ in $$$S$$$ where $$$x+y=z$$$ ($$$x$$$, $$$y$$$ and $$$z$$$ do not necessarily have to be distinct).

For example:

  • $$$\{2, 3, 7\}$$$ is anti-closed, because no such $$$x$$$, $$$y$$$, $$$z$$$ exist
  • $$$\{1, 3, 7, 10\}$$$ is not anti-closed, because $$$3$$$, $$$7$$$, and $$$10$$$ are all in the set and $$$3+7=10$$$
  • $$$\{2, 4\}$$$ is also not anti-closed, because $$$2$$$ and $$$4$$$ are in the set and $$$2+2=4$$$

You are given an array $$$a$$$ of $$$n$$$ distinct integers. Partition it into at most $$$60$$$ anti-closed subsequences. Formally, find an array $$$b$$$ of size $$$n$$$ such that

  • $$$1 \leq b_i \leq 60$$$ for all $$$1 \le i \le n$$$
  • For each $$$1 \le x \le 60$$$, the subsequence of $$$a$$$ containing all values at indices $$$i$$$ where $$$b_i = x$$$ is anti-closed (The empty subsequence is considered anti-closed)

It can be shown that a solution always exists.

Input

The first line of the input contains a single integer $$$n$$$ ($$$1 \le n \le 10^4$$$) — the size of the array $$$a$$$.

The next line of the input contains $$$n$$$ distinct integers $$$a_1, a_2 \cdots a_n$$$ ($$$1 \le a_i \le 10^{18}$$$) — the elements of the array $$$a$$$.

Output

Output $$$n$$$ integers $$$b_1, b_2, \cdots b_n$$$ ($$$1 \le b_i \le 60$$$), representing the partition of $$$a$$$, as described above.

If there are multiple solutions, print any.

Examples
Input
15
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Output
1 2 2 1 3 1 3 3 2 2 4 4 4 2 1
Input
3
250000000000000000 500000000000000000 1000000000000000000
Output
1 2 3
Note

For the first test case, the input array is partitioned into these subsequences:

  • $$$b_i = 1$$$: $$$\{1, 4, 6, 15\}$$$
  • $$$b_i = 2$$$: $$$\{2, 3, 9, 10, 14\}$$$
  • $$$b_i = 3$$$: $$$\{5, 7, 8\}$$$
  • $$$b_i = 4$$$: $$$\{11, 12, 13\}$$$

We can show that each of these sets is anti-closed.

B. Card Pairs
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

You have a hand of cards, where each card has one of $$$n$$$ types. For each $$$i$$$ from $$$1$$$ to $$$n$$$, you have $$$a_i$$$ cards with type $$$i$$$, and the bank has infinite cards with type $$$i$$$.

You can perform the following trade with the bank any number of times:

Choose any two cards with the same type from your hand, and exchange them for a single card from the bank with any type except the type of the cards you just exchanged. Note that the bank only has cards with types $$$1$$$ through $$$n$$$, so you cannot trade for cards with any other types.

For example, here is a valid sequence of trades on the first sample case:

What is the maximum number of trades you can perform?

Input

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

The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 2\cdot 10^5$$$) — the number of card types.

The second line of each test case contains $$$n$$$ integers $$$a_1, a_2 \cdots a_n$$$ ($$$0 \le a_i \le 10^9$$$), where $$$a_i$$$ is the number of cards of type $$$i$$$ that you currently have.

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

Output

For each test case, print a single integer — the maximum number of trades you can perform.

Example
Input
9
4
2 0 0 1
2
5 2
6
1 5 0 6 7 1
4
1 1 0 1
2
4 2
7
1 2 4 8 4 2 1
2
1 1
4
0 0 0 0
3
1000000000 1000000000 1000000000
Output
2
5
19
0
5
21
0
0
2999999999
Note

The diagram above describes an optimal sequence of trades in the first test case.

In the fourth test case, it is impossible to perform any trades, since you don't start with any pair of cards with the same type, so the answer is $$$0$$$.

C. End-Balanced Subarrays
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given an array $$$a$$$ of $$$n$$$ integers. A sub-array $$$[a_l, a_{l+1}, \cdots a_r]$$$ is considered end-balanced if $$$l \lt r$$$ and $$$a_l + a_r = a_{l+1} + ... + a_{r-1}$$$.

For example, the subarrays $$$[4, 9, 5]$$$, $$$[-1, 3, 5, 9]$$$, and $$$[0, 0]$$$ are considered end-balanced, and the subarrays $$$[0]$$$, $$$[-2, -3, -5]$$$, and $$$[1, 1]$$$ are not.

How many subarrays of $$$a$$$ are end-balanced?

Input

The first line of the input contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The description of the test cases follows.

The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 2\cdot 10^5$$$) — the size of the array $$$a$$$.

The second line of each test case contains $$$n$$$ integers $$$a_1, a_2 \cdots a_n$$$ ($$$-10^9 \le a_i \le 10^9$$$) — the elements of the array $$$a$$$.

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

Output

For each test case, print a single integer — the number of end-balanced subarrays of $$$a$$$.

Example
Input
7
5
1 2 3 4 5
3
0 0 0
4
-10 5 -5 10
6
2 2 2 2 2 2
7
1 0 1 0 1 0 1
5
1000000000 1000000000 1000000000 1000000000 1000000000
1
-1000000000
Output
2
3
2
3
5
2
0
Note

The end-balanced subarrays in the first test case are:

  • $$$[a_1, a_2, a_3, a_4] = [1, 2, 3, 4]$$$
  • $$$[a_2, a_3, a_4, a_5] = [2, 3, 4, 5]$$$

The end-balanced subarrays in the second test case are:

  • $$$[a_1, a_2] = [0, 0]$$$
  • $$$[a_2, a_3] = [0, 0]$$$
  • $$$[a_1, a_2, a_3] = [0, 0, 0]$$$

The end-balanced subarrays in the third test case are:

  • $$$[a_2, a_3] = [5, -5]$$$
  • $$$[a_1, a_2, a_3, a_4] = [-10, 5, -5, 10]$$$

D. Hungry Arachnid
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given a tree on $$$n$$$ nodes rooted at node $$$1$$$. A spider and a fly are in the tree. The spider has three legs which are initially on nodes $$$a$$$, $$$b$$$, and $$$c$$$. The fly is on node $$$f$$$ and does not move.

Some nodes are considered to be in the shadow of the spider. A node is in the shadow of the spider if it lies on any of the three shortest paths between its legs, $$$a$$$–$$$b$$$, $$$a$$$–$$$c$$$, and $$$b$$$–$$$c$$$.

The spider can move its legs from vertices $$$a$$$, $$$b$$$, $$$c$$$ to vertices $$$a'$$$, $$$b'$$$, $$$c'$$$ if the size of its shadow remains constant and $$$\max\{\textrm{dist}(a, a'), \textrm{dist}(b, b'), \textrm{dist}(c, c')\}\leq 1$$$. The function $$$\textrm{dist}(u,\,v)$$$ indicates the number of edges on the shortest path between nodes $$$u$$$ and $$$v$$$ in the tree.

For example, here is one possible sequence of two moves by a spider with $$$6$$$ nodes in its shadow. The vertices that have a red outline are in the shadow of the spider, and the vertices that are colored red are the spider's legs.

The spider eats through its legs. Determine whether the spider can move any of its legs to the fly's location, after any number of moves (possibly zero).

Input

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

The first line of each test case contains a single integer $$$n$$$ ($$$2\leq n\leq 2\cdot 10^5$$$) — the number of vertices in the tree.

The next line of each test case contains $$$n-1$$$ integers $$$p_2,\,p_3,\,\ldots,\,p_n$$$ ($$$1 \le p_i \lt i$$$) — the parents of each vertex in the tree, except the root.

The next line of each test case contains three integers $$$a$$$, $$$b$$$, and $$$c$$$ ($$$1\leq a,\,b,\,c\leq n$$$) — the initial positions of each of the spider's legs.

The fourth and final line of each test case contains an integer $$$f$$$ ($$$1\leq f\leq n$$$) — the position of the fly.

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

Output

For each test case, print "YES" if the spider is able to catch the fly, 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
7
2
1
2 2 2
1
6
1 1 3 1 5
2 4 5
1
6
1 1 3 1 5
2 4 6
1
18
1 2 3 2 5 3 1 7 7 7 4 2 12 4 12 4 15
12 15 12
16
12
1 1 3 4 5 5 7 4 9 10 9
1 6 11
12
12
1 1 3 4 5 5 7 4 9 10 9
1 6 11
4
12
1 1 3 4 5 5 7 4 9 10 9
1 6 11
6
Output
Yes
Yes
No
Yes
Yes
No
Yes
Note

In the first test case, all legs of the spider are initially on vertex $$$2$$$, so that is the only vertex in the shadow. By moving all legs to vertex $$$1$$$ at the same time, the spider can reach the food while keeping its shadow the same size.

In the second test case, the spider can use this move to reach the food with one of its legs:

In the third test case, the food is located at vertex $$$1$$$, which is in the shadow of the spider, but the spider cannot move any of its legs to the food:

E. Combination Lock
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

You are faced with a combination lock that consists of an $$$n$$$ by $$$m$$$ grid of dials. A $$$k$$$-dial is a dial that can display values $$$0, 1, \cdots k-1$$$. A $$$k$$$-dial currently displaying value $$$v$$$ can be incremented to make it display value $$$(v + 1)\mod k$$$.

This particular lock consists of $$$3$$$-dials and $$$5$$$-dials in a checkerboard arrangement, with the top left dial being a $$$3$$$-dial. In a single move, you can increment a dial and all of its horizontal and vertical neighbors.

For example, here is one possible sequence of moves on a grid with $$$n=3$$$, $$$m=4$$$ (where $$$3$$$-dials are gray and $$$5$$$-dials are white):

Initially, all of the dials are displaying $$$0$$$. You remember what positions the dials must be in for the lock to open, but you forgot the combination of moves to reach it. Find a sequence of moves that sets all dials to the correct positions. You do not need to minimize the number of moves, but you can use no more than $$$20nm$$$ moves.

It can be shown that such a solution always exists.

Input

The first line of the input contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The description of the test cases follows.

The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 100$$$) — the number of rows and columns in the grid, respectively.

The $$$i$$$-th of the next $$$n$$$ lines contains $$$m$$$ integers describing the $$$i$$$-th row of the combination. The $$$j$$$-th of these is $$$a_{ij}$$$, the desired position of the dial at position $$$(i, j)$$$. If $$$i+j$$$ is even, $$$0 \le a_{ij} \lt 3$$$. Otherwise, $$$0 \le a_{ij} \lt 5$$$.

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

Output

The first line of output for each test case should contain a single integer $$$k$$$ ($$$0 \le k \le 20nm$$$) — the number of moves you will perform.

The next $$$k$$$ lines of output should each contain integers $$$i$$$ and $$$j$$$, indicating that the next move will be performed at position $$$(i, j)$$$.

If there are multiple solutions, print any.

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

Here is the move used in the first sample case:

In the second sample case, the lock is already in the target position, so no moves are needed.

F. Bitwise Triangles
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

We define a bitwise triangle to be a triplet of distinct integers $$$(a,\,b,\,c)$$$ with $$$a\&b\neq0$$$, $$$a\&c\neq0$$$, and $$$b\&c\neq0$$$, where $$$\&$$$ indicates the bitwise AND operator.

You are given an integer $$$n$$$. We define a triangle packing to be a set of pairwise disjoint bitwise triangles consisting of integers from $$$1$$$ to $$$n$$$ inclusive. Find a maximal triangle packing (one with the maximum number of bitwise triangles).

Input

The first line of the input contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The description of the test cases follows.

Each test case consists of a single line containing one integer $$$n$$$ ($$$1 \le n \le 2\cdot 10^5$$$) — the maximum allowed number in any of the triangles.

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

Output

The first line of output for each test case should contain a single integer $$$k$$$ ($$$0 \le k \le n$$$) — the size of the maximal triangle packing.

The next $$$k$$$ lines of output should each contain three distinct integers $$$a$$$, $$$b$$$, and $$$c$$$ ($$$1 \le a, b, c \le n$$$), representing one triangle of the maximal triangle packing.

If there are multiple solutions, print any.

Example
Input
6
4
5
6
10
18
14
Output
0
1
1 3 5
1
1 3 5
3
1 3 5
8 9 10
2 6 7
6
1 5 15
6 7 13
2 3 10
4 12 14
8 9 11
16 17 18
4
1 5 11
8 9 10
2 3 6
4 7 12
Note

In the first test case, we have $$$n=4$$$. It can be shown that we cannot create any bitwise triangles from the set $$$\{1, 2, 3, 4\}$$$, so the size of the maximal packing is $$$0$$$.

In the second test case, we have $$$n=5$$$. The given triangle packing is valid since each of the three numbers in the one bitwise triangle are odd, and therefore the bitwise AND of any two of them is nonzero. It can be shown that this packing is maximal for $$$n=5$$$.

G. Knight Polygon
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Two points $$$(x_1, y_1)$$$ and $$$(x_2, y_2)$$$ are considered to be a knight-move apart if exactly one of the following conditions holds:

  • $$$|x_1 - x_2| = 2$$$ and $$$|y_1 - y_2| = 1$$$
  • $$$|x_1 - x_2| = 1$$$ and $$$|y_1 - y_2| = 2$$$

Notice that this definition closely matches how a knight moves in chess. For example, here are three pairs of points that are a knight-move apart:

You are given integers $$$p$$$ and $$$q$$$. Find a simple lattice polygon whose area is $$$p/q$$$, where each pair of adjacent vertices is a knight-move apart, or state that no such polygon exists.

A polygon is simple if there are exactly two edges touching each vertex, and no two edges of the polygon intersect except at its vertices. A polygon is a lattice polygon if the coordinates of each of its vertices are integers.

Input

The first line of the input contains a single integer $$$t$$$ ($$$1 \le t \le 10$$$) — the number of test cases. The description of the test cases follows.

Each test case consists of a single line containing two integers $$$p$$$ and $$$q$$$ ($$$1 \le p, q \le 10^4$$$) — the numerator and denominator of the desired area, respectively.

Output

For each test case, if there is no solution, output a single integer $$$-1$$$.

Otherwise, the first line of output for each test case should contain a single integer $$$n$$$ ($$$3 \le n \le 10^5$$$) — the number of vertices in your polygon.

The next $$$n$$$ lines of output should each contain two integers $$$x$$$ and $$$y$$$ ($$$-10^9 \le x, y \le 10^9$$$) — the vertices of your polygon in either clockwise or counterclockwise order.

Your polygon should be simple, have an area of $$$\frac{p}{q}$$$, and each pair of adjacent vertices should be a knight-move apart.

If there are multiple solutions, print any.

Example
Input
4
18 3
1 2
8 1
20 3
Output
6
0 0
2 1
1 3
0 1
-1 3
-2 1
-1
14
-1 -2
-3 -1
-2 1
-1 3
0 1
1 3
2 1
3 -1
1 -2
2 0
1 2
0 0
-1 2
-2 0
-1
Note

Here is the polygon described by the output of the first test case, with an area of $$$\frac{18}{3} = 6$$$:

Here is the polygon described by the output of the third test case, with an area of $$$\frac{8}{1} = 8$$$:

For the second and fourth test cases, we can show that no valid polygon exists.

H. Illuminated Lights II
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

There are $$$n$$$ lights in a row, all initially deactivated. Some lights, when activated, will illuminate themselves and all lights to their left. The others, when activated, will illuminate themselves and all lights to their right.

This image shows the result of activating the fourth light (and nothing else) in the first sample case. The first four lights are illuminated, and everything else is not.

You have already figured out the minimum number of lights that need to be activated to illuminate everything, but now, you're wondering how many good subsets of lights exist.

A subset $$$S$$$ of the lights is considered good if when you activate the lights in $$$S$$$ (and only the lights in $$$S$$$), every light on the row is illuminated.

How many good subsets are there? Since the answer may be large, output it modulo $$$10^9+7$$$.

Input

The first line of the input contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The description of the test cases follows.

The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 2\cdot 10^5$$$) — the number of lights.

The next line of each test case contains a string $$$s$$$ consisting of $$$n$$$ characters L and R, indicating whether the lights are pointed to the left or right.

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

Output

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

Example
Input
7
6
LRRLRL
1
L
2
RL
2
LR
5
RRRRL
10
LRLRLRLRLR
31
LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL
Output
50
1
3
1
24
912
73741817
Note

For the second and fourth test cases, the only way to illuminate everything is to activate every light. Therefore, for these cases, the only good subset is the set containing all lights.

For the third test case, as long as either of the two lights is activated, everything will be illuminated. Therefore, the sets $$$\{1\}$$$, $$$\{2\}$$$, and $$$\{1, 2\}$$$ are good.

I. Domino Swap
time limit per test
4 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

There is an $$$n$$$ by $$$m$$$ grid of black and white squares. In one operation, you can pick any two adjacent squares of the same color and swap the colors of both. For example, here is a valid sequence of two operations on a grid with $$$n=4$$$, $$$m=3$$$:

You are also given a target grid of $$$n$$$ by $$$m$$$ black and white squares. Perform at most $$$200nm$$$ operations on the starting grid to reach the target grid, or state that it is impossible to do so. You do not need to minimize the number of operations.

It can be shown that if there is a solution, there is one that uses at most $$$200nm$$$ operations.

Input

The first line of the input contains a single integer $$$t$$$ ($$$1 \le t \le 500$$$) — the number of test cases. The description of the test cases follows.

The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 50$$$) — the number of rows and columns in the grid, respectively.

The $$$i$$$-th of the next $$$n$$$ lines contains $$$m$$$ characters describing the $$$i$$$-th row of the starting grid. The $$$j$$$-th of these is '#' if the square at position $$$(i, j)$$$ is black, and '.' if the square at position $$$(i, j)$$$ is white.

The next $$$n$$$ lines describe the target grid in the same format as the starting grid.

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

Output

For each test case, if there is no solution, output a single integer $$$-1$$$.

Otherwise, the first line of output for each test case should contain a single integer $$$k$$$ ($$$0 \le k \le 200nm$$$) — the number of operations you will perform.

The next $$$k$$$ lines of output should each contain four integers $$$i_1$$$, $$$j_1$$$, $$$i_2$$$, and $$$j_2$$$ ($$$1 \le i_1, i_2 \le n$$$, $$$1 \le j_1, j_2 \le m$$$) — the two adjacent squares $$$(i_1, j_1)$$$ and $$$(i_2, j_2)$$$ that are part of this operation.

If there are multiple solutions, print any.

Example
Input
6
3 3
..#
.#.
.#.
#.#
..#
.##
4 6
#.#.#.
.#.#.#
#.#.#.
.#.#.#
......
......
......
......
1 1
#
#
4 5
#...#
...#.
###.#
....#
..#.#
.#...
###.#
.##.#
1 8
...#....
........
4 2
..
..
..
..
##
##
##
##
Output
3
1 1 1 2
1 2 2 2
2 3 3 3
-1
0
5
1 2 1 3
2 2 2 3
1 1 1 2
2 3 2 4
4 2 4 3
-1
4
1 1 1 2
4 1 4 2
2 1 3 1
2 2 3 2
Note

Here is the sequence of $$$3$$$ operations described by the output of the first test case:

It is impossible to perform any operations in the second test case, so it is not possible to reach the target grid.

J. Ambiguous Permutations
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You want to find two permutations $$$p$$$ and $$$q$$$ of size $$$n$$$ that satisfy $$$m$$$ restrictions of the form $$$(p_i-p_j)\cdot (q_i-q_j) \gt 0$$$.

A permutation of length $$$n$$$ is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2, 3, 1, 5, 4]$$$ is a permutation, but $$$[1, 2, 2]$$$ is not a permutation ($$$2$$$ appears twice in the array), and $$$[1, 3, 4]$$$ is also not a permutation ($$$l=3$$$ but there is $$$4$$$ in the array).

Find two distinct permutations $$$p$$$ and $$$q$$$ of size $$$n$$$ such that all restrictions are satisfied, or state that it is impossible to do so. $$$p$$$ and $$$q$$$ are considered distinct if there is at least one index $$$i$$$ where $$$p_i \neq q_i$$$.

Input

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

The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1\leq n\leq 2\cdot 10^5$$$, $$$0 \le m \le min(2\cdot 10^5, \frac{n(n-1)}{2})$$$) — the size of the permutations and the number of restrictions, respectively.

Each of the next $$$m$$$ lines of the test case contains two integers $$$i$$$ and $$$j$$$ ($$$1 \le i \lt j \le n$$$) — representing the restriction $$$(p_i-p_j)\cdot (q_i-q_j) \gt 0$$$. It is guaranteed that all restrictions in the input are distinct.

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

Similarly, the sum of $$$m$$$ over all test cases does not exceed $$$2\cdot 10^5$$$.

Output

The first line of output for each test case should contain "YES" if a valid $$$p$$$ and $$$q$$$ exist, 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.

If you printed "YES", print two additional lines of output.

The first of these should contain $$$n$$$ distinct integers $$$p_1,\, p_2, \cdots\, p_n$$$ ($$$1 \le p_i \le n$$$) — the permutation $$$p$$$.

The second of these should contain $$$n$$$ distinct integers $$$q_1,\, q_2,\, \cdots\, q_n$$$ ($$$1 \le q_i \le n$$$) — the permutation $$$q$$$.

Examples
Input
3
1 0
2 0
2 1
1 2
Output
No
Yes
1 2 
2 1 
No
Input
2
5 8
1 4
1 5
2 3
2 4
2 5
3 4
3 5
4 5
4 3
1 2
1 3
1 4
Output
Yes
5 3 4 1 2
4 3 5 1 2
Yes
1 3 4 2
1 2 3 4
Input
1
5 10
1 2
1 3
1 4
1 5
2 3
2 4
2 5
3 4
3 5
4 5
Output
No
Note

In the first test case of the first test, $$$n=1$$$. Since there is only one distinct permutation of size $$$1$$$, there is no solution.

In the second test case of the first test, $$$n=2$$$ and there are no restrictions that need to be satisfied. So $$$p = [1,\, 2]$$$ and $$$q = [2,\,1]$$$ is a valid solution.

K. Tree With One Edge
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Alice and Bob are playing a game on a tree rooted at node 1. A token is placed on the root then the players take turns making moves with Alice moving first. During a move a player must move the token from the node its on to one of that node's children. If there are no legal moves available, then the player whose turn it is will lose.

Since Alice and Bob are too good at this game they decided to play a modified version of the game. Before the game begins, Alice can add a single directed edge $$$(u, v)$$$ to the tree. Then, during the game, if the token is on vertex $$$u$$$ and the extra edge is present, the current player can choose to move the token to vertex $$$v$$$ and delete the extra edge (preventing it from being used multiple times in one game).

Of the $$$n^2$$$ possible pairs $$$(u, v)$$$ Alice can choose, how many will allow Alice to win the game assuming both players play optimally? Note that $$$u=v$$$ is allowed, as are $$$(u, v)$$$ pairs that match an existing edge in the tree (in either direction).

Input

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

The first line of each test case contains a single integer $$$n$$$ ($$$2\leq n\leq 2\cdot 10^5$$$) — the number of vertices in the tree.

The next line of each test case contains $$$n-1$$$ integers $$$p_2,\,p_3,\,\ldots,\,p_n$$$ ($$$1 \le p_i \lt i$$$) — the parents of each vertex in the tree, except the root.

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

Output

For each test case, print a single integer — the number of $$$(u, v)$$$ pairs that will allow Alice to win, assuming both players play optimally.

Example
Input
4
3
1 2
6
1 1 3 3 5
6
1 2 3 4 5
5
1 1 1 1
Output
4
33
27
25
Note

For the first test test case, here are the $$$4$$$ edges Alice can add to give herself the winning strategy:

L. Two Squares
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

There is an $$$n$$$ by $$$m$$$ grid of white squares. You want to place a $$$k$$$ by $$$k$$$ red square and a $$$k$$$ by $$$k$$$ blue square on this grid such that they do not overlap. For example, here is a valid solution for $$$n=6$$$, $$$m=8$$$, $$$k=3$$$:

How many ways are there to do this? Two ways are considered different if at least one square is a different color in each.

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

Input

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

Each test case consists of a single line containing three integers $$$n$$$, $$$m$$$, and $$$k$$$ ($$$1 \le n, m \le 10^9$$$, $$$1 \le k \le \min(n, m)$$$) — the number of rows and columns in the grid, and the side length of the squares, respectively.

Output

For each test case, print a single integer — the number of ways to place both squares in the grid, taken modulo $$$10^9+7$$$.

Example
Input
6
1 2 1
4 3 3
3 4 2
10 10 3
13 9 4
1000000000 1000000000 12345678
Output
2
0
8
2940
1860
547313402
Note

The solutions for the first test case are:

In the second test case, there is no way to fit both squares inside the rectangle.

The solutions for the third test case are:

M. LIS On Tree
time limit per test
4 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

There were too many constructive problems in this contest, so we decided to set a standard data structure problem.

You are given a tree with $$$n$$$ labeled nodes. Each node also has a blank value initially.

The longest increasing tree subsequence between two nodes $$$(u, v)$$$ on the tree is computed as follows:

  • Write down all the nonblank values from the nodes on the path from $$$u$$$ to $$$v$$$, in order. Compute the longest increasing subsequence$$$^\dagger$$$ of the resulting sequence.

You are given $$$n$$$ updates, $$$x_1, x_2 \dots x_n$$$. For update $$$i$$$, fill in the value $$$i$$$ at node $$$x_i$$$. After each update, compute the longest longest increasing tree subsequence among all pairs of nodes $$$(u, v)$$$ in the tree.

It is guaranteed that all $$$x_i$$$ values are distinct.

$$$^\dagger$$$ Define a sequence of integers $$$a_i...a_m$$$. A subsequence $$$a_{i_1}, a_{i_2}, ..., a_{i_k}$$$ where $$$1 \leq i_1 \lt i_2 \lt \cdots \lt i_k \leq m$$$ is called increasing if $$$a_{i_1} \lt a_{i_2} \lt a_{i_3} \lt ... \lt a_{i_k}$$$. An increasing subsequence is called longest if it has maximum length among all increasing subsequences.

Input

The first line of the input contains an integer $$$1 \leq t \leq 10^4$$$, denoting the number of test cases.

The first line of each test case contains one integer $$$2 \leq n \leq 5 \cdot 10^5$$$.

The next $$$n - 1$$$ lines contain two integers $$$1 \leq u, v \leq n$$$, denoting an undirected edge between the nodes with labels $$$u$$$ and $$$v$$$, respectively. It is guaranteed that the input edges form a tree.

The last line of input for the testcase consists of $$$n$$$ integers $$$x_1...x_n$$$, denoting the updates in order. It is guaranteed that all $$$x_i$$$ values are distinct.

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

Output

For each test case, print $$$n$$$ space-separated integers on a single line, denoting the answer after the $$$i^\textrm{th}$$$ update.

Example
Input
4
5
1 2
2 3
3 4
4 5
3 1 5 2 4
10
5 1
3 4
3 6
3 7
8 3
5 8
2 5
9 2
9 10
3 6 9 4 5 2 1 8 10 7
15
10 1
3 4
13 5
3 7
8 3
15 8
12 13
9 12
2 9
11 2
11 14
6 11
10 6
10 15
9 2 10 5 13 14 7 15 11 12 1 6 8 3 4
2
1 2
1 2
Output
1 2 2 2 3
1 2 2 2 2 3 3 3 4 4
1 2 3 3 3 3 4 4 4 4 4 4 5 6 7
1 2
Note

Remember that the updates tell you the value of the $$$x_i^\textrm{th}$$$ node, not that the value of node $$$i$$$ is $$$x_i$$$.

An example of the process for the first tree is shown below. The yellow nodes are one potential longest increasing tree subsequence after each operation. The node's labels are 1-5 from left to right, initially with blank values.

N. String Split
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given two strings $$$s$$$ and $$$t$$$. In one operation, you can delete all the odd-indexed characters from $$$s$$$ or all the even-indexed characters from $$$s$$$.

For example, if you perform an operation on the string abcdefg, you could choose to turn it into aceg or bdf.

After performing any number of operations on $$$s$$$ (including zero), is it possible for $$$s$$$ to equal $$$t$$$?

Input

The first line of the input contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The description of the test cases follows.

The first line of each test case contains a string $$$s$$$ ($$$1 \le |s| \le 2\cdot 10^5$$$) of lowercase letters — the starting string, as described above.

The second line of each test case contains a string $$$t$$$ ($$$1 \le |t| \le 2\cdot 10^5$$$) of lowercase letters — the desired ending string, as described above.

It is guaranteed that the sum of $$$|s|$$$ over all test cases does not exceed $$$2\cdot 10^5$$$. Similarly, the sum of $$$|t|$$$ over all test cases does not exceed $$$2\cdot 10^5$$$.

Output

For each test case, print "YES" if it is possible to make all $$$s$$$ equal to $$$t$$$ after any number of operations, 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
6
tjhdoumraise
thomas
nick
james
baccba
baccba
abcdefg
cde
abcdabcd
aa
abcdabcd
bc
Output
YES
NO
YES
NO
YES
NO
Note

In the first test case, by removing all even-indexed characters of $$$s$$$, we obtain $$$s = thomas$$$, so we have $$$s = t$$$.

In the second test case, the length of $$$s$$$ is less than the length of $$$t$$$, and if we perform any operations, the length of $$$s$$$ will only decrease. Therefore, $$$s$$$ can never equal $$$t$$$.

In the third test case, we have $$$s = t$$$ even before performing any operations.