Game of Coders 5.0 | Finals Round
A. Remaining Towers
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Luffy and Zoro are exploring a ruined island containing a row of $$$n$$$ ancient stone towers. The height of the tower at position $$$i$$$ is $$$a_i$$$.

For each query $$$[l,r]$$$, consider only the towers whose positions are between $$$l$$$ and $$$r$$$, inclusive. Towers outside this segment do not affect the query.

The destruction happens in rounds. In each round, a remaining tower at position $$$i$$$ crumbles if there exist two other remaining towers at positions $$$j$$$ and $$$k$$$ such that $$$l \le j \lt i \lt k \le r$$$ and $$$a_j \gt a_i$$$ and $$$a_k \gt a_i$$$.

In other words, a tower crumbles if there is at least one strictly taller remaining tower somewhere to its left and at least one strictly taller remaining tower somewhere to its right.

The towers at positions $$$j$$$ and $$$k$$$ do not need to be adjacent to the tower at position $$$i$$$. Any remaining towers on its left and right may satisfy the condition.

All towers satisfying the condition are removed simultaneously. The process is repeated until an entire round passes without any tower crumbling.

For each query, determine how many towers in the segment $$$[l,r]$$$ remain after the process ends.

Input

The first line contains an 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 $$$q$$$ ($$$1 \le n, q \le 2 \times 10^{5}$$$) — the number of towers and the number of queries.

The second line contains $$$n$$$ distinct integers $$$a_{1}, a_{2}, \dots, a_{n}$$$ ($$$1 \le a_{i} \le 10^{9}$$$) — the heights of the towers.

Each of the next $$$q$$$ lines contains two integers $$$l$$$ and $$$r$$$ ($$$1 \le l \le r \le n$$$) — the segment for the query.

It is guaranteed that the sum of $$$n$$$ and the sum of $$$q$$$ over all test cases do not exceed $$$2 \times 10^{5}$$$.

Output

For each query, output one integer — the number of towers that stay standing in the segment $$$[l, r]$$$.

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

For the first query, towers with heights 1, 3, and 2 disappear in the first round. The towers with heights 4 and 5 remain.

For the second query, only 2 is removed.

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

The night sky was clear, and the moon reflected beautifully off the quiet ocean. Nami and Samer Samora sat close together on the deck of the Sunny. Samer Samora looked into Nami's eyes, gently took her hand, and promised to share all his adventures, treasures, and even his debts with her.

They mapped out their future using a cyclic array $$$a_{1}, a_{2}, \dots, a_{n}$$$ that sums to 0. A rotation represents a possible starting point for their new life, reading all $$$n$$$ items in a circle.

Because Nami loves money and Samer Samora deeply loves Nami, he wants to ensure she never feels insecure. A rotation is considered "safe" if their shared wealth (the prefix sums) never drops below zero. Help Samer Samora count the number of safe starting points so he can give Nami the perfect, secure, happy ending they deserve.

Input

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

The first line of each test case contains an integer $$$n$$$ ($$$1 \le n \le 2 \times 10^{5}$$$) — the number of balance changes.

The second line contains $$$n$$$ integers $$$a_{1}, a_{2}, \dots, a_{n}$$$ ($$$-10^{9} \le a_{i} \le 10^{9}$$$) — the balance changes around the table.

It is guaranteed that $$$\sum_{i=1}^{n}a_{i}=0$$$ for each test case, and the sum of $$$n$$$ over all test cases does not exceed $$$2 \times 10^{5}$$$.

Output

For each test case, output one integer — the number of safe rotations.

Example
Input
2
3
1 -1 0
3
-1 1 0
Output
2
1
Note

In the first test case, the safe rotations are [1, -1, 0] and [0, 1, -1].

In the second test case, only the rotation [1, 0, -1] is safe.

C. Tree Score
time limit per test
5 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Chopper and Usopp find a giant plant shaped like a tree with $$$n$$$ vertices and $$$n-1$$$ branches. Vertex $$$v$$$ has a value $$$a_{v}$$$.

For each branch $$$i$$$, two values are written:

  • $$$p_{i}$$$, its contribution if the branch is kept;
  • $$$q_{i}$$$, its contribution if the branch is cut.

