OCPC 2025 Winter, Day 2: Songyang Chen Contest 5
A. Two Fences
time limit per test
3 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

In geometry, a convex polygon is a polygon that is the boundary of a convex set. This means that the line segment between two points of the polygon is contained in the union of the interior and the boundary of the polygon. In particular, it is a simple polygon (not self-intersecting). Equivalently, a polygon is convex if every line that does not contain any edge intersects the polygon in at most two points.

You are given $$$n$$$ distinct points $$$p_1,p_2,\dots,p_n$$$ on the 2D plane. It is guaranteed that all points are distinct and no three points are collinear.

A fence can be regarded as a convex polygon such that:

  • The area is positive.
  • Each endpoint of any edge belongs to the given set of points.

The profit of the fence $$$F$$$ comes from the points on the boundary or inside it. Formally: $$$$$$\text{Profit}(F)=\sum_{p_i\text{ is on the boundary of }F}a_i+\sum_{p_i\text{ is strictly inside }F}b_i$$$$$$

You are required to build exactly two fences $$$A,B$$$ such that:

  • $$$\text{Profit}(A)\cdot\text{Profit}(B)$$$ is maximized.
  • Two fences can not be overlapped or nested. They must share no common vertices.
Input

There is only one test case in each test.

The test case starts with a line containing a single integer $$$n$$$ ($$$6 \leq n \leq 100$$$), denoting the number of points.

Each of the next $$$n$$$ lines contains four integers $$$x_i,y_i,a_i$$$ and $$$b_i$$$ ($$$1\leq x_i,y_i\leq 10^9$$$, $$$1\leq a_i,b_i\leq 10^6$$$), describing the $$$i$$$-th point $$$p_i$$$ located at $$$(x_i,y_i)$$$.

It is guaranteed that no two points are identical and no three points are collinear.

Output

Print a single line containing a single integer, denoting the maximum possible value of $$$\text{Profit}(A)\cdot\text{Profit}(B)$$$. You can assume that the answer always exists.

Example
Input
9
1 1 10 1
10 1 10 1
10 10 10 1
1 10 10 1
2 3 1 10
104 102 10 1
106 102 10 1
105 109 10 1
105 108 1 10
Output
2000

B. King's Ruins
time limit per test
3 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

The first king of Byteland was buried with his $$$n$$$ knights a thousand years ago. In the ruins, the archaeologists have found $$$n$$$ stone tablets in a row, labeled by $$$1,2,\dots,n$$$ from left to right. Each stone tablet belongs to a knight. On the surface of the $$$k$$$-th stone tablet, five numbers describe the ability of the $$$k$$$-th knight in five aspects:

  • The wind ability $$$w$$$, denoting how fast the knight is.
  • The guard ability $$$g$$$, denoting how many attacks can be defended by the knight.
  • The ice ability $$$i$$$, denoting the power of ice crystal cast by the knight.
  • The flame ability $$$f$$$, denoting the power of fire cast by the knight.
  • The light ability $$$l$$$, denoting the power of thunder cast by the knight.

Little Q is visiting the stone tablets from left to right, according to the labels from $$$1$$$ to $$$n$$$. After visiting a stone tablet, before moving right to the next one, he can choose to take a photo with it or do nothing. Little Q estimates the value of each stone tablet, he wants to maximize the total value that he takes photos with, and the knight corresponding to the next photo always dominates the current one. Here the $$$x$$$-th knight dominates the $$$y$$$-th knight if and only if $$$w_x\geq w_y$$$, $$$g_x\geq g_y$$$, $$$i_x\geq i_y$$$, $$$f_x\geq f_y$$$ and $$$l_x\geq l_y$$$.

There are so many stone tablets that Little Q can not determine which to take photos with. Please write a program to help him.

Input

There is only one test case in each test.

The test case starts with a line containing a single integer $$$n$$$ ($$$1 \leq n \leq 100\,000$$$), denoting the number of stone tablets.

