Codeforces Round 815 (Div. 2)
A. Burenka Plays with Fractions
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Burenka came to kindergarden. This kindergarten is quite strange, so each kid there receives two fractions ($$$\frac{a}{b}$$$ and $$$\frac{c}{d}$$$) with integer numerators and denominators. Then children are commanded to play with their fractions.

Burenka is a clever kid, so she noticed that when she claps once, she can multiply numerator or denominator of one of her two fractions by any integer of her choice (but she can't multiply denominators by $$$0$$$). Now she wants know the minimal number of claps to make her fractions equal (by value). Please help her and find the required number of claps!

Input

The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then follow the descriptions of each test case.

The only line of each test case contains four integers $$$a$$$, $$$b$$$, $$$c$$$ and $$$d$$$ ($$$0 \leq a, c \leq 10^9$$$, $$$1 \leq b, d \leq 10^9$$$) — numerators and denominators of the fractions given to Burenka initially.

Output

For each test case print a single integer — the minimal number of claps Burenka needs to make her fractions equal.

Example
Input
8
2 1 1 1
6 3 2 1
1 2 2 3
0 1 0 100
0 1 228 179
100 3 25 6
999999999 300000000 666666666 100000000
33 15 0 84
Output
1
0
2
0
1
1
1
1
Note

In the first case, Burenka can multiply $$$c$$$ by $$$2$$$, then the fractions will be equal.

In the second case, fractions are already equal.

In the third case, Burenka can multiply $$$a$$$ by $$$4$$$, then $$$b$$$ by $$$3$$$. Then the fractions will be equal ($$$\frac{1 \cdot 4}{2 \cdot 3} = \frac{2}{3}$$$).

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

You are given an array $$$a$$$ that contains $$$n$$$ integers. You can choose any proper subsegment $$$a_l, a_{l + 1}, \ldots, a_r$$$ of this array, meaning you can choose any two integers $$$1 \le l \le r \le n$$$, where $$$r - l + 1 \lt n$$$. We define the beauty of a given subsegment as the value of the following expression:

$$$$$$\max(a_{1}, a_{2}, \ldots, a_{l-1}, a_{r+1}, a_{r+2}, \ldots, a_{n}) - \min(a_{1}, a_{2}, \ldots, a_{l-1}, a_{r+1}, a_{r+2}, \ldots, a_{n}) + \max(a_{l}, \ldots, a_{r}) - \min(a_{l}, \ldots, a_{r}).$$$$$$

Please find the maximum beauty among all proper subsegments.

Input

The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Then follow the descriptions of each test case.

The first line of each test case contains a single integer $$$n$$$ $$$(4 \leq n \leq 10^5)$$$ — the length of the array.

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 elements of the given array.

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

Output

For each testcase print a single integer — the maximum beauty of a proper subsegment.

Example
Input
4
8
1 2 2 3 1 5 6 1
5
1 2 3 100 200
4
3 3 3 3
6
7 8 3 1 1 8
Output
9
297
0
14
Note

In the first test case, the optimal segment is $$$l = 7$$$, $$$r = 8$$$. The beauty of this segment equals to $$$(6 - 1) + (5 - 1) = 9$$$.

In the second test case, the optimal segment is $$$l = 2$$$, $$$r = 4$$$. The beauty of this segment equals $$$(100 - 2) + (200 - 1) = 297$$$.

C. Corners
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given a matrix consisting of $$$n$$$ rows and $$$m$$$ columns. Each cell of this matrix contains $$$0$$$ or $$$1$$$.

Let's call a square of size $$$2 \times 2$$$ without one corner cell an L-shape figure. In one operation you can take one L-shape figure, with at least one cell containing $$$1$$$ and replace all numbers in it with zeroes.

Find the maximum number of operations that you can do with the given matrix.

Input

The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 500$$$) — the number of test cases. Then follow the descriptions of each test case.

The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq n, m \leq 500$$$) — the size of the matrix.