Consider any subset $$$K$$$ of branches. Branches in $$$K$$$ are kept, and all other branches are cut. The kept branches split the vertices into several connected components.

For a component $$$C$$$, define its value as: $$$$$$W(C) = \sum_{v \in C} a_{v}$$$$$$

The score of choice $$$K$$$ is: $$$$$$score(K) = \left(\prod_{i \in K} p_{i}\right) \left(\prod_{i \notin K} q_{i}\right) \left(\prod_{C} W(C)\right)$$$$$$ where the last product is over all connected components after cutting the branches not in $$$K$$$.

Define: $$$$$$F = \sum score(K)$$$$$$ In other words, $$$F$$$ is the sum of $$$score(K)$$$ over all $$$2^{n-1}$$$ possible choices of which branches are kept and which are cut.

All calculations are performed modulo 998244353.

You must process $$$Q$$$ updates. After each update, output the current value of $$$F$$$.

There are two types of updates:

  • $$$1\ v\ x$$$: set $$$a_{v}=x$$$.
  • $$$2\ i\ p\ q$$$: set $$$p_{i}=p$$$ and $$$q_{i}=q$$$.
Branches are numbered from 1 to $$$n-1$$$ in their input order.
Input

The first line contains two integers $$$n$$$ and $$$Q$$$ ($$$1 \le n, Q \le 2 \times 10^{5}$$$) — the number of vertices and the number of updates.

The second line contains $$$n$$$ integers $$$a_{1}, a_{2}, \dots, a_{n}$$$ ($$$0 \le a_{v} \lt 998244353$$$) — the vertex values.

The $$$i$$$-th of the next $$$n-1$$$ lines contains four integers $$$u_{i}$$$, $$$v_{i}$$$, $$$p_{i}$$$, and $$$q_{i}$$$ ($$$1 \le u_{i}, v_{i} \le n$$$, $$$0 \le p_{i}, q_{i} \lt 998244353$$$) — the ends and the two values of the $$$i$$$-th branch.

It is guaranteed that these branches form a tree.

Each of the next $$$Q$$$ lines contains one update in one of the following formats:

  • $$$1\ v\ x$$$ ($$$1 \le v \le n$$$, $$$0 \le x \lt 998244353$$$).
  • $$$2\ i\ p\ q$$$ ($$$1 \le i \le n-1$$$, $$$0 \le p, q \lt 998244353$$$).
Output

After each update, output one integer — the current value of $$$F$$$ modulo 998244353.

Example
Input
2 2
1 2
1 2 3 5
1 1 4
2 1 1 2
Output
58
22
Note

After the first update, the vertex values are 4 and 2.

If the edge is kept, the contribution is $$$3 \times (4+2) = 18$$$. If the edge is removed, the contribution is $$$5 \times 4 \times 2 = 40$$$. Therefore, $$$F = 18 + 40 = 58$$$.

After the second update, the edge values become $$$p_{1}=1$$$ and $$$q_{1}=2$$$, so $$$F = 1 \times (4+2) + 2 \times 4 \times 2 = 22$$$.

D. Max MST
time limit per test
0.5 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Captain Kareem Elgoker is establishing a new pirate haven in the treacherous Algorithmic Seas. He has obtained an ancient map detailing the locations of $$$n$$$ uncharted islands. Due to the mystical properties of the ocean, each island emits a unique magnetic signature, denoted by an integer $$$a_i$$$.

The Captain must select exactly $$$k$$$ of these islands to form his secret archipelago territory. To ensure his crew can travel safely without being detected by the Royal Navy, he must connect all $$$k$$$ chosen islands into a single continuous network using hidden smuggling routes.

A smuggling route can be established between any two selected islands $$$u$$$ and $$$v$$$. The "Secrecy Level" (cost) of the route between island $$$u$$$ and island $$$v$$$ is exactly the Least Significant Bit (LSB) of the absolute difference of their magnetic signatures.

Formally, the weight of the edge between $$$u$$$ and $$$v$$$ is $$$LSB(\vert{}a_u - a_v\vert{})$$$.