In the next $$$n$$$ lines, the $$$k$$$-th line contains six integers $$$w_k$$$, $$$g_k$$$, $$$i_k$$$, $$$f_k$$$, $$$l_k$$$ and $$$v_k$$$ ($$$1\leq w_k,g_k,i_k,f_k,l_k\leq n$$$, $$$1\leq v_k\leq 10\,000$$$), describing the $$$k$$$-th stone tablet, $$$v_k$$$ denoting the value of the photo with it.

Output

Output $$$n$$$ lines, the $$$k$$$-th ($$$1\leq k\leq n$$$) of which containing an integer, denoting the maximum total value when the last photo is taken with the $$$k$$$-th stone tablet.

Example
Input
4
1 2 1 2 1 30
2 1 2 1 2 40
3 3 3 3 3 50
2 3 3 2 4 100
Output
30
40
90
140

C. Maximum Query
time limit per test
2 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

In this problem, your task is to maintain a multiset $$$T$$$ whose elements are pairs $$$(v_i,w_i)$$$. Initially, $$$T$$$ is empty. You are required to perform $$$n$$$ operations, each of which is one of the following three types:

  • "+ v w" ($$$1\leq v\leq 500\,000$$$, $$$1\leq w\leq n$$$): Insert a pair $$$(v,w)$$$ into the multiset $$$T$$$.
  • "- v w" ($$$1\leq v\leq 500\,000$$$, $$$1\leq w\leq n$$$): Remove a pair $$$(v,w)$$$ from $$$T$$$. It is guaranteed that such a pair always exists. Note that $$$T$$$ is a multiset, when multiple pairs match $$$(v,w)$$$, just remove one pair.
  • "? k" ($$$1\leq k\leq 500\,000$$$): It is a query. You are required to find a pair $$$(v,w)$$$ from $$$T$$$ such that $$$\gcd(v,k)=1$$$ and $$$w$$$ is maximized. You need to report the value of $$$w$$$. When there is no finding, please report "-1" instead.
Input

There is only one test case in each test.

The test case starts with a line containing a single integer $$$n$$$ ($$$1 \leq n \leq 200\,000$$$), denoting the number of operations.

Each of the next $$$n$$$ lines describes an operation in the formats described in the statement above.

Output

For each query, print a single line containing a single integer, denoting the value of $$$w$$$ that you find, or "-1" if there is no finding.

Example
Input
6
+ 4 5
+ 3 4
? 2
? 3
- 3 4
? 4
Output
4
5
-1

D. Divide the Sweets
time limit per test
9 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

To celebrate Children's Day, a mother of $$$k$$$ kids comes to the shop to buy sweets. There are $$$n$$$ boxes of sweets in the shop, the $$$i$$$-th box has $$$w_i$$$ sweets. The mother will choose which boxes to buy, and will divide all the bought boxes among the $$$k$$$ kids. Each kid will receive several (maybe zero) boxes, and will count the total number of sweets he receives. Assume the $$$i$$$-th kid receives $$$c_i$$$ sweets, the unfairness is defined as $$$\sum_{i=1}^k c_i^2$$$. To make all the kids happy, the mother will always divide boxes to minimize the unfairness. Note that she can not open any box to adjust the number of sweets inside it.

The mother is wondering which boxes to buy. She can not make her final decision. Please write a program to help the mother try all possible $$$2^n-1$$$ non-empty subsets of boxes to buy, and figure out the minimum unfairness for each subset. Note that you only need to report the sum of the minimum unfairness among all $$$2^n-1$$$ non-empty subsets.

Input

The first line contains a single integer $$$T$$$ ($$$1 \leq T \leq 50$$$), the number of test cases. For each test case:

The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \leq m\leq n \leq 20$$$, $$$n+m\leq 23$$$), denoting the number of boxes and the parameter $$$m$$$.

The second line contains $$$n$$$ integers $$$w_1,w_2,\dots,w_n$$$ ($$$1\leq w_i\leq 50\,000$$$), denoting the number of sweets in each box.

