OCPC 2025 Summer, Day 9: Indian Contest
A. String conversion
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given two strings $$$X$$$ and $$$Y$$$ of length $$$n$$$ each, consisting of characters A and B. The following two kinds of operations can be applied on the string $$$X$$$ any (including $$$0$$$) number of times:

  • You can replace an occurrence of AA by BA.
  • You can replace an occurrence of BB by AB.

For example, you can convert ABAA to ABBA, and then ABBA to AABA.

First, find whether it is possible to convert $$$X$$$ into $$$Y$$$ using any (including 0) number of allowed operations. If it is so possible, find the minimum number of operations required for the conversion.

Then, you need to process updates of the following types:

  • X p: Flip the $$$p-$$$th character in $$$X$$$. Make it $$$\texttt{A}$$$ if it was $$$\texttt{B}$$$ before, and make it $$$\texttt{B}$$$ otherwise.
  • Y p: Flip the $$$p-$$$th character in $$$Y$$$. Make it $$$\texttt{A}$$$ if it was $$$\texttt{B}$$$ before, and make it $$$\texttt{B}$$$ otherwise.

After each update, find if it is possible to convert $$$X$$$ into $$$Y$$$. Note that, you only have to check if it is possible to convert $$$X$$$ into $$$Y$$$; you don't have to actually convert it. Also, you don't have to print the minimum number of operations required after updating, in case the conversion is possible.

Input

The first line contains $$$t (1 \le t \le 100)$$$, the number of test cases. Then, the testcases follow:

  • The first line of each testcase contains two space separated integers $$$n (1 \le n \le 3 \cdot 10^5)$$$ and $$$q (0 \le q \le 3 \cdot 10^5)$$$, the length of both the strings, and the number of updates respectively.
  • The second line contains the string $$$X$$$.
  • The third line contains the string $$$Y$$$.
  • Each of the next $$$q$$$ lines contains two values, the first of which is a character, either X or Y, and the second is an integer $$$p$$$, representing the position at which the character has to be flipped.
It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$3 \cdot 10^5$$$. The sum of $$$q$$$ over all testcases also does not exceed $$$3 \cdot 10^5$$$.
Output

For each testcase:

  1. On the first line, print "YES" if $$$X$$$ can be converted into $$$Y$$$ using the allowed operations, before any updates have been done. Otherwise, print "NO". Then, if the conversion is possible, print a single line containing the minimum number of operations required to do so.
  2. Then, in the $$$i-$$$th of the next $$$q$$$ lines, print "YES" if it is possible to convert $$$X$$$ into $$$Y$$$, after the first $$$i$$$ updates and "NO" otherwise.
Example
Input
2
1 0
A
B
4 2
ABAA
AABA
Y 3
X 2
Output
NO
YES
2
NO
YES
Note

In the first testcase, $$$X=\texttt{A}$$$ and $$$Y=\texttt{B}$$$. Since the conversion is not possible, we print "NO" on the first line. Then, there are no updates to process ($$$Q = 0$$$), so we move to the next testcase

In the second testcase:

Number of updates$$$X$$$$$$Y$$$Possible to convert?Minimum Number of moves required
0ABAAAABAYES2
1ABAAAAAANO-
2AAAAAAAAYES0 (not to be printed)
  • Before any updates are done, $$$X$$$ is $$$\texttt{ABAA} $$$ and $$$Y$$$ is $$$\texttt{AABA}$$$, and as explained earlier, we can do the conversion as:

    ABAA $$$\rightarrow$$$ ABBA $$$\rightarrow$$$ AABA

  • After the first update, $$$X$$$ is $$$\texttt{ABAA} $$$ and $$$Y$$$ is $$$\texttt{AAAA}$$$, and the conversion is not possible.
  • After the second update, $$$X = Y = \texttt{AAAA} $$$, and the conversion is possible in $$$0$$$ operations.

B. Mirror Grid
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given a grid of size $$$N \times N$$$. Rows are numbered from $$$0$$$ to $$$N-1$$$ from top to bottom and columns are numbered from $$$0$$$ to $$$N-1$$$ from left to right. $$$(i, j)$$$ represents the cell on row $$$i$$$ and column $$$j$$$. $$$(0, 0)$$$ is therefore the cell at the top most left position.