(Note: $$$LSB(x)$$$ is the largest power of $$$2$$$ that divides $$$x$$$. For example, $$$LSB(12) = 4$$$, and $$$LSB(7) = 1$$$.)

To build a secure base, the network of islands will be connected by its Minimum Spanning Tree (MST) based on these Secrecy Levels. However, to maximize the overall obscurity of his territory, Captain Kareem wants to select the subset of $$$k$$$ islands such that the total weight of this MST is maximized.

Given the array $$$a$$$ representing the magnetic signatures of the islands, find the maximum possible weight of the MST if the Captain optimally chooses exactly $$$k$$$ islands.

Input

The first line 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 $$$k$$$ ($$$2 \le k \le n \le 2 \cdot 10^5$$$) — the total number of islands and the number of islands Captain Kareem must select.

The second line contains $$$n$$$ distinct integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \lt 2^{30}$$$) — the magnetic signatures of the islands.

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

Output

For each test case, output a single integer — the maximum possible weight of the Minimum Spanning Tree formed by choosing exactly $$$k$$$ islands.

Example
Input
3
5 3
10 7 14 6 24
4 4
1 2 3 4
6 4
2 4 8 16 32 64
Output
8
3
24

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

Sherbiny has set sail on a grand pirate conquest! He attacks a new island every day for $$$n$$$ days. Brook stands at the bow of the ship, playing a victorious tune on his violin.

On day $$$i$$$, Sherbiny loots $$$a_{i}$$$ pieces of gold. Brook wants to write a song about the crew's massive fortune.

Find the total amount of gold Sherbiny has collected after all $$$n$$$ days so Brook can finish his masterpiece.

Input

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

The first line of each test case contains an integer $$$n$$$ ($$$1 \le n \le 2 \times 10^{5}$$$) — the number of days.

The second line contains $$$n$$$ integers $$$a_{1}, a_{2}, \dots, a_{n}$$$ ($$$0 \le a_{i} \le 10^{9}$$$) — the gold found on each day.

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

Output

For each test case, output one integer — the total amount of gold Sherbiny gets.

Example
Input
3
1
7
5
1 2 3 4 5
4
0 10 0 20
Output
7
15
30
Note

In the second test case, Sherbiny gains $$$1+2+3+4+5=15$$$ gold coins.

F. Subsequence GCD
time limit per test
5 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

Sanji is cooking a brand-new recipe in the galley, but he needs the perfect ratio of ingredients. Sherbiny gives him an array $$$a_1, a_2, \ldots, a_n$$$.

Consider all $$$2^n$$$ subsequences of the array, including the empty subsequence.

A subsequence is obtained by choosing some indices of the array and keeping the chosen elements in their original relative order. Different choices of indices are considered different subsequences, even if they produce the same sequence of values.

For a subsequence $$$b_1, b_2, \ldots, b_m$$$, define its flavor score as $$$\sum_{i=2}^{m} \gcd(b_i, b_{i-1}).$$$

We define $$$\gcd(0,0)=0$$$. The empty subsequence and every subsequence of length $$$1$$$ have a flavor score of $$$0$$$.

Sherbiny writes the flavor scores of all $$$2^n$$$ subsequences in non-increasing order. Equal scores are kept as separate entries in this order.

Sanji wants to find the $$$k$$$-th score in this order.

Input

The first line contains an integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases.

The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n, k \le 1000$$$, $$$k \le 2^{n}$$$).

The second line contains $$$n$$$ integers $$$a_{1}, a_{2}, \dots, a_{n}$$$ ($$$0 \le a_{i} \le 10^{9}$$$).

It is guaranteed that the sum of $$$n * k$$$ over all test cases does not exceed $$$2 \times 10^{6}$$$.

Output

For each test case, output one integer — the $$$k$$$-th biggest score.

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

In the first test case, the array is [4,6,2].

  • The subsequence [4, 6, 2] has value $$$gcd(4,6)+gcd(6,2)=2+2=4$$$.
  • Each of the subsequences [4, 6], [4,2], and [6, 2] has value 2.
  • Every other subsequence (including all subsequences of length at most 1, and the empty subsequence) has value 0.

Sorting all 8 values in non-increasing order gives 4, 2, 2, 2, 0, 0, 0, 0, so the 2-nd maximum is 2.