It is guaranteed that there are at most $$$2$$$ cases such that $$$n \gt 12$$$.

Output

For each test case, output $$$m$$$ lines, the $$$i$$$-th ($$$1\leq i\leq m$$$) of which containing an integer, denoting the answer when $$$k=i$$$. Note that the answer may be extremely large, so please print it modulo $$$998\,244\,353$$$ instead.

Example
Input
1
5 4
1 2 3 4 5
Output
2240
1180
930
884

E. Spanning Tree Game
time limit per test
6 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

Alice and Bob are playing a game on an undirected graph with $$$n$$$ vertices and $$$m$$$ edges. The vertices are labeled by $$$1,2,\dots,n$$$. The edges are labeled by $$$1,2,\dots,m$$$. The $$$i$$$-th edge connects the $$$u_i$$$-th vertex and the $$$v_i$$$-th vertex directly, and its weight will be chosen from the given two values $$$a_i$$$ and $$$b_i$$$.

First, Alice needs to assign weights to all the $$$m$$$ edges such that there are exactly $$$k$$$ edges whose weights are taken from $$$a$$$ while the weights of other $$$m-k$$$ edges are taken from $$$b$$$. Then, Bob needs to choose exactly $$$n-1$$$ edges from the graph such that every pair of different vertices are connected by these $$$n-1$$$ edges directly or indirectly.

The final score of the game is equal to the total weights of the $$$n-1$$$ edges chosen by Bob. Alice wants to maximize the score while Bob wants to minimize it. Please write a program to predict the final score for $$$k=0,1,2,\dots,m$$$ if both of the players play optimally.

Input

The first line contains a single integer $$$T$$$ ($$$1 \leq T \leq 20$$$), the number of test cases. For each test case:

The first line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq n \leq 9$$$, $$$n-1\leq m\leq 30$$$), denoting the number of vertices and the number of edges.

Each of the following $$$m$$$ lines contains four integers $$$u_i,v_i,a_i$$$ and $$$b_i$$$ ($$$1 \leq u_i,v_i \leq n$$$, $$$u_i\neq v_i$$$, $$$1\leq a_i,b_i\leq 1\,000\,000$$$), describing an edge.

It is guaranteed that the graph is connected. Note that there can be multiple edges between same pair of vertices.

Output

For each test case, output $$$m+1$$$ lines, the $$$i$$$-th ($$$1\leq i\leq m+1$$$) of which containing an integer, denoting the final score when $$$k=i-1$$$.

Example
Input
1
3 3
1 2 4 6
1 3 2 7
2 3 3 5
Output
11
9
7
5

F. Dusk Moon
time limit per test
6 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

You are given $$$n$$$ points $$$p_1,p_2,\dots,p_n$$$ on the 2D plane. You need to perform $$$q$$$ operations. Each operation is one of the following:

  • "1 $$$k$$$ $$$x$$$ $$$y$$$" ($$$1 \leq k \leq n$$$, $$$1\leq x,y\leq 10^8$$$): Change the coordinate of the point $$$p_k$$$ into $$$(x,y)$$$.
  • "2 $$$l$$$ $$$r$$$" ($$$1 \leq l\leq r\leq n$$$): Find the minimum non-negative integer $$$R$$$ such that you can cover all the points $$$p_l,p_{l+1},\dots,p_r$$$ using a single circle whose radius is $$$R$$$. Note that a point is considered to be covered if and only if it is inside the circle or on the border of the circle.
Input

The first line contains a single integer $$$T$$$ ($$$1 \leq T \leq 3$$$), the number of test cases. For each test case:

The first line of the input contains two integers $$$n$$$ and $$$q$$$ ($$$1 \leq n,q \leq 100\,000$$$), denoting the number of points and the number of operations.

In the next $$$n$$$ lines, the $$$i$$$-th line contains two integers $$$x_i$$$ and $$$y_i$$$ ($$$1\leq x_i,y_i\leq 10^8$$$), describing the coordinate of $$$p_i$$$.