Each of the following $$$n$$$ lines contains a binary string of length $$$m$$$ — the description of the matrix.

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

Output

For each test case output the maximum number of operations you can do with the given matrix.

Example
Input
4
4 3
101
111
011
110
3 4
1110
0111
0111
2 2
00
00
2 2
11
11
Output
8
9
0
2
Note

In the first testcase one of the optimal sequences of operations is the following (bold font shows l-shape figure on which operation was performed):

  • Matrix before any operation was performed:
    101
    111
    011
    110
  • Matrix after $$$1$$$ operation was performed:
    100
    101
    011
    110
  • Matrix after $$$2$$$ operations were performed:
    100
    100
    011
    110
  • Matrix after $$$3$$$ operations were performed:
    100
    100
    010
    110
  • Matrix after $$$4$$$ operations were performed:
    100
    000
    010
    110
  • Matrix after $$$5$$$ operations were performed:
    100
    000
    010
    100
  • Matrix after $$$6$$$ operations were performed:
    100
    000
    000
    100
  • Matrix after $$$7$$$ operations were performed:
    000
    000
    000
    100
  • Matrix after $$$8$$$ operations were performed:
    000
    000
    000
    000

In the third testcase from the sample we can not perform any operation because the matrix doesn't contain any ones.

In the fourth testcase it does not matter which L-shape figure we pick in our first operation. We will always be left with single one. So we will perform $$$2$$$ operations.

D1. Xor-Subsequence (easy version)
time limit per test
2 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

It is the easy version of the problem. The only difference is that in this version $$$a_i \le 200$$$.

You are given an array of $$$n$$$ integers $$$a_0, a_1, a_2, \ldots a_{n - 1}$$$. Bryap wants to find the longest beautiful subsequence in the array.

An array $$$b = [b_0, b_1, \ldots, b_{m-1}]$$$, where $$$0 \le b_0 \lt b_1 \lt \ldots \lt b_{m - 1} \lt n$$$, is a subsequence of length $$$m$$$ of the array $$$a$$$.

Subsequence $$$b = [b_0, b_1, \ldots, b_{m-1}]$$$ of length $$$m$$$ is called beautiful, if the following condition holds:

  • For any $$$p$$$ ($$$0 \le p \lt m - 1$$$) holds: $$$a_{b_p} \oplus b_{p+1} \lt a_{b_{p+1}} \oplus b_p$$$.

Here $$$a \oplus b$$$ denotes the bitwise XOR of $$$a$$$ and $$$b$$$. For example, $$$2 \oplus 4 = 6$$$ and $$$3 \oplus 1=2$$$.

Bryap is a simple person so he only wants to know the length of the longest such subsequence. Help Bryap and find the answer to his question.

Input

The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 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 \leq n \leq 3 \cdot 10^5$$$) — the length of the array.

The second line of each test case contains $$$n$$$ integers $$$a_0,a_1,...,a_{n-1}$$$ ($$$0 \leq a_i \leq 200$$$) — the elements of the array.

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

Output

For each test case print a single integer — the length of the longest beautiful subsequence.

Example
Input
3
2
1 2
5
5 2 4 3 1
10
3 8 8 2 9 1 6 2 8 3
Output
2
3
6
Note

In the first test case, we can pick the whole array as a beautiful subsequence because $$$1 \oplus 1 \lt 2 \oplus 0$$$.

In the second test case, we can pick elements with indexes $$$1$$$, $$$2$$$ and $$$4$$$ (in $$$0$$$-indexation). For this elements holds: $$$2 \oplus 2 \lt 4 \oplus 1$$$ and $$$4 \oplus 4 \lt 1 \oplus 2$$$.

D2. Xor-Subsequence (hard version)
time limit per test
2 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

It is the hard version of the problem. The only difference is that in this version $$$a_i \le 10^9$$$.

You are given an array of $$$n$$$ integers $$$a_0, a_1, a_2, \ldots a_{n - 1}$$$. Bryap wants to find the longest beautiful subsequence in the array.