G. Pirate Team
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Luffy wants to form a team for an upcoming raid. There are $$$n$$$ pirates available, and the fighting power of the $$$i$$$-th pirate is $$$a_i$$$.

Luffy must choose a nonempty team and select exactly one pirate from that team as its captain.

After excluding the captain, the fighting powers of the remaining team members must be close to each other. More precisely, if the remaining pirates have minimum fighting power $$$x$$$ and maximum fighting power $$$y$$$, then the following condition must hold: $$$y-x \le d.$$$

A team containing only one pirate is always valid, since that pirate can be chosen as the captain.

Find the maximum possible number of pirates in a valid team.

Input

The first line contains an 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 $$$d$$$ ($$$1 \le n \le 2 \times 10^{5}$$$, $$$0 \le d \le 10^{9}$$$) — the number of pirates and the maximum difference.

The second line contains $$$n$$$ integers $$$a_{1}, a_{2}, \dots, a_{n}$$$ ($$$0 \le a_{i} \le 10^{9}$$$) — the power of the pirates.

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

Output

For each test case, output one integer — the biggest team size.

Example
Input
2
3 1
1 10 11
1 0
7
Output
3
1
Note

In the first test case, choose all three pirates. Make the pirate with strength 1 the captain, so only strengths 10 and 11 are checked.

In the second test case, the one-person team is valid.

H. Video Frames
time limit per test
4 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Franky is building a massive movie projector to watch movies on the side of the Sunny. He receives $$$n$$$ video frames sorted by time. Frame $$$i$$$ arrives at time $$$T_{i}$$$ and needs $$$C_{i}$$$ cola-power to run.

He must cut the frames into continuous groups. For a group having frames $$$L, L+1, \dots, R$$$:

  • its delay is $$$T_{R} - T_{L}$$$;
  • its load is $$$\max(C_{L}, C_{L+1}, \dots, C_{R})$$$.

The total load is the sum of the loads of all groups. Sherbiny calculates the ship's engine limit and warns Franky that the total load must be $$$M$$$ or less. Find the smallest possible max delay among all groups so the movie doesn't lag.

Input

The first line contains an 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 \le n \le 10^{5}$$$, $$$1 \le M \le 10^{14}$$$) — the number of frames and the limit.

The $$$i$$$-th of the next $$$n$$$ lines contains two integers $$$T_{i}$$$ and $$$C_{i}$$$ ($$$1 \le T_{i} \le 10^{9}$$$, $$$1 \le C_{i} \le 10^{9}$$$) — the time and power.

It is guaranteed that $$$T_{i} \lt T_{i+1}$$$. The sum of $$$n$$$ does not exceed $$$10^{5}$$$.

Output

For each test case, output one integer — the smallest possible max delay. If it is impossible, output -1.

Example
Input
2
5 15
1 5
3 4
5 8
7 2
10 6
3 10
1 15
2 5
3 20
Output
4
-1
Note

In the first test case, one valid grouping is frames 1, 2, 3 together and frames 4,5 together. The total load is $$$8+6=14$$$, and the maximum delay is 4.

In the second test case, one frame has compute requirement larger than M, so every partition has total system load greater than M. The answer is -1.

I. Valid Pairs
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Sherbiny and Elgoker just raided a marine base and stole a lockbox. To open it, they have to decipher treasure codes from an array $$$a$$$ of $$$n$$$ positive numbers.

Elgoker figures out the puzzle: a pair of places $$$(i, j)$$$ is a match if $$$1 \le i \lt j \le n$$$ and $$$a_{i} \oplus a_{j} \le \min(a_{i}, a_{j})$$$, where $$$\oplus$$$ is the bitwise XOR operation.

Help Sherbiny find the number of matches so they can pop the lock and get the loot!

Input

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

The first line of each test case contains an integer $$$n$$$ ($$$2 \le n \le 2 \times 10^{5}$$$) — the length of the array.

The second line contains $$$n$$$ integers $$$a_{1}, a_{2}, \dots, a_{n}$$$ ($$$1 \le a_{i} \lt 2^{30}$$$).

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

Output

For each test case, output one integer — the number of matches.