Each of the next $$$q$$$ lines describes an operation in formats described in the statement above.

It is guaranteed that all the values of $$$x_i$$$, $$$y_i$$$, $$$x$$$, $$$y$$$ are chosen uniformly at random from integers in their corresponding ranges. The randomness condition does not apply to the sample test case, but your solution must pass the sample as well.

Output

For each query, print a single line containing an integer, denoting the minimum radius.

Example
Input
1
5 5
1 1
2 2
3 1
3 3
2 5
2 1 5
2 1 1
2 1 2
1 1 10 1
2 1 5
Output
3
0
1
5

G. Update the Sequence
time limit per test
6 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

You are given two sequences $$$a_0,a_1,\dots,a_{n-1}$$$ and $$$b_0,b_1,\dots,b_{n-1}$$$, you are required to perform $$$q$$$ operations. For each operation, you will be given an integer $$$k$$$ ($$$0\leq k \lt n$$$), you need to update $$$a_i$$$ into $$$\max(a_i,b_{(i+k)\bmod n})$$$ for each $$$i$$$ ($$$0\leq i \lt n$$$). To prove that you do perform these operations, output the value of $$$\sum_{i=0}^{n-1} a_i$$$ after each operation.

Input

The first line contains a single integer $$$T$$$ ($$$1 \leq T \leq 2$$$), the number of test cases. For each test case:

The first line contains two integers $$$n$$$ and $$$q$$$ ($$$1\leq n,q\leq 250\,000$$$), denoting the length of the sequence and the number of operations.

The second line contains $$$n$$$ positive integers $$$a_0,a_1,\dots,a_{n-1}$$$ ($$$1\leq a_i\leq 10^9$$$).

The third line contains $$$n$$$ positive integers $$$b_0,b_1,\dots,b_{n-1}$$$ ($$$1\leq b_i\leq 10^9$$$).

Each of the next $$$q$$$ lines contains a single integer $$$k$$$ ($$$0\leq k \lt n$$$), describing each operation.

It is guaranteed that all the values of $$$a_i$$$, $$$b_i$$$ are chosen uniformly at random from integers in $$$[1,10^9]$$$, and each $$$k$$$ is chosen uniformly at random from integers in $$$[0,n)$$$. The randomness condition does not apply to the sample test case, but your solution must pass the sample as well.

Output

For each test case, output $$$q$$$ lines, the $$$i$$$-th ($$$1\leq i\leq q$$$) of which containing an integer, denoting the value of $$$\sum_{i=0}^{n-1} a_i$$$ after the $$$i$$$-th operation is applied.

Example
Input
1
5 5
1 5 3 6 8
2 5 4 7 3
3
2
4
1
0
Output
29
31
33
35
36

H. Subset Query
time limit per test
3 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

Little Q and Little T are playing a guessing game. In each round, Little T comes up with $$$n$$$ positive integers $$$x_1,x_2,\dots,x_n$$$ and writes down them on the paper. He should ensure these integers are all in the range $$$[1,m]$$$, and he is not allowed to change them during the game. Then Little Q can ask as many questions as he likes. There are two types of questions Little Q can ask:

  • "What's the value of the minimum number in the subset $$$S$$$?"
  • "What's the value of the maximum number in the subset $$$S$$$?"

For each question, Little T will always answer it honestly in the form "It's in the range $$$[l,r]$$$."

Now Little Q has asked $$$q$$$ questions, he still can't figure out the value of $$$x_1,x_2,\dots,x_n$$$, can you please tell him the minimum possible value of $$$x_1+x_2+\dots+x_n$$$ and the maximum possible value of $$$x_1+x_2+\dots+x_n$$$?

Input

The first line contains a single integer $$$T$$$ ($$$1 \leq T \leq 5$$$), the number of test cases. For each test case:

