OCPC 2023 Winter, Day 4: Jatin Garg Contest 1
A. Charged Intervals
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given $$$n$$$ intervals $$$[l_i, r_i]$$$. Assign a charge of $$$+1$$$ or $$$-1$$$ to each interval such that for every integer $$$i$$$ the following holds:

The sum of charges of intervals containing $$$i$$$ is $$$+1$$$, $$$-1$$$ or $$$0$$$. If no intervals contain $$$i$$$, this sum of charges is considered to be $$$0$$$.

The interval $$$[l, r]$$$ is said to contain an integer $$$j$$$ if and only if $$$l \le j \le r$$$.

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 each test case follows.

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

The next $$$n$$$ lines each contain two integers $$$l_i$$$ and $$$r_i$$$ ($$$0 \le l \le r \le 2 \cdot 10^5$$$).

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

Output

For each test case, if there doesn't exist an assignment that satisfies the above constraints, print NO. Otherwise, print YES followed by $$$n$$$ integers $$$c_1, c_2, \dots, c_n$$$ ($$$c_i \in \{-1, 1\}$$$) on a new line — the charges assigned to the intervals in the order that they appear in the input.

Example
Input
4
4
10 27
8 24
6 19
21 22
1
12 12
3
2 26
11 14
17 29
2
8 23
22 29
Output
YES
1 -1 1 1 
YES
1 
YES
1 -1 -1 
YES
1 -1 

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

Alice has $$$n$$$ positive integers $$$a_1, a_2, \dots, a_n$$$. Bob wants to secretly modify these integers by picking a positive integer $$$x$$$ and replacing $$$a_i$$$ with $$$a_i | x$$$ for every valid $$$i$$$. Here, $$$|$$$ represents the bitwise OR operation.

Alice doesn't notice any change if the resulting sequence is a permutation of $$$a$$$. Find the number of choices of $$$x$$$ such that Bob's modification goes unnoticed by Alice.

Input

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

The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the number of integers that Alice has.

The second line contains $$$n$$$ space separated integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$).

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

Output

For each test case, print a single integer on a new line, the answer to the problem.

Example
Input
2
5
15 9 11 25 13
4
3 5 9 17
Output
3
1

C. Counting Pops
time limit per test
4 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

Consider having two stacks of integers, one loaded with some distinct integers and the other empty. The following operation is performed until both stacks become empty:

  • Let $$$m$$$ be the minimum element among all integers in both the stacks.
  • If $$$m$$$ is at the top of its stack, pop it. Otherwise, pop the top element of $$$m$$$'s stack and push it into the other one.

For a sequence $$$S$$$ of distinct integers, define $$$f(S)$$$ as the number of operations performed if the first stack is loaded with $$$S$$$.

Given a permutation $$$p$$$ of length $$$n$$$, compute the sum of $$$f(S)$$$ over all non-empty subsequences $$$S$$$ of $$$p$$$.

Input

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

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

The second line contains $$$n$$$ integers $$$p_1, p_2, \dots, p_n$$$. It is guaranteed that $$$p$$$ is a permutation of $$$1$$$ to $$$n$$$.

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 on a new line, the sum of $$$f(S)$$$ over all non-empty subsequences $$$S$$$ of $$$p$$$ modulo $$$10^9+7$$$.

Example
Input
5
3
2 3 1
4
2 4 1 3
5
4 1 3 5 2
2
1 2
7
3 5 4 7 1 6 2
Output
14
44
121
5
734

D. Divided Xor
time limit per test
4 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

Given a permutation $$$p$$$ of length $$$n$$$, consider the undirected simple graph $$$G$$$ with $$$n$$$ vertices constructed as follows.

  • Vertices $$$i$$$ and $$$j$$$ ($$$i \lt j$$$) are connected if and only if for all $$$k$$$ between $$$i$$$ and $$$j$$$ it holds that $$$p_k \lt \min(p_i, p_j)$$$.

Given an integer $$$K$$$ and $$$n$$$ non-negative integers $$$b_1, b_2, \dots b_n$$$, compute the number of non-empty subsequences $$$s$$$ of $$$\{1, 2, \dots n\}$$$ such that

$$$$$$b_{s_1} \oplus b_{s_2} \oplus \dots b_{s_{|s|}} = K$$$$$$

and the set of nodes $$$s$$$ forms an independent set in $$$G$$$. Here, $$$\oplus$$$ represents the bitwise XOR operation.

Input

The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases.

The first line of each test case contains two integers $$$n$$$ and $$$K$$$ ($$$2 \le n \le 10^3, 0 \le K \lt 2^{10}$$$).

