Codeforces Round 845 (Div. 2) and ByteRace 2023
A. Everybody Likes Good Arrays!
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

An array $$$a$$$ is good if for all pairs of adjacent elements, $$$a_i$$$ and $$$a_{i+1}$$$ ($$$1\le i \lt n$$$) are of different parity. Note that an array of size $$$1$$$ is trivially good.

You are given an array of size $$$n$$$.

In one operation you can select any pair of adjacent elements in which both elements are of the same parity, delete them, and insert their product in the same position.

Find the minimum number of operations to form a good array.

Input

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

The first line of each test case contains an integer $$$n$$$ ($$$1 \le n \le 100$$$).

The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le 10^{9}$$$).

Output

For each test case print an integer, the minimum number of operations required to form a good array.

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

Consider the first test case. Select the $$$2$$$-nd and the $$$3$$$-rd integers and apply the operation on them. The array changes from $$$[1, \color{red}{7}, \color{red}{11}, 2, 13]$$$ to $$$[1, \color{red}{77}, 2, 13]$$$. Next, select the $$$1$$$-st and the $$$2$$$-nd integers, array changes from $$$[\color{red}{1}, \color{red}{77}, 2, 13]$$$ to $$$[\color{red}{77}, 2, 13]$$$. Thus we require $$$2$$$ operations. It can be proved that this is the minimum number of operations.

In the second test case, the given array is already good. So we require $$$0$$$ operations.

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

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 ($$$n=3$$$ but there is $$$4$$$ in the array). There are $$$n! = n \cdot (n-1) \cdot (n - 2) \cdot \ldots \cdot 1$$$ different permutations of length $$$n$$$.

Given a permutation $$$p$$$ of $$$n$$$ numbers, we create an array $$$a$$$ consisting of $$$2n$$$ numbers, which is equal to $$$p$$$ concatenated with its reverse. We then define the beauty of $$$p$$$ as the number of inversions in $$$a$$$.

The number of inversions in the array $$$a$$$ is the number of pairs of indices $$$i$$$, $$$j$$$ such that $$$i \lt j$$$ and $$$a_i \gt a_j$$$.

For example, for permutation $$$p = [1, 2]$$$, $$$a$$$ would be $$$[1, 2, 2, 1]$$$. The inversions in $$$a$$$ are $$$(2, 4)$$$ and $$$(3, 4)$$$ (assuming 1-based indexing). Hence, the beauty of $$$p$$$ is $$$2$$$.

Your task is to find the sum of beauties of all $$$n!$$$ permutations of size $$$n$$$. Print the remainder we get when dividing this value by $$$1\,000\,000\,007$$$ ($$$10^9 + 7$$$).

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.

Each test case has only one line — the integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$).

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

Output

For each test case, print one integer — the sum of beauties of all permutations of size $$$n$$$ modulo $$$1\,000\,000\,007$$$ ($$$10^9 + 7$$$).

Example
Input
3
1
2
100
Output
0
4
389456655
Note

For the first test case of the example, $$$p = [1]$$$ is the only permutation. $$$a = [1, 1]$$$ has $$$0$$$ inversions.

For the second test case of the example, the permutations are $$$[1, 2]$$$ and $$$[2, 1]$$$. Their respective $$$a$$$ arrays are $$$[1, 2, 2, 1]$$$ and $$$[2, 1, 1, 2]$$$, both of which have $$$2$$$ inversions.

C. Quiz Master
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

A school has to decide on its team for an international quiz. There are $$$n$$$ students in the school. We can describe the students using an array $$$a$$$ where $$$a_i$$$ is the smartness of the $$$i$$$-th ($$$1 \le i \le n$$$) student.

There are $$$m$$$ topics $$$1, 2, 3, \ldots, m$$$ from which the quiz questions will be formed. The $$$i$$$-th student is considered proficient in a topic $$$T$$$ if $$$(a_i \bmod T) = 0$$$. Otherwise, he is a rookie in that topic.

We say that a team of students is collectively proficient in all the topics if for every topic there is a member of the team proficient in this topic.

Find a team that is collectively proficient in all the topics such that the maximum difference between the smartness of any two students in that team is minimized. Output this difference.

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 $$$n$$$ and $$$m$$$ ($$$1 \le n,m \le 10^5$$$).

The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 10^5$$$).

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

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

Output

For each test case, print the answer on a new line. If there is no solution, output $$$-1$$$.

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

In the first test case, we have participants with smartnesses $$$3$$$ and $$$7$$$, and $$$m = 4$$$. Thus, there is no student with smartness divisible by $$$2$$$. Since $$$2 \leq m$$$, there is no way to choose a team.

In the second test case, we can select the participant with smartness $$$2$$$ to be the only one on the team. This way the team will be collectively proficient in both topics $$$1$$$ and $$$2$$$.

In the third test case, consider the team with participants of smartnesses $$$4, 5, 6, 7$$$. This way the team will be collectively proficient in all topics $$$1, 2, \ldots, 7$$$.

D. Score of a Tree
time limit per test
2 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

You are given a tree of $$$n$$$ nodes, rooted at $$$1$$$. Every node has a value of either $$$0$$$ or $$$1$$$ at time $$$t=0$$$.

At any integer time $$$t \gt 0$$$, the value of a node becomes the bitwise XOR of the values of its children at time $$$t - 1$$$; the values of leaves become $$$0$$$ since they don't have any children.