The first line contains three integers $$$n$$$, $$$m$$$ and $$$q$$$ ($$$1 \leq n \leq 8$$$, $$$1\leq m\leq 1\,000\,000$$$, $$$1\leq q\leq 500$$$), denoting the number of numbers, the upper bound of numbers and the number of questions.

Each of the following $$$q$$$ lines starts with two integers $$$op_i$$$ and $$$k_i$$$ ($$$1\leq op_i\leq 2,1\leq k_i\leq n$$$), followed by $$$k_i$$$ distinct integers $$$p_{i,1},p_{i,2},\dots,p_{i,k_i}$$$ ($$$1\leq p_{i,j}\leq n$$$), ending with two integers $$$l_i$$$ and $$$r_i$$$ ($$$1\leq l_i\leq r_i\leq m$$$), describing each question and the answer:

  • $$$op_i=1$$$ means the value of the minimum number among the set $$$\{x_{p_{i,1}},x_{p_{i,2}},\dots,x_{p_{i,k_i}}\}$$$ is in the range $$$[l_i,r_i]$$$.
  • $$$op_i=2$$$ means the value of the maximum number among the set $$$\{x_{p_{i,1}},x_{p_{i,2}},\dots,x_{p_{i,k_i}}\}$$$ is in the range $$$[l_i,r_i]$$$.
Output

For each test case, output a single line containing two integers $$$\min\{sum\}$$$ and $$$\max\{sum\}$$$, denoting the minimum possible value of $$$x_1+x_2+\dots+x_n$$$ and the maximum possible value of $$$x_1+x_2+\dots+x_n$$$.

It is guaranteed that the answer always exists.

Example
Input
2
3 10 1
1 1 1 5 5
5 100 2
1 3 2 3 4 7 9
2 3 2 3 4 7 9
Output
7 25
23 227

I. Taxi
time limit per test
1 second
memory limit per test
512 megabytes
input
standard input
output
standard output

There are $$$n$$$ towns in Byteland, labeled by $$$1,2,\dots,n$$$. The $$$i$$$-th town's location is $$$(x_i,y_i)$$$. Little Q got a taxi VIP card, he can use the VIP card to cut down the taxi fare. Formally, assume Little Q is at $$$(x',y')$$$, if he calls a taxi to drive him to the $$$k$$$-th town, the VIP card will reduce $$$\min(|x'-x_k|+|y'-y_k|,w_k)$$$ dollars.

Little Q wants to make full use of his VIP card. He will give you $$$q$$$ queries, in each query you will be given his location, and you need to choose a town such that the VIP card will reduce the most taxi fare.

Input

The first line contains a single integer $$$T$$$ ($$$1 \leq T \leq 100$$$), the number of test cases. For each test case:

The first line contains two integers $$$n$$$ and $$$q$$$ ($$$1 \leq n,q \leq 100\,000$$$), denoting the number of towns and the number of queries.

Each of the following $$$n$$$ lines contains three integers $$$x_i$$$, $$$y_i$$$ and $$$w_i$$$ ($$$1 \leq x_i,y_i,w_i \leq 10^9$$$), describing a town.

Each of the following $$$q$$$ lines contains two integers $$$x'$$$ and $$$y'$$$ ($$$1 \leq x',y' \leq 10^9$$$), describing a query.

It is guaranteed that the sum of all $$$n$$$ is at most $$$500\,000$$$, and the sum of all $$$q$$$ is at most $$$500\,000$$$.

Output

For each query, print a single line containing an integer, denoting the maximum possible reduced taxi fare.

Example
Input
1
3 4
1 5 7
5 1 6
2 3 9
1 5
2 2
4 3
10 10
Output
6
4
5
9

J. Period of Matrix
time limit per test
3 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

You are given a matrix $$$\textbf{M}$$$ with $$$n$$$ rows and $$$n$$$ columns. Let's define $$$f(i,j,k)=[(\textbf{M}^k)_{i,j}\neq 0]$$$. For each pair of $$$(i,j)$$$ ($$$1\leq i,j\leq n$$$), find the smallest positive integer $$$t$$$ such that $$$f(i,j,k)=f(i,j,k+t)$$$ always holds when $$$k$$$ is sufficiently large.