The second and third lines of each test case contain the permutation $$$p$$$ and the sequence $$$b$$$ respectively ($$$1 \le p_i \le n, 0 \le b_i \lt 2^{10}$$$). It is guaranteed that $$$p$$$ is a permutation.

It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^3$$$.

Output

For each test case, print a single integer on a new line, the answer to the problem modulo $$$10^9 + 7$$$.

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

In the first test case, the $$$5$$$ independent sets satisfying the conditions above are $$$\{1\}$$$, $$$\{2\}$$$, $$$\{3\}$$$, $$$\{4\}$$$ and $$$\{1, 5\}$$$.

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

You are given an array $$$a$$$ consisting of $$$n$$$ positive integers. You can perform the following two kinds of operations.

  1. Choose an index $$$i$$$ ($$$1 \le i \le n$$$) and multiply $$$a_i$$$ by $$$2$$$.
  2. Choose an integer $$$x$$$ ($$$-10^9 \le x \le 10^9$$$) and subtract it from all elements in the array.

Find any sequence of at most $$$20 n$$$ operations such that all elements of $$$a$$$ become equal after performing them.

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 contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the number of elements in the array.

The second line contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le 10^6$$$) — the elements of the array.

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

Output

For each test case, on the first line print $$$k$$$ ($$$0 \le k \le 20 n$$$) — the number of operations to be performed, followed by $$$k$$$ lines each of which are one of the following two kinds.

  1. If the operation is of the first kind, print 1 i where $$$i$$$ ($$$1 \le i \le n$$$) is the chosen index.
  2. If the operation is of the second kind, print 2 x where $$$x$$$ ($$$-10^9 \le x \le 10^9$$$) is the integer to be subtracted from all elements of $$$a$$$.

Note that at all times, every integer in $$$a$$$ must satisfy $$$-10^9 \le a_i \le 10^9$$$.

If there are multiple possible sequences of operations, you may print any of them.

Example
Input
3
5
4 19 5 7 11
3
296 168 48
2
99 99
Output
11
2 3
1 1
1 1
1 1
1 1
1 3
1 3
1 3
1 4
1 4
1 5
7
2 40
1 2
1 3
1 3
1 3
1 3
1 3
0

F. Good Substrings
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

For string $$$s$$$ of lowercase Latin letters, we start with an empty string $$$t$$$ and perform the following operation for each character $$$c$$$ of $$$s$$$ in left-to-right order:

  • If $$$t$$$ is empty, push $$$c$$$ at the back of $$$t$$$.
  • Otherwise if the last character of $$$t$$$ is equal to $$$c$$$, pop it.
  • Otherwise (it is different), push the last character of $$$t$$$ at the back of $$$t$$$.

The string $$$s$$$ is considered good if the corresponding string $$$t$$$ is empty after the above sequence of operations. Given a string $$$s$$$ count the number of good substrings of $$$s$$$.

Input

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

Each test case consists of a single string $$$s$$$ ($$$1 \le |s| \le 2 \cdot 10^6$$$).

It is guaranteed that the sum of $$$|s|$$$ over all test cases doesn't exceed $$$2 \cdot 10^6$$$.

Output

For each test case, print a single integer — the number of good substrings of $$$s$$$.

Example
Input
5
abbcbabbaa
ppqpqqpqp
zzz
helloosijek
rreeerer
Output
8
4
2
3
4

$$$k$$$-strings
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Given a string $$$s$$$ of lower case Latin letters and an integer $$$k$$$ ($$$1 \le k \le |s|$$$), construct a string $$$t$$$, also of lowercase Latin letters and of the same length as $$$s$$$, such that $$$\mathrm{LCS}(s, t) = k$$$.

Note: $$$\mathrm{LCS}(s, t)$$$ stands for the length of the longest common subsequence of strings $$$s$$$ and $$$t$$$.

Input

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

The first line of each test case contains two integers $$$|s|$$$ and $$$k$$$ ($$$1 \le k \le |s| \le 5000$$$).

The second line of each test case contains the string $$$s$$$.

It is guaranteed that the sum of $$$|s|$$$ over all test cases does not exceed $$$5000$$$.

Output

For each test case, if there exists any string $$$t$$$ satisfying the above constraints, print it. Otherwise print $$$-1$$$.

Example
Input
2
5 3
apple
5 1
juice
Output
pleaa
aaase

H. Max Min LCS
time limit per test
1 second
memory limit per test
512 megabytes
input
standard input
output
standard output