Example
Input
4
5
1 2 3 4 7
6
8 9 10 15 16 31
2
1 1073741823
4
5 5 5 5
Output
2
7
0
6
Note

In the first test case, the valid pairs of indices are (2,3) and (4,5). Their XOR values are $$$2\oplus3=1$$$ and $$$4\oplus7=3$$$, respectively.

In the third test case, the only pair is not valid.

In the fourth test case, every pair is valid, so the answer is $$$\frac{4\times3}{2}=6$$$.

J. String Split
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Brook is trying to write a beautiful, soulful new song using a long string of lyrics $$$s$$$.

Robin reads over his sheet music and suggests cutting the lyric string into three specific non-empty parts A, B, and C such that $$$A + B + C = s$$$ to create an echoing effect so that:

  • A is a prefix of C;
  • B is a suffix of C.

A and B can overlap inside C. Different cuts are counted as completely different musical arrangements. Help Robin find the number of ways to cut the string so Brook can sing his song.

Input

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

Each test case has one line with a string $$$s$$$ ($$$3 \le |s| \le 2 \times 10^{5}$$$). It has lowercase letters.

It is guaranteed that the sum of the lengths of $$$s$$$ over all test cases does not exceed $$$2 \times 10^{5}$$$.

Output

For each test case, output one integer — the number of ways to cut the string.

Example
Input
3
aaaaaa
aaaa
abc
Output
4
1
0
Note

For $$$s = \texttt{aaaaaa}$$$, the valid splits are:

  • $$$A = \texttt{a}$$$, $$$B = \texttt{a}$$$, $$$C = \texttt{aaaa}$$$;
  • $$$A = \texttt{a}$$$, $$$B = \texttt{aa}$$$, $$$C = \texttt{aaa}$$$;
  • $$$A = \texttt{aa}$$$, $$$B = \texttt{a}$$$, $$$C = \texttt{aaa}$$$;
  • $$$A = \texttt{aa}$$$, $$$B = \texttt{aa}$$$, $$$C = \texttt{aa}$$$.
In the last split, the matching prefix and suffix inside $$$C$$$ are the same two characters.

For $$$s = \texttt{aaaa}$$$, the valid split is $$$A = \texttt{a}$$$, $$$B = \texttt{a}$$$, and $$$C = \texttt{aa}$$$.

For $$$s = \texttt{abc}$$$, no split satisfies both conditions.

K. Point Diameter
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Zoro has somehow gotten completely lost again, this time on a flat island with $$$n$$$ landmarks. Sanji is furious and tries to guide him using a map. The $$$i$$$-th landmark is at $$$(x_{i}, y_{i})$$$.

To make things easier for Zoro's terrible sense of direction, Sanji decides to erase up to two landmarks from the map. The "lost diameter" of the map is the biggest Manhattan distance between any two remaining landmarks.

The Manhattan distance between $$$(x_{1}, y_{1})$$$ and $$$(x_{2}, y_{2})$$$ is $$$|x_{1}-x_{2}| + |y_{1}-y_{2}|$$$. If less than two landmarks are left, the diameter is 0.

Find the smallest possible lost diameter Sanji can achieve after removing at most two landmarks.

Input

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

The first line of each test case contains an integer $$$n$$$ ($$$1 \le n \le 2 \times 10^{5}$$$) — the number of points.

Each of the next $$$n$$$ lines contains two integers $$$x_{i}$$$ and $$$y_{i}$$$ ($$$-10^{9} \le x_{i}, y_{i} \le 10^{9}$$$).

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

Output

For each test case, output one integer — the smallest possible diameter.

Example
Input
2
3
0 0
10 0
0 10
4
0 0
2 0
0 2
2 2
Output
0
2
Note

In the first test case, remove (10,0) and (0,10). Only one point remains, so the diameter is 0.

In the second test case, after removing two opposite corners, the remaining two points can have Manhattan distance 2.

L. Secret Polynomial
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Elgoker locked the ship's treasure inside a secure safe. He encoded the master key as the constant term, $$$P(0)$$$, of a secret polynomial $$$P(x)$$$ of degree at most $$$k-1$$$. All calculations are heavily secured and evaluated modulo $$$10^{9}+7$$$.