An array $$$b = [b_0, b_1, \ldots, b_{m-1}]$$$, where $$$0 \le b_0 \lt b_1 \lt \ldots \lt b_{m - 1} \lt n$$$, is a subsequence of length $$$m$$$ of the array $$$a$$$.

Subsequence $$$b = [b_0, b_1, \ldots, b_{m-1}]$$$ of length $$$m$$$ is called beautiful, if the following condition holds:

  • For any $$$p$$$ ($$$0 \le p \lt m - 1$$$) holds: $$$a_{b_p} \oplus b_{p+1} \lt a_{b_{p+1}} \oplus b_p$$$.

Here $$$a \oplus b$$$ denotes the bitwise XOR of $$$a$$$ and $$$b$$$. For example, $$$2 \oplus 4 = 6$$$ and $$$3 \oplus 1=2$$$.

Bryap is a simple person so he only wants to know the length of the longest such subsequence. Help Bryap and find the answer to his question.

Input

The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 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 \leq n \leq 3 \cdot 10^5$$$) — the length of the array.

The second line of each test case contains $$$n$$$ integers $$$a_0,a_1,...,a_{n-1}$$$ ($$$0 \leq a_i \leq 10^9$$$) — the elements of the array.

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

Output

For each test case print a single integer — the length of the longest beautiful subsequence.

Example
Input
3
2
1 2
5
5 2 4 3 1
10
3 8 8 2 9 1 6 2 8 3
Output
2
3
6
Note

In the first test case, we can pick the whole array as a beautiful subsequence because $$$1 \oplus 1 \lt 2 \oplus 0$$$.

In the second test case, we can pick elements with indexes $$$1$$$, $$$2$$$ and $$$4$$$ (in $$$0$$$ indexation). For this elements holds: $$$2 \oplus 2 \lt 4 \oplus 1$$$ and $$$4 \oplus 4 \lt 1 \oplus 2$$$.

E. Misha and Paintings
time limit per test
3.5 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Misha has a square $$$n \times n$$$ matrix, where the number in row $$$i$$$ and column $$$j$$$ is equal to $$$a_{i, j}$$$. Misha wants to modify the matrix to contain exactly $$$k$$$ distinct integers. To achieve this goal, Misha can perform the following operation zero or more times:

  1. choose any square submatrix of the matrix (you choose $$$(x_1,y_1)$$$, $$$(x_2,y_2)$$$, such that $$$x_1 \leq x_2$$$, $$$y_1 \leq y_2$$$, $$$x_2 - x_1 = y_2 - y_1$$$, then submatrix is a set of cells with coordinates $$$(x, y)$$$, such that $$$x_1 \leq x \leq x_2$$$, $$$y_1 \leq y \leq y_2$$$),
  2. choose an integer $$$k$$$, where $$$1 \leq k \leq n^2$$$,
  3. replace all integers in the submatrix with $$$k$$$.

Please find the minimum number of operations that Misha needs to achieve his goal.

Input

The first input line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \leq n \leq 500, 1 \leq k \leq n^2$$$)  — the size of the matrix and the desired amount of distinct elements in the matrix.

Then $$$n$$$ lines follows. The $$$i$$$-th of them contains $$$n$$$ integers $$$a_{i, 1}, a_{i, 2}, \ldots, a_{i, n}$$$ ($$$1 \leq a_{i,j} \leq n^2$$$) — the elements of the $$$i$$$-th row of the matrix.

Output

Output one integer — the minimum number of operations required.

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

In the first test case the answer is $$$1$$$, because one can change the value in the bottom right corner of the matrix to $$$1$$$. The resulting matrix can be found below:

111
112
341

In the second test case the answer is $$$2$$$. First, one can change the entire matrix to contain only $$$1$$$s, and the change the value of any single cell to $$$2$$$. One of the possible resulting matrices is displayed below:

111
111
112