Let $$$S(t)$$$ denote the sum of values of all nodes at time $$$t$$$.

Let $$$F(A)$$$ denote the sum of $$$S(t)$$$ across all values of $$$t$$$ such that $$$0 \le t \le 10^{100}$$$, where $$$A$$$ is the initial assignment of $$$0$$$s and $$$1$$$s in the tree.

The task is to find the sum of $$$F(A)$$$ for all $$$2^n$$$ initial configurations of $$$0$$$s and $$$1$$$s in the tree. Print the sum 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 10^5$$$). The description of the test cases follows.

The first line of each test case contains $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of nodes in the tree.

The next $$$n-1$$$ lines of each test case contain two integers each — $$$u$$$, $$$v$$$ indicating an edge between $$$u$$$ and $$$v$$$ ($$$1 \le u, v \le n$$$).

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

Output

Output the sum modulo $$$10^9+7$$$ for each test case.

Example
Input
1
6
1 2
1 3
3 4
3 5
3 6
Output
288
Note

Let us find $$$F(A)$$$ for the configuration $$$A = [0,1,0,0,1,1]$$$ ($$$A[i]$$$ denotes the value of node $$$i$$$). Initially (at $$$t = 0$$$) our tree is as shown in the picture below. In each node, two values are shown: the number and the value of this node. $$$S(0)$$$ for this configuration is $$$3$$$.

At $$$t = 1$$$ the configuration changes to $$$[1,0,0,0,0,0]$$$. The tree looks as shown below. $$$S(1) = 1$$$.

At $$$t = 2$$$ the configuration changes to $$$[0,0,0,0,0,0]$$$. The tree looks as shown below. $$$S(2) = 0$$$.

For all $$$t \gt 2$$$, the graph remains unchanged, so $$$S(t)=0$$$ for all $$$t \gt 2$$$. So, for the initial configuration $$$A = [0,1,0,0,1,1]$$$, the value of $$$F(A) = 3 + 1 = 4$$$.

Doing this process for all possible $$$2^{6}$$$ configurations yields us an answer of $$$\textbf{288}$$$.

E. Edge Reverse
time limit per test
4 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

You will be given a weighted directed graph of $$$n$$$ nodes and $$$m$$$ directed edges, where the $$$i$$$-th edge has a weight of $$$w_i$$$ ($$$1 \le i \le m$$$).

You need to reverse some edges of this graph so that there is at least one node in the graph from which every other node is reachable. The cost of these reversals is equal to the maximum weight of all reversed edges. If no edge reversal is required, assume the cost to be $$$0$$$.

It is guaranteed that no self-loop or duplicate edge exists.

Find the minimum cost required for completing the task. If there is no solution, print a single integer $$$-1$$$.

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.

Each test case begins with a line containing two integers $$$n$$$ and $$$m$$$ ($$$2 \le n \le 2 \cdot 10^5$$$, $$$1 \le m \le 2 \cdot 10^5$$$) — the number of nodes in the graph and the number of edges in the graph.

The next $$$m$$$ lines of each test case contain $$$3$$$ integers each — $$$u$$$, $$$v$$$, $$$w$$$ ($$$1 \le u, v \le n$$$, $$$1 \le w \le 10^9$$$), indicating an edge from $$$u$$$ to $$$v$$$ with a weight of $$$w$$$. It is guaranteed that no edge connects a vertex to itself, and no pair of edges share the same origin and destination simultaneously.

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

Output

For each test case, output the minimum cost. If there is no solution, print $$$-1$$$.

Example
Input
4
2 1
1 2 3
5 4
1 2 10
2 3 10
3 1 10
4 5 10
4 5
1 2 10000
2 3 20000
1 3 30000
4 2 500
4 3 20
4 5
1 2 10000
2 3 20000
1 3 30000
4 2 5
4 3 20
Output
0
-1
20
5
Note

In the first test case, an edge exists from $$$1$$$ to $$$2$$$, so all nodes are reachable (from $$$1$$$).

In the second test case, no nodes are reachable from any node no matter what edges we reverse, so the answer is $$$-1$$$.

In the third test case, reversing the $$$4$$$-th or $$$5$$$-th edge allows all nodes to be reachable from $$$1$$$. We choose the $$$5$$$-th edge here because its weight is smaller.

F. Comfortably Numb
time limit per test
8 seconds
memory limit per test
1024 megabytes
input
standard input
output
standard output

You are given an array $$$a$$$ consisting of $$$n$$$ non-negative integers.

The numbness of a subarray $$$a_l, a_{l+1}, \ldots, a_r$$$ (for arbitrary $$$l \leq r$$$) is defined as $$$$$$\max(a_l, a_{l+1}, \ldots, a_r) \oplus (a_l \oplus a_{l+1} \oplus \ldots \oplus a_r),$$$$$$ where $$$\oplus$$$ denotes the bitwise XOR operation.

Find the maximum numbness over all subarrays.

Input

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

The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$).

The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \leq a_i \leq 10^9$$$).

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 one integer — the maximum numbness over all subarrays of the given array.

Example
Input
2
5
1 2 3 4 5
3
10 47 52
Output
7
47
Note

For the first test case, for the subarray $$$[3, 4, 5]$$$, its maximum value is $$$5$$$. Hence, its numbness is $$$3 \oplus 4 \oplus 5 \oplus 5$$$ = $$$7$$$. This is the maximum possible numbness in this array.

In the second test case the subarray $$$[47, 52]$$$ provides the maximum numbness.