To ensure the safe could be opened later, Elgoker distributed $$$k+2$$$ access codes to Chopper. Each access code is a coordinate pair $$$(x_{i}, y_{i})$$$, where $$$y_{i} = P(x_{i}) \pmod{10^{9}+7}$$$.

However, a mischievous monkey managed to infiltrate the ship and tampered with exactly one of the $$$k+2$$$ access codes, altering its $$$y_{i}$$$ value to a fake integer to lock everyone out. The remaining $$$k+1$$$ codes are completely untouched and lie perfectly on the original polynomial curve.

Given the $$$k+2$$$ access codes, your task is to help Chopper identify the corrupted code, discard it, and recover the master key $$$P(0) \pmod{10^{9}+7}$$$ so they can access the treasure!

Input

The first line of the input contains a single integer $$$k$$$ ($$$2 \le k \le 2000$$$) indicating that Elgoker's secret polynomial $$$P(x)$$$ has a maximum degree of $$$k-1$$$.

The next $$$k+2$$$ lines each contain two integers $$$x_{i}$$$ and $$$y_{i}$$$ ($$$1 \le x_{i} \lt 10^{9}+7$$$, $$$0 \le y_{i} \lt 10^{9}+7$$$) — the $$$x$$$ and $$$y$$$ values of the $$$i$$$-th access code.

It is guaranteed that:

  • All $$$x_{i}$$$ are pairwise distinct.
  • Exactly one $$$y_{i}$$$ has been altered from its true value $$$P(x_{i}) \pmod{10^{9}+7}$$$.
  • The original polynomial $$$P(x)$$$ has degree $$$ \le k-1$$$.
Output

Print a single integer: the original master key $$$P(0) \pmod{10^{9}+7}$$$.

Example
Input
2
1 15
2 20
3 99
4 30
Output
10
Note

In the example, the threshold is $$$k=2$$$, meaning Elgoker's polynomial has a degree of at most 1 (it is a linear equation).

The true polynomial is $$$P(x)=5x+10 \pmod{10^{9}+7}$$$.

The actual valid access codes should be: $$$P(1)=15$$$, $$$P(2)=20$$$, $$$P(3)=25$$$, $$$P(4)=30$$$.

The third access code (3, 99) was tampered with by the monkey. After discarding it, the remaining valid codes (1, 15), (2, 20), and (4, 30) perfectly form the line $$$P(x)=5x+10$$$. The master key $$$P(0)$$$ is 10.

M. Database Pool
time limit per test
2.5 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Sherbiny and Samer Samora are optimizing the core database engine for the Sunny's navigation logs. They need to implement a buffer pool of capacity $$$C$$$ to keep frequently accessed data pages in fast memory.

The pool can hold up to $$$C$$$ distinct pages, each identified by a positive integer. When a page is read or written, it is brought into the pool and becomes the most recently accessed page. If the pool is already at full capacity $$$C$$$ and a new page must be brought in, the Least Recently Used (LRU) page is evicted to make room.

A write operation modifies a page, marking it as dirty. If a dirty page is evicted from the pool, it is completely removed and is no longer considered dirty.

You must process $$$Q$$$ queries of the following four types:

  • 1 $$$x$$$ — Read: Access page $$$x$$$. Bring it into the pool (evicting the LRU page if necessary). Page $$$x$$$ becomes the most recently accessed. If $$$x$$$ was already in the pool, its dirty status remains unchanged.
  • 2 $$$x$$$ — Write: Access page $$$x$$$ using the exact same LRU rules as a read, but additionally mark page $$$x$$$ as dirty.
  • 3Flush: Print the total number of dirty pages currently in the pool, then mark all of them as clean (not dirty).
  • 4 $$$x$$$ — Status: Print whether page $$$x$$$ is currently in the pool, and whether it is dirty. This query does not count as an access and does not change the LRU order.
Input

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

The first line of each test case contains two integers $$$C$$$ and $$$Q$$$ ($$$1 \le C \le 10^{5}$$$, $$$1 \le Q \le 2 \times 10^{5}$$$) — the capacity of the buffer pool and the number of queries.

The next $$$Q$$$ lines describe the queries in the format given above. All page IDs satisfy $$$1 \le x \le 10^{9}$$$.