Each cell in the grid is either empty or contains a block. There are $$$B$$$ ($$$1 \le B \le 20$$$) blocks in the grid. The blocks are placed in such a way that no two blocks share a side or a corner. Formally, no two cells $$$(a, b)$$$ and $$$(c, d)$$$ have blocks such that $$$|a-b| \leq 1 \And |c-d| \leq 1$$$. It is also guaranteed that there is no block on the cell $$$(0, 0)$$$. Your task is to find the positions of these blocks given the dimension of the grid.

You can do a query $$$420$$$ times to place mirrors on the grid and send a light ray starting from $$$(0, 0)$$$ in some direction. Note that mirrors placed for a particular query are removed before the next query. Mirrors can be placed on any cell, including $$$(0, 0)$$$ and cells containing blocks.

The query returns the number of mirrors the ray of light gets reflected from before hitting a block or going outside the grid. If a mirror is placed on the same cell as a block, light hits the block first and the mirror isn't counted in the return value. If a light ray hits the non reflecting side of a mirror it passes through to the next cell and nothing gets added. If the light starts going in a cycle $$$-1$$$ is returned.

A mirror is represented by a tuple of size $$$3$$$. The number of mirrors in a query can be no more than $$$4 \cdot N$$$ and no two mirrors can be placed in the same cell. The first $$$2$$$ integers in the tuple represent the row and column at which the mirror is placed respectively. The $$$3$$$rd integer represents the type of mirror. The mirrors are tilted such that light reflects perpendicularly on them and can be of $$$4$$$ types:

  • topLeft: The mirror is tilted like $$$/$$$ and the reflecting side is on top left. This is represented by $$$0$$$ in the code.
  • topRight: The mirror is tilted like $$$ \backslash $$$ and the reflecting side is on top right. This is represented by $$$1$$$ in the code.
  • bottomRight: The mirror is tilted like $$$/$$$ and the reflecting side is on bottom right. This is represented by $$$2$$$ in the code.
  • bottomLeft: The mirror is tilted like $$$\backslash$$$ and the reflecting side is on bottom left. This is represented by $$$3$$$ in the code.

You can choose the direction light travels in when starting from the cell $$$(0,0)$$$. These can be Down or Right represented by $$$0$$$ and $$$1$$$ in code respectively.

Please view the interaction format and sample explanation for more clarity.

Input

The input consists of a single integer $$$N(2 \le N \le 1000)$$$, representing the dimensions of the grid.

Interaction

Your program can ask queries using the format specified. It should first print $$$M$$$, the number of mirrors placed and $$$D$$$, the direction of the light. Then $$$M$$$ lines should follow, containing three integers each — the row $$$r_i$$$, column $$$c_i$$$ and type $$$t_i$$$ of mirror placed.

? M D

$$$r_1$$$ $$$c_1$$$ $$$t_1$$$

$$$r_2$$$ $$$c_2$$$ $$$t_2$$$

$$$\ldots$$$

$$$r_M$$$ $$$c_M$$$ $$$t_M$$$

When you want to give an answer you should first print the number of blocks $$$B$$$ determined, followed by the position of each block.

! B

$$$r_1$$$ $$$c_1$$$

$$$r_2$$$ $$$c_2$$$

$$$\ldots$$$

$$$r_B$$$ $$$c_B$$$

Note: In case of a wrong answer or invalid query, $$$-2$$$ will be returned by the judge. In such cases you must exit the program immediately.

Example
Input
4




1




0
Output

? 3 1
0 1 1
0 2 3
1 2 0

? 3 0
0 1 1
0 2 3
1 2 0

! 2
1 2
3 0
Note

The path of light for the first query can be seen in the image provided below. For the second query, light doesn't hit any mirrors and directly goes to the block at $$$(3, 0)$$$.

C. Graph Subsets
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Consider an undirected tree. A subset of nodes of this tree is called good if there exists a node $$$r$$$ such that if the tree is rooted at $$$r$$$, no node in the subset is an ancestor of another node in the subset.