You are given a string $$$s$$$ of lowercase Latin letters. You need to rearrange the characters of $$$s$$$ to form a new string $$$t$$$ such that the following value is maximized:

$$$$$$\min(\mathrm{LCS}(s, t), \mathrm{LCS}(s, \overline{t})).$$$$$$

Note:

  • $$$\mathrm{LCS}(s, t)$$$ equals the length of the longest common subsequence of the strings $$$s$$$ and $$$t$$$.
  • $$$\overline{t}$$$ represents the reversed string of $$$t$$$. For example, if $$$t=abdc$$$, $$$\overline{t} = cdba$$$.
Input

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

Each test case consists of a single integer $$$n$$$ ($$$1 \le n \le 5 \cdot 10^3$$$) — the length of the string $$$s$$$ followed by $$$s$$$ on a new line.

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

Output

For each test case, print a single integer — the maximum possible value of the above expression among all rearrangements $$$t$$$ of $$$s$$$.

Example
Input
5
4
abcd
5
abcba
3
aaa
12
adhocsmatter
7
abccabc
Output
2
5
3
7
5

I. Nim X2
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

There are $$$n$$$ piles of stones. Two players take turns. In his turn, the player must do the following:

  • Choose exactly two different non-empty piles, and take from each of them exactly one stone.

The player who cannot make a move loses. Who will win if the game is played correctly?

Input

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

The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 5$$$)  — the number of piles. Note the small bound on $$$n$$$!

The second line of each input contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ $$$(1 \le a_i \le 10^9)$$$  — the numbers of stones in the piles.

Output

For each set of input data, if the first player (the player who starts) wins, print First. Otherwise, print Second.

Example
Input
5
2
2 2
2
3 5
3
1 42 33
4
2023 2023 2023 2023
5
10 10 10 10 11
Output
Second
First
First
Second
First

J. Subarray Reversals
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given a sequence of $$$n$$$ non-negative integers $$$a$$$. You can perform the following operation on it.

Pick a pair of indices $$$l, r$$$ $$$(1 \le l \le r \le n)$$$ such that $$$a_l \oplus a_{l+1} \oplus \dots a_r \neq 0$$$ and reverse the subarray $$$a_l, a_{l+1}, \dots a_r$$$. Here, $$$\oplus$$$ represents the bitwise XOR operation.

Given $$$b$$$ which is a permutation of $$$a$$$, find a sequence of at most $$$2n$$$ such operations that transform $$$a$$$ into $$$b$$$. It can be shown that such a sequence always exists.

Input

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

The first line contains a single integer $$$n$$$ ($$$1 \le n \le 5 \cdot 10^3$$$) — the number of integers in $$$a$$$.

The next two lines contain $$$n$$$ space separated integers each, the elements of $$$a$$$ and $$$b$$$ respectively ($$$0 \le a_i, b_i \le 10^9$$$). It is guaranteed that $$$b$$$ is a permutation of $$$a$$$.

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

Output

For each test case, print $$$k$$$ ($$$0 \le k \le 2 \cdot n$$$) — the number of operations to be performed on $$$a$$$ followed by $$$k$$$ lines containing integers $$$l_i$$$ and $$$r_i$$$ $$$(1 \le l_i \le r_i \le n)$$$, indicating that in the $$$i^\text{th}$$$ operation the subarray $$$[l_i, r_i]$$$ of $$$a$$$ is to be reversed.

Example
Input
3
3
1 2 1
2 1 1
5
1 5 2 4 3
2 3 5 1 4
4
3 2 7 3
7 3 3 2
Output
1
1 2
3
2 4
1 3
2 5
2
2 4
1 3

K. Unique Tournaments
time limit per test
0.5 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

A multiset $$$S$$$ is said to be $$$n$$$-admissible if there exists some tournament graph on $$$n$$$ vertices whose multiset of outdegrees is $$$S$$$.

An $$$n$$$-admissible multiset $$$S$$$ is good if there exists a unique tournament graph on $$$n$$$ vertices (up to isomorphism) whose multiset of outdegrees equals $$$S$$$.

Find the number of good $$$n$$$-admissible multisets with all elements being even integers.

Input

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

The only line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 10^{18})$$$.

Output

For each test case, print a single integer on a new line, the number of good $$$n$$$-admissible multisets with all elements being even integers, modulo $$$10^9 + 7$$$.

Example
Input
5
4
6
3
8
121
Output
1
0
0
2
2178309
Note

A tournament graph is a directed graph with no self-loops such that for each pair of vertices $$$u \ne v$$$, exactly one of the edges $$$u \to v$$$ and $$$v \to u$$$ exists.