It is guaranteed that the sum of $$$Q$$$ over all test cases does not exceed $$$2 \times 10^{5}$$$.

Output

For each test case, output the answers for the Flush and Status queries in the order they appear:

  • For each $$$3$$$ (Flush) query, output a single integer on a new line: the number of dirty pages.
  • For each $$$4\ x$$$ (Status) query, output two space-separated integers on a new line: in_pool and is_dirty. Print 1 if the condition is true, and 0 if it is false.
Example
Input
1
2 8
1 10
2 20
4 10
4 20
2 30
4 10
3
4 20
Output
1 0
1 1
0 0
2
1 0
Note

In the first test case, the buffer pool has capacity $$$C = 2$$$.

After reading page $$$10$$$, the pool contains page $$$10$$$ (clean). After writing page $$$20$$$, the pool contains pages $$$10$$$ and $$$20$$$, with page $$$20$$$ marked as dirty.

The first status query for page $$$10$$$ reports 1 0 (in pool, clean). The second status query for page $$$20$$$ reports 1 1 (in pool, dirty).

Writing page $$$30$$$ brings in a new page while the pool is full. Page $$$10$$$ is the least recently used, so it is evicted. The pool now contains pages $$$20$$$ and $$$30$$$, both dirty.

The status query for page $$$10$$$ now reports 0 0 because it was evicted.

The flush query finds $$$2$$$ dirty pages ($$$20$$$ and $$$30$$$), outputs 2, and marks them both as clean.

The final status query for page $$$20$$$ reports 1 0 because it is still in the pool, but was cleaned by the flush.

N. Tree Signals
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Usopp and Robin are analyzing a massive island shaped like a rooted tree with $$$n$$$ vertices. The root is vertex 1. Each branch (edge) connecting the vertices has an integer value representing the wind speed, and each vertex $$$v$$$ has a wind tolerance $$$k_{v}$$$.

Usopp can shoot a signal flare from a vertex $$$u$$$ to a descendant $$$v$$$ if the difference between the maximum and minimum wind speeds (edge values) on the path from $$$u$$$ to $$$v$$$ is at most the receiver's tolerance $$$k_{v}$$$:

max(w on the path from u to v) - min(w on the path from u to v) $$$\le k_{v}$$$.

Robin notes that the tolerance used here belongs to the receiver $$$v$$$, not to the sender $$$u$$$.

If $$$u=v$$$, the path contains zero edges. In this case, the difference is defined as 0, so every vertex can successfully receive a signal from itself.

For every vertex $$$u$$$, find how many descendants ($$$u$$$ included) can receive a signal from $$$u$$$.

Note: a descendant of $$$u$$$ is any node $$$v$$$ in the subtree of $$$u$$$, including node $$$u$$$

Input

The first line 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$$$ ($$$1 \le n \le 2 \times 10^{5}$$$) — the number of vertices in the rooted tree. The root is vertex 1.

The second line of each test case contains $$$n$$$ integers $$$k_{1}, k_{2}, \dots, k_{n}$$$ ($$$0 \le k_{i} \le 10^{9}$$$) — the tolerance of each vertex.

For each $$$i$$$ from 2 to $$$n$$$, the next line contains two integers $$$p_{i}$$$ and $$$w_{i}$$$ ($$$1 \le p_{i} \lt i$$$, $$$0 \le w_{i} \le 10^{9}$$$) — the parent of vertex $$$i$$$ and the wind speed value on the edge between $$$p_{i}$$$ and $$$i$$$.

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

Output

For each test case, output $$$n$$$ integers on a single line. The $$$u$$$-th integer must be the number of descendants that can receive a signal from vertex $$$u$$$.

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

Vertex 1 reaches itself, vertex 2, and vertex 3. It does not reach vertex 4, because the values on the path to vertex 4 are 5 and 6, and their difference is greater than $$$k_{4}=0$$$.

O. Hidden Permutation
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

This is an interactive problem. Refer to the Interaction Protocol section below for better understanding.

Sherbiny has hidden a secret permutation $$$p$$$ of length $$$n$$$ (an array containing every integer from 1 to $$$n$$$ exactly once). Luffy's task is to reconstruct this permutation using at most $$$n-1$$$ queries.