You are given a connected undirected graph of $$$N$$$ nodes numbered from $$$1$$$ to $$$N$$$ and $$$M$$$ edges numbered from $$$1$$$ to $$$M$$$. Each edge $$$j$$$ connects the nodes $$$U_j$$$ and $$$V_j$$$. The graph has no self-loops or duplicate edges. A subset of nodes of this graph is called ideal if there is some spanning tree of the graph for which this subset of nodes is good.

You are given a subset of nodes of this graph which has size at least 2. Find whether or not it is ideal. If it is ideal, find a valid spanning tree and root as specified above.

Input

The first line contains two integers, $$$N$$$ and $$$M$$$, respectively the number of nodes and number of edges.

The next $$$M$$$ lines describe the edges. The $$$j$$$-th line contains two numbers $$$U_j$$$ and $$$V_j$$$, the endpoints of the $$$j$$$-th edge.

The next line contains a single integer $$$S$$$, the size of the given subset of nodes.

The next line contains $$$S$$$ integers, $$$T_1, ... T_S$$$, the given subset of nodes.

Constraints

  • $$$2 \leq N \leq 3 \times 10^5$$$
  • $$$N-1 \leq M \leq 3 \times 10^5$$$
  • $$$1 \leq U_j \leq N$$$, $$$1 \leq V_j \leq N$$$, $$$U_j \neq V_j$$$ for all $$$1 \leq j \leq M$$$.
  • $$$2 \leq S \leq N$$$
  • $$$1 \leq T_k \leq N$$$ for all $$$1 \leq k \leq S$$$.
  • All $$$T_k$$$ are distinct.
Output

If the given subset is not ideal, you should print a single line which is NO.

If the given subset is ideal, on the first line, you should print YES.

On the second line, you should print your chosen root.

Then, on the next $$$N-1$$$ lines you should describe your chosen spanning tree. Each line should contain the two endpoints of a single chosen edge. The two endpoints may be in any order. The edges themselves may be in any order.

Examples
Input
6 7
1 2
2 3
1 4
5 6
5 1
6 3
4 3
3
4 2 5
Output
YES
1
1 2
1 4
1 5
2 3
5 6
Input
6 7
1 2
2 3
1 4
5 6
5 1
6 3
4 3
3
3 6 5
Output
NO
Note

Samples

In Sample 1, the given subset is ideal. If the spanning tree in the sample output is chosen and rooted at 1, no node in the subset is an ancestor of another node in the subset.

In Sample 2, the given subset is not ideal. It is impossible to choose a valid spanning tree and root.

D. Bricks
time limit per test
3 seconds
memory limit per test
1024 megabytes
input
standard input
output
standard output

There are $$$N$$$ bricks numbered from $$$1$$$ to $$$N$$$ and $$$M$$$ colors numbered from $$$1$$$ to $$$M$$$. Each brick $$$i$$$ has a size $$$A_i$$$ and color $$$B_i$$$. It is guaranteed that there is at least one brick of each color.

A tower made of some set of bricks is called $$$k$$$-beautiful if the absolute difference in sizes between every two adjacent bricks is at most $$$k$$$.

Your task is to answer $$$Q$$$ queries. The $$$j$$$-th query is: can at least one $$$K_j$$$-beautiful tower be made using all the bricks of the colors $$$X_j$$$ and $$$Y_j$$$ and no bricks of any other colors? It is guaranteed that $$$X_j$$$ and $$$Y_j$$$ are distinct.

Input

The first line contains two integers, $$$N$$$ and $$$M$$$.

The next $$$N$$$ lines describe the bricks. The $$$i$$$-th of these lines contains two integers $$$A_i$$$ and $$$B_i$$$.

The next line contains a single integer, $$$Q$$$.

The next $$$Q$$$ lines describe the queries. The $$$j$$$-th of these lines contain three integers, $$$K_j$$$, $$$X_j$$$ and $$$Y_j$$$.