Note that matrix multiplication should be calculated under $$$\mathbb{Z}$$$.

Input

The first line contains a single integer $$$T$$$ ($$$1 \leq T \leq 20$$$), the number of test cases. For each test case:

The first line contains a single integer $$$n$$$ ($$$1\leq n\leq 60$$$).

Each of the next $$$n$$$ lines contains a binary string of length $$$n$$$. The $$$j$$$-th bit of the $$$i$$$-th string denotes $$$\textbf{M}_{i,j}$$$ ($$$1\leq i,j\leq n$$$). In other words, all the values in $$$\textbf{M}$$$ are in $$$\{0,1\}$$$.

Output

For each test case, output $$$n$$$ lines. Output $$$n$$$ integers in the $$$i$$$-th line, the $$$j$$$-th integer of which denoting the smallest positive integer $$$t$$$ such that $$$f(i,j,k)=f(i,j,k+t)$$$ always holds when $$$k$$$ is sufficiently large. Note that if you can not find such $$$t$$$, output "-1" instead.

Example
Input
1
9
010010000
001000001
000100000
010000000
000001000
000000100
000000010
000010001
000000000
Output
1 3 3 3 4 4 4 4 12
1 3 3 3 1 1 1 1 3
1 3 3 3 1 1 1 1 3
1 3 3 3 1 1 1 1 3
1 1 1 1 4 4 4 4 4
1 1 1 1 4 4 4 4 4
1 1 1 1 4 4 4 4 4
1 1 1 1 4 4 4 4 4
1 1 1 1 1 1 1 1 1

K. Find a Cycle
time limit per test
15 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

You are given a directed graph with $$$n$$$ vertices and $$$m$$$ edges. There may be multiple edges between same pair of vertices. Self-loops are also allowed in this graph. The vertices are labeled by $$$1,2,\dots,n$$$. The length of the $$$i$$$-th edge $$$u_i\rightarrow v_i$$$ is $$$998\,244\,353^{w_i}$$$.

Your task is to find a cycle in the given graph such that the average length of all edges on the cycle is minimized, or determine there is no cycle.

Input

The first line contains a single integer $$$T$$$ ($$$1 \leq T \leq 100$$$), the number of test cases. For each test case:

The first line contains two positive integers $$$n$$$ and $$$m$$$ ($$$1\leq n\leq 1000$$$, $$$1\leq m\leq 2000$$$), denoting the number of vertices and edges.

Each of the following $$$m$$$ lines contains three integers $$$u_i,v_i$$$ and $$$w_i$$$ ($$$1\leq u_i,v_i\leq n$$$, $$$0\leq w_i \lt n$$$), describing an edge.

It is guaranteed that there are at most $$$10$$$ cases such that $$$n \gt 100$$$ or $$$m \gt 100$$$.

Output

For each test case, output a single line containing an integer, denoting the average length of all edges on the cycle that you find.

More precisely, if the answer is $$$\frac{p}{q}$$$, you should output the minimum non-negative integer $$$r$$$ such that $$$q \cdot r \equiv p \pmod{(10^9+7)}$$$. You may safely assume that such $$$r$$$ always exists in all test cases. But if there is no cycle, please print "-1" instead.

Example
Input
6
3 3
1 2 0
2 3 0
3 1 0
2 1
1 2 0
2 2
1 2 0
2 1 1
3 5
1 2 0
1 3 1
2 3 2
3 2 0
2 3 1
4 5
1 2 3
2 3 3
3 1 3
2 4 1
4 1 1
1 1
1 1 0
Output
1
-1
499122177
499122177
540815376
1

L. Treasure Hunting
time limit per test
7 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

You got a treasure map that marks $$$n$$$ pirate chests buried in the ground, labeled by $$$1,2,\dots,n$$$. The $$$i$$$-th chest is located at point $$$(i,p_i)$$$ on the plane. When you open the $$$i$$$-th pirate chest, you will get $$$v_i$$$ coins.