Luffy cannot see the elements of $$$p$$$, but he can ask Sherbiny to evaluate specific subsets of indices. When Luffy queries a subset, Sherbiny isolates the elements at those indices—keeping them in their original relative order—and tells Luffy exactly how many inversions exist just among those isolated elements.

Formal Query Definition: Specifically, if Luffy queries a subset of $$$k$$$ indices $$$1 \le i_{1} \lt i_{2} \lt \dots \lt i_{k} \le n$$$, Sherbiny constructs a new sequence $$$a = [p_{i_{1}}, p_{i_{2}}, \dots, p_{i_{k}}]$$$. Sherbiny then returns the total number of inversions in $$$a$$$.

The number of inversions in an array is the number of pairs of indices $$$u, v$$$ such that $$$u \lt v$$$ and $$$a_{u} \gt a_{v}$$$.

Input

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

For each test case, Sherbiny first outputs a single integer $$$n$$$ ($$$2 \le n \le 1000$$$) — the length of the hidden permutation.

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

Output

For each test case, after at most $$$n-1$$$ questions, output one line:

  • ! $$$p_1$$$ $$$p_2$$$ $$$\ldots$$$ $$$p_n$$$
where $$$p_1, p_2, \ldots, p_n$$$ must be a permutation of the integers from $$$1$$$ to $$$n$$$ and must match the hidden permutation.
Interaction

For each test case, after reading $$$n$$$, you may ask at most $$$n-1$$$ questions before you must output your guess.

To ask a question, output a line in the following format:

  • ? $$$k$$$ $$$i_1$$$ $$$i_2$$$ $$$\ldots$$$ $$$i_k$$$
where $$$k$$$ ($$$1 \leq k \leq n$$$) is the number of chosen positions, and $$$i_1, i_2, \ldots, i_k$$$ are distinct integers from $$$1$$$ to $$$n$$$.

After each question, read one integer from the judge — the number of inversions among the values at the chosen positions.

After printing each line, flush the output. For example:

  • cout « endl or fflush(stdout) in C++;
  • System.out.flush() in Java;
  • sys.stdout.flush() in Python.

When you have determined the hidden permutation, output a single line:

  • ! $$$p_1$$$ $$$p_2$$$ $$$\ldots$$$ $$$p_n$$$
where $$$p_1, p_2, \ldots, p_n$$$ is your guess for the permutation. After outputting !, do not ask any more questions for the current test case; proceed to the next test case if one remains.
Example
Input
2
3

1

0

2

1
Output
? 2 1 3

? 2 2 3

! 3 1 2

? 2 1 2

! 2 1
Note

In the example, the judge first sends $$$t = 2$$$. The hidden permutations are $$$[3, 1, 2]$$$ in the first test case and $$$[2, 1]$$$ in the second test case.

ParticipantJudge
$$$2$$$
$$$3$$$
? 2 1 3
$$$1$$$
? 2 2 3
$$$0$$$
! 3 1 2
$$$2$$$
? 2 1 2
$$$1$$$
! 2 1

In the first test case, $$$n = 3$$$ and the hidden permutation is $$$[3, 1, 2]$$$. The question ? 2 1 3 asks about positions $$$1$$$ and $$$3$$$, whose values are $$$3$$$ and $$$2$$$; there is one inversion because $$$3 \gt 2$$$, so the judge replies with $$$1$$$. The question ? 2 2 3 asks about positions $$$2$$$ and $$$3$$$, whose values are $$$1$$$ and $$$2$$$; there are no inversions, so the judge replies with $$$0$$$. The participant then outputs ! 3 1 2.

In the second test case, $$$n = 2$$$ and the hidden permutation is $$$[2, 1]$$$. The question ? 2 1 2 selects both positions; the values $$$2$$$ and $$$1$$$ form one inversion, so the judge replies with $$$1$$$. The participant outputs ! 2 1.

Note that the participant uses only $$$2 = n - 1$$$ questions in the first test case and $$$1 = n - 1$$$ question in the second test case.

Empty lines in the example input and output are shown only for readability. Your solution must not output blank lines.