Constraints

  • $$$1 \leq M \leq N \leq 2 \cdot 10^5$$$.
  • $$$1 \leq Q \leq 2 \cdot 10^5$$$.
  • For all $$$1 \leq i \leq N$$$:
    • $$$1 \leq A_i \leq 2 \cdot 10^9$$$.
    • $$$1 \leq B_i \leq M$$$.
  • There is at least one brick of each color.
  • For all $$$1 \leq j \leq Q$$$:
    • $$$1 \leq K_j \leq 2 \cdot 10^9$$$.
    • $$$1 \leq X_j \leq M$$$.
    • $$$1 \leq Y_j \leq M$$$.
    • $$$X_j \neq Y_j$$$.
Output

For each query, you should print either YES or NO on a separate line.

Examples
Input
8 4
5 1
10 2
2 2
20 3
33 4
6 1
17 2
42 1
5
13 3 4
7 2 3
25 1 3
20 1 2
20 2 4
Output
YES
NO
YES
NO
YES
Input
5 2
5 1
5 1
10 2
15 1
20 2
1
5 1 2
Output
YES
Input
10 4
35 4
92 3
97 3
19 1
20 1
84 4
11 1
26 2
60 3
29 3
10
8 2 4
10 2 4
34 2 3
43 2 3
47 1 2
24 2 3
30 3 4
45 1 3
46 1 4
14 2 3
Output
NO
NO
YES
YES
YES
NO
YES
YES
NO
NO

E. Tree Vertex Ordering
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given a tree with $$$n$$$ vertices, numbered $$$1, 2, \ldots, n$$$. For two vertices $$$x$$$ and $$$y$$$, let $$$d(x, y)$$$ denote the number of edges on the unique simple path between $$$x$$$ and $$$y$$$ in the tree.

For distinct nodes $$$a$$$ and $$$b$$$, we say that node $$$a$$$ dominates node $$$b$$$ if the number of vertices $$$x$$$ such that $$$d(a, x) \lt d(b, x)$$$ is strictly more than the number of vertices $$$y$$$ such that $$$d(a, y) \gt d(b, y)$$$.

You want to order the $$$n$$$ vertices such that for any $$$a, b$$$, if $$$a$$$ dominates $$$b$$$, then $$$a$$$ appears before $$$b$$$ in the order. Find such an ordering, or claim that it doesn't exist.

Input

The first line contains a single integer $$$t(1 \le t \le 3 \cdot 10^5)$$$, the number of test cases. Then the testcases follow:

  • The first line of each testcase contains a single integer $$$n (1 \le n \le 3 \cdot 10^5)$$$ — the number of vertices.
  • Each of the next $$$n - 1$$$ lines contains two integers $$$u$$$ and $$$v$$$, denoting an edge between vertices $$$u$$$ and $$$v$$$.

The sum of $$$n$$$ over all testcases does not exceed $$$3 \cdot 10^5$$$.

Output

For each test case:

  • On the first line, print YES if a valid ordering exists, and NO otherwise.
  • If a valid ordering exists, print one such ordering on a new line. If there are multiple valid orderings, you can output any of them.
Example
Input
2
6
1 2
2 3
2 4
3 5
3 6
2
1 2
Output
YES
2 3 1 4 5 6 
YES
1 2 
Note

In the first testcase, the ordered domination pairs are the following:

  • 2 dominates 1, 4, 5, 6
  • 3 dominates 1, 4, 5, 6
Note that no vertex appears after a vertex that it dominates, in the ordering $$$2, 3, 1, 4, 5, 6$$$, hence the output is valid.

The ordering $$$3, 2, 4, 5, 1, 6$$$ is also valid, among others.

F. Arrow Game
time limit per test
10 seconds
memory limit per test
192 megabytes
input
standard input
output
standard output

You are given an initial configuration of $$$N$$$ cells (from left to right), where each cell can have one of the 4 things: empty, tombstone, left arrow or right arrow.

Two players play a game on it, starting from Player A followed by Player B turn-by-turn.

  • Player A: picks a currently empty position (say index $$$z$$$) OR ends the game.
  • Player B: replaces every right arrow on the left of $$$z$$$ with a tombstone AND replaces every left arrow on the right of $$$z$$$ with a tombstone (i.e. replace all arrows pointing towards z with a tombstone) AND then assigns an arrow to position $$$z$$$. Player B chooses whether to assign a left arrow or right arrow.