You will start at $$$(0,0)$$$, and every time you can choose to move from $$$(x,y)$$$ to $$$(x+1,y)$$$ or $$$(x,y+1)$$$. You can dig up a pirate chest and take all the coins it contains when you are right above it.

Unfortunately, there is a dangerous trap area that is not marked! Through much research, you know that this is a rectangular area with sides parallel to the coordinate axes, and that it can only be one of $$$m$$$ possible areas. Please write a program to calculate how many coins you can take by following the optimal route for each possible case.

Assume that the real trap area is the $$$i$$$-th rectangle $$$[x_1,x_2]\times [y_1,y_2]$$$, then a position $$$(x,y)$$$ is considered to be dangerous if and only if $$$x_1\leq x\leq x_2$$$ and $$$y_1\leq y\leq y_2$$$. You should never pass through any dangerous position.

Input

The first line contains a single integer $$$T$$$ ($$$1 \leq T \leq 100$$$), the number of test cases. For each test case:

The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1\leq n,m\leq 300\,000$$$), denoting the number of pirate chests and the number of possible traps.

Each of the following $$$n$$$ lines contains two integers $$$p_i$$$ and $$$v_i$$$ ($$$1\leq p_i\leq n$$$, $$$1\leq v_i\leq 10^9$$$), describing each pirate chest. It is guaranteed that the values of all $$$p_i$$$ are pairwise distinct.

Each of the following $$$m$$$ lines contains four integers $$$x_1,y_1,x_2$$$ and $$$y_2$$$ ($$$1\leq x_1\leq x_2\leq n$$$, $$$1\leq y_1\leq y_2\leq n$$$), describing each possible rectangular trap area.

It is guaranteed that the sum of all $$$n$$$ is at most $$$1\,500\,000$$$, and the sum of all $$$m$$$ is at most $$$1\,500\,000$$$.

Output

For each test case, output $$$m$$$ lines, the $$$i$$$-th ($$$1\leq i\leq m$$$) of which containing an integer, denoting the maximum number of coins that you can take when the dangerous trap area is the $$$i$$$-th rectangle.

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

M. Optimal K Intervals
time limit per test
8 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

You are given an integer sequence $$$a_1,a_2,\dots,a_n$$$, find exactly $$$k$$$ disjoint consecutive subsequences from it such that the length of each consecutive subsequence is prime. Assume the sum of all integers in the $$$i$$$-th consecutive subsequence is $$$s_i$$$, you need to maximize $$$\min\{s_1,s_2,\dots,s_k\}$$$.

Note that the selected consecutive subsequences don't need to cover the entire sequence. Here, "consecutive subsequence" means that it must be contiguous (i.e. a substring).

Input

The first line contains a single integer $$$T$$$ ($$$1 \leq T \leq 100$$$), the number of test cases. For each test case:

The first line contains two integers $$$n$$$ and $$$k$$$ ($$$2\leq n\leq 200\,000$$$, $$$1\leq k\leq n$$$).

The second line contains $$$n$$$ integers $$$a_1,a_2,\dots,a_n$$$ ($$$|a_i|\leq 1000$$$).

It is guaranteed that all the values of $$$a_i$$$ are chosen uniformly at random from integers in $$$[-1000,1000]$$$. The randomness condition does not apply to the sample test case, but your solution must pass the sample as well.

It is also guaranteed that the sum of all $$$n$$$ is at most $$$10^6$$$.

Output

For each test case, output a single line containing an integer, denoting the maximum possible value of $$$\min\{s_1,s_2,\dots,s_k\}$$$. If there is no solution, please output "impossible" instead.

Example
Input
4
6 1
1 1 4 5 1 4
6 1
-1 -1 -4 -5 -1 -4
6 3
-1 -1 -4 -5 -1 -4
2 2
0 0
Output
15
-2
-9
impossible