Player A's objective is to maximize the number of arrows (which were not turned to tombstones) when the game ends and Player B's objective is to minimize the number of arrows when the game ends.

Find the number of arrows at the end of the game if both players play optimally.

Input
  • The first line contains $$$T (1 \le T \le 10^5)$$$ and $$$ N (1 \le N \le 20)$$$, the number of test cases and the sizes of all the input strings.
  • The $$$i^{\text{th}}$$$ of the next $$$T$$$ lines each contain string $$$S$$$ of size $$$N$$$, representing the initial configuration of cells.
  • In the input string $$$S$$$, X represents an empty spot, T represents a tombstone, L represents a left arrow, R represents a right arrow.
Output

For each test case, output the number of arrows at the end of game under optimal play.

Examples
Input
4 4
XXXX
LTXR
RTXL
LRTR
Output
2
3
2
3
Input
4 4
XXXX
LTXR
RTXL
LRTR
Output
2
3
2
3
Note
  • Initially, all the cells are empty:
  • Player A chooses $$$z = 2$$$
  • Player B chooses to assign a right arrow to $$$z = 2$$$. The configuration now is XRXX:

  • Player A chooses $$$z = 3$$$
  • Player B replaces the right arrow at $$$z = 2$$$ with a tombstone and chooses to assign a right arrow to $$$z = 3$$$. The configuration now is XTRX:

  • Player A chooses $$$z = 1$$$
  • Player B chooses to assign a left arrow to $$$z = 1$$$. The configuration now is LTRX:

  • Player A ends the game with $$$2$$$ arrows.

G. Graph Passage
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

There is a directed road network with $$$N$$$ nodes and $$$M$$$ weighted edges, where you are at node $$$1$$$. Your car has battery capacity $$$C$$$, and also starts with $$$C$$$ battery.

Each edge in the graph either increases or decreases your battery when you traverse it. When you have $$$x$$$ battery, you can traverse an edge with weight $$$w$$$ if and only if $$$x \geq w$$$. After traversing such an edge, you will have $$$\min(C, x - w)$$$ battery. Some edges may even be negative, thus increasing your battery.

You want to know the following: which nodes can you reach from your starting node? For each node you can reach, what is the maximum amount of battery you can have when you reach it?

Input

The first line contains $$$T(1 \le T \le 50)$$$, the number of testcases. Then the test cases follow, each with the following format:

  • The first line contains three space-separated integers: $$$N(1 \le N \le 100)$$$, $$$M(1 \le M \le 500)$$$, and $$$C(0 \le C \le 5 \cdot 10^{17}$$$, the number of nodes, number of edges and the battery capacity respectively.
  • $$$i^{\text{th}}$$$ of the next $$$M$$$ lines contains three space separated integers, $$$U_i, V_i (1 \le U_i, V_i \le N)$$$ and $$$W_i (-10^{15} \le W_i \le 10^{15})$$$, indicating a directed edge from $$$U_i$$$ to $$$V_i$$$ with weight $$$W_i$$$.
Output

For each testcase, print $$$N$$$ space-separated integers on a new line. The $$$i^{\text{th}}$$$ of these integers should denote the maximum possible battery you can have when you reach node $$$i$$$, or $$$-1$$$ if it is impossible.

Example
Input
2
6 6 11
1 2 3
1 3 2
2 3 5
3 4 4
4 5 7
4 6 1
6 6 11
1 3 2
1 2 3
2 3 5
3 4 -9
4 5 12
4 6 10
Output
11 8 9 5 -1 4 
11 8 9 11 -1 1 

H. Matrix Sum Approximation
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

There is a hidden matrix $$$M$$$ with $$$n$$$ rows numbered $$$1, 2, \ldots n$$$ and $$$m$$$ columns numbered $$$1, 2, \ldots m$$$. Let $$$(i, j)$$$ denote the cell with row number $$$i$$$ and column number $$$j$$$, and let $$$M[i][j]$$$ denote the matrix value in the cell $$$(i, j)$$$. The matrix is guaranteed to satisfy the following properties:

  • The matrix values are integers in the range $$$[1, 10^9]$$$, that is $$$1 \leq M[i][j] \leq 10^9$$$ for all $$$1 \leq i \leq n, 1 \leq j \leq m$$$.
  • For any row $$$1 \leq i \leq n$$$, and column $$$1 \leq j \lt m$$$, $$$M[i][j] \geq M[i][j + 1]$$$.
  • For any row $$$1 \leq i \lt n$$$, and column $$$1 \leq j \leq m$$$, $$$M[i][j] \geq M[i + 1][j]$$$.

In other words, every row and every column of the matrix is non-increasing.

To know the value of the cell $$$(i, j)$$$ of the matrix, you can make a query of the form (without quotes):

"? i j"

where $$$1 \leq i \leq n$$$ and $$$1 \leq j \leq m$$$. In response, you get the value of $$$M[i][j]$$$.

You are allowed to make at most $$$50$$$ queries, and your task is to approximate the sum of the matrix to within a factor of $$$2$$$. In particular, if the actual sum is $$$X$$$, you need to output an integer $$$Y$$$ such that $$$\lceil X/2 \rceil \leq Y \leq 2\cdot X$$$.

Note that the grader is adaptive, which means that the matrix is not fixed before the interaction begins. However, the judge will never contradict the replies given in the past. This means that, it is guaranteed at each point of time that there exists a valid matrix with the aforementioned properties, whose values are consistent with the responses of the judge to the past queries.

Interaction

First, read an integer $$$t (1 \leq t \leq 200)$$$, denoting the number of test-cases. Each test case should be considered independently. For each test case,

  • First read two space separated integers, $$$n (1 \leq n \leq 10^4)$$$ and $$$m(1 \leq m \leq 10^4)$$$. It is guaranteed that $$$n \cdot m \leq 10^4$$$.
  • Then, whenever you want to know $$$M[i][j]$$$ for some $$$1 \leq i \leq n, 1 \leq j \leq m$$$, you should output "? i j" on a new line, without quotes. The judge replies with the value of $$$M[i][j]$$$.
  • When you have a $$$2-$$$ approximation of the sum (say an integer $$$Y$$$), you should output "! Y" on a new line, without quotes, and then move to the next testcase (if any).

Please note that, there is no additional constraint on sum of $$$n$$$, sum of $$$m$$$, or sum of $$$n \cdot m$$$ over the testcases.

Example
Input
2
2 3

10

3

4

1 1

1
Output


? 1 1

? 2 2

? 1 3

! 42

? 1 1

! 1

Note

In the first test case, the matrix is:

1054
831

The total sum is $$$10+5+4+8+3+1 = 31$$$, and the output sum is $$$42$$$, which is a valid $$$2-$$$approximation, since $$$16 \leq 42 \leq 62$$$.

In the second test-case, the matrix is a single cell with value $$$1$$$, and hence the output $$$1$$$ is considered correct.

I. Paint It Black
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

You have a matrix $$$A$$$ with $$$N$$$ rows and $$$M$$$ columns. The rows are numbered $$$1, 2, \ldots N$$$, and the columns are numbered $$$1, 2, \ldots M$$$. For each $$$1 \le i \le N$$$ and $$$1 \le j \le M$$$, let $$$(i, j)$$$ denote the cell with row number $$$i$$$ and column number $$$j$$$.

Initially, all of its $$$N \times M$$$ cells are colored white. You want to do the following operation exactly $$$K$$$ times:

  • Choose a rectangular submatrix and paint it black. More formally, choose integers $$$L_1, R_1, L_2, R_2$$$ satisfying $$$1 \le L_1 \le R_1 \le N$$$ and $$$1 \le L_2 \le R_2 \le M$$$, and for each cell $$$(i, j)$$$ satisfying, $$$L_1 \le i \le R_1$$$, $$$L_2 \le j \le R_2$$$, paint it black.

You have a target $$$N \times M$$$ matrix $$$C$$$ consisting of $$$0$$$ and $$$1$$$. For each cell $$$(i, j)$$$, if $$$C_{i,j} = 1$$$, you want the cell to be colored black in the matrix $$$A$$$ and otherwise you want it to remain white.

Find if there exist $$$K$$$ operations that color the matrix as required. Note that, it is allowed to do the same operation multiple times.

Input

The first line contains $$$N, M (1 \le N, M \le 1000)$$$ and $$$K (1 \le K \le 4)$$$, the number of rows, the number of columns, and the required number of operations, respectively.

For each $$$1 \le i \le N$$$, the $$$i^{\text{th}}$$$ of the next $$$N$$$ lines contains a binary string of length $$$M$$$, whose $$$j^{\text{th}}$$$ character is $$$1$$$ if $$$C_{i,j}=1$$$ and $$$0$$$ otherwise.

Output

Print "Yes" (without quotes) if it is possible to convert the matrix into the required state after K operations and "No" (without quotes) otherwise.

Note that the output is case insensitive. So, you can also print "yEs" instead of "Yes".

Examples
Input
5 6 2
000000
001100
001111
000111
000000
Output
Yes
Input
5 6 1
000000
001100
001111
000111
000000
Output
No
Note

In the first sample:

In the first operation, choose the submatrix $$$[2, 3] \times [3, 4]$$$

In the second operation, choose the submatrix $$$[3, 4] \times [4, 6]$$$

In the second sample, one can not obtain the required coloring in $$$1$$$ operation.

J. Removing Leaves
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

For a rooted tree $$$T$$$, define the depth of a node to be the number of edges on the path from the root to it. Also, define $$$f(T)$$$ as the sum of depths of all the nodes in the tree.

You are given a rooted tree $$$T$$$ with $$$N$$$ nodes numbered $$$1, 2, \ldots, N$$$, rooted at node $$$1$$$. For $$$i \ge 2$$$, the parent of node $$$i$$$ is $$$p_i$$$. It is given that $$$p_i \lt i$$$ for all $$$i \ge 2$$$.

You want to remove all nodes from the tree one by one until only the root remains. In a single step, you can remove any leaf node (a non-root node which has exactly one adjacent node). Note that a node which is not a leaf node initially can become a leaf node later and become eligible for removal.

In this process, let $$$T_0$$$(the original tree), $$$T_1, T_2, \ldots T_{n-1}$$$(the tree with only the root remaining) be the sequence of trees formed. In other words, $$$T_i$$$ is the tree formed after $$$i$$$ nodes have been removed. What is the maximum possible value of $$$\displaystyle \sum_{i=0}^{n-1} f(T_i)$$$?

Input

The first line contains $$$Q(1 \le Q \le 40)$$$, the number of test-cases. Then the testcases follow, each containing two lines:

  • The first line contains $$$N(1 \le N \le 40)$$$, the number of nodes in the tree.
  • The second line contains $$$N - 1$$$ space separated integers $$$p_2, p_3, \ldots p_N$$$, such that $$$1 \le p_i \lt i$$$ for all $$$i = 2, 3, \ldots N$$$.

The sum of $$$N$$$ over all test-cases does not exceed $$$40$$$.

Output

For each testcase, print the maximum possible value of $$$\displaystyle \sum_{i=0}^{n-1} f(T_i) $$$ on a new line.

Example
Input
2
4
1 1 1
5
1 2 2 1
Output
6
15
Note

In the first testcase, the depth of node $$$1$$$ is $$$0$$$, and that of nodes $$$2, 3, 4$$$ are $$$1$$$. If we remove the nodes in the order $$$2, 3, 4$$$, the objective value is $$$(0 + 1 + 1 + 1) + (0 + 1 + 1) + (0 + 1) + 0 = 6$$$. This is the maximum possible value, and so is the answer.

In the second testcase, the depths of nodes $$$1, 2, 3, 4, 5$$$ are $$$0, 1, 2, 2, 1$$$ respectively. We can remove nodes in the order $$$5, 3, 4, 2$$$ to get the objective value as $$$6 + 5 + 3 + 1 + 0 = 15$$$. The sequence $$$T_0, T_1, \ldots T_4$$$ is :