ICPC 2024 Thailand - Chulalongkorn University Internal Round
A. Card Dealer Game
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Two players are competing in a two-player game involving $$$n$$$ decks of cards. The $$$i$$$-th deck contains $$$p_i$$$ red cards and $$$q_i$$$ blue cards. The cards in each deck are indistinguishable from one another when placed face down.

The game consists of $$$n$$$ rounds, with each round played as follows:

  1. At the beginning of each round, one player is assigned the role of the dealer and the other the role of the guesser.
  2. The dealer chooses one of the remaining decks and shuffles the cards, placing them face down on the table.
  3. The guesser selects one card from the deck and flips it over:
    • If the card is red, the roles remain the same for the next round.
    • If the card is blue, the roles are switched for the next round, with the dealer becoming the guesser and the guesser becoming the dealer.
  4. Once a deck is used in a round, it is discarded and cannot be chosen again.
The game continues for $$$n$$$ rounds, with all decks being used exactly once. The player who is the dealer at the end of the $$$n$$$-th round is declared the winner.

Given that both players are playing optimally and know the exact composition of each deck (i.e., the number of red and blue cards in each deck), determine the probability that the player who starts as the dealer will win the game.

Let $$$P = 1\,000\,000\,007$$$. It can be shown that the answer can be expressed as an irreducible fraction $$$\frac{x}{y}$$$ where $$$x$$$ and $$$y$$$ are integers and $$$y \not\equiv 0 \pmod P$$$. Output the integer equal to $$$x\cdot y^{-1} \pmod P$$$. In other words, output such an integer $$$a$$$ that $$$0 \le a \lt P$$$ and $$$a\cdot y=x \pmod P$$$.

Input

The first line of the input contains an integer $$$n$$$ ($$$1 \le n \le 10^6$$$), the number of decks. The $$$i$$$-th of the following $$$n$$$ lines contains two integers $$$p_i$$$ and $$$q_i$$$ ($$$0 \le p_i, q_i \le 10^6$$$, $$$p_i + q_i \gt 0$$$): the number of red cards and the number of blue cards in the $$$i$$$-th deck, respectively.

Output

Print the answer modulo $$$1\,000\,000\,007$$$ in a single line.

Examples
Input
1
1 2
Output
333333336
Input
4
2 3
3 5
4 7
1 4
Output
96818183
Note

In the first example, the dealer has $$$\frac{1}{3}$$$ chance of winning if the guesser flips up a red card.

B. Emma and the Pixie dust
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

In the fairy village with a river passing through the middle of the city, there is a fairy named Emma who wishes to fly higher into the sky. In order for that, she decides to borrow the power of pixie dusts, however; pixie dusts aren't for sale. So she has to make it herself by mixing the elements that she gets from the shop. In the shop, there are $$$M$$$ elements, and each of them has the cuteness level of $$$A[i]$$$. Each element has a $$$\textbf{distinct}$$$ cuteness level from others. However, Emma is allowed to purchase only $$$N$$$ elements (any elements).

Making pixie dust isn't an easy task. To make it, two elements have to be combined. Assuming those have cuteness levels of $$$X$$$ and $$$Y$$$, the result will be $$$\textbf{one}$$$ pixie dust and an element of cuteness level of $$$GCD(X, Y)$$$. (Note that this result element can be used to create another pixie dust again and again and those 2 initial elements will be $$$\textbf{gone}$$$)

Emma's goal is to get $$$K$$$ pixie dust to fly through the mist and cloud, together with maximizing the sum of the cuteness levels of every element left. Emma will be very pleased if you help her choose $$$N$$$ elements from the shop and tell her the maximum sum of the cuteness levels of every element left.

Input

The first line contains three integers $$$M, N, K (1 \leq K \lt N \leq M \leq 5,000,000)$$$ — the number of elements sold in the shop, the number of elements that Emma can buy, the number of Pixie dust that Emma wants

The next line contains $$$M$$$ integers $$$A[1],A[2],A[3],...,A[M]$$$ $$$(1 \leq A[i] \leq 5,000,000)$$$ and $$$\textbf{all of them are distinct}$$$. — the cuteness level of each element sold in the shop

Output

Output a single value — the largest sum of cuteness of all elements left after the procedure of making Pixie dust

Examples
Input
7 4 2
11 1 3 2 7 8 12
Output
13
Input
8 6 2
14 11 2 4 9 12 8 13
Output
42
Note

In the first test case, Emma can choose elements from the shop with a cuteness level of $$$11,2,8,12$$$. First, combining elements with cuteness $$$2$$$ and $$$8$$$, Emma gets the first Pixie dust and element of cuteness $$$GCD(2,8)=2$$$. So, the elements left have a cuteness level of $$$11,12,2$$$. Then, combining elements with cuteness $$$2$$$ and $$$11$$$, Emma gets the second Pixie dust and element of cuteness $$$GCD(2,11)=1$$$. So, the elements left have a cuteness level of $$$12,1$$$. Thus, in this case, the sum is $$$12+1=13$$$, which is the largest sum Emma can achieve.

In the second test case, Emma can choose elements from the shop with a cuteness level of $$$14,11,4,12,8,13$$$. First, combining elements with cuteness $$$12$$$ and $$$8$$$, Emma gets the first Pixie dust and element of cuteness $$$GCD(12,8)=4$$$. So, the elements left have a cuteness level of $$$14,11,4,13,4$$$. Then, combining elements with cuteness $$$4$$$ and $$$4$$$, Emma gets the second Pixie dust and element of cuteness $$$GCD(4,4)=4$$$. So, the elements left have a cuteness level of $$$14,11,13,4$$$. Thus, in this case, the sum is $$$14+11+13+4=42$$$, which is the largest sum Emma can achieve.

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

There are $$$N$$$ pairs of chopsticks. The length of each chopstick of the $$$i^{th}$$$ pair is $$$l_i$$$ unit. Each chopstick is so thin that you can consider it as a line segment. Find the maximum area of the rectangle you can form by selecting two pairs of chopsticks.

Input

The first line contains an integer $$$N(2 \leq N \leq 100)$$$ — the number of pairs of chopsticks.

The next line contains $$$N$$$ integers $$$l_1,l_2,\dots,l_N\ (1 \leq l_1,l_2,\dots,l_N \leq 10^9) $$$ — the length of each pair of chopsticks.

Output

Output one integer $$$A$$$ — the maximum area possible in square units.

Examples
Input
3
1 2 3
Output
6
Input
4
1 2 3 3
Output
9
Note

In the first example, you pick the second and the third pair of chopsticks to form a rectangle of size $$$2 \cdot 3 = 6 $$$.

In the second example, you pick the third and fourth pair of chopsticks to form a rectangle of size $$$3 \cdot 3 = 9 $$$.

D. Animal Circus
time limit per test
2 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

You are the manager of the animal circus show. You have $$$N$$$ types of animals in your storage; you have $$$a_i$$$ animals of $$$i^{th}$$$ type. You have to group the animals into a cage such that each cage has exactly $$$k$$$ animals, and for some reason, having the same type of animal in one cage is kind of boring for the visitors, so you should never put the same type of animal in the same cage. The problem is that the animal circus show lasts for $$$Q$$$ days, and on the $$$i^{th}$$$ day, the animal of type $$$x_i$$$ got changed by $$$y_i$$$ (if $$$y_i$$$ is positive, the number of animals of type $$$x_i$$$ increases by $$$y_i$$$, otherwise, the number of animals of type $$$x_i$$$ decreases by $$$-y_i$$$)

Your job is to find the maximum number of cages you can bring to the show each day.

In this problem, the last answer will affect the next question by equation $$$ x_{new} = (answer \oplus x_{i}) \%\ N + 1 $$$ where $$$x_{new}$$$ is the $$$x$$$ of the next question, $$$answer$$$ is the latest output, and $$$x_i$$$ is the new input.

where $$$\oplus$$$ representing Bitwise XOR

and $$$\%$$$ representing Modulo

Input

The first line contains an integer $$$N$$$ and $$$k\ (1 \leq k \leq N \leq 10^5)$$$ — the number of types of animals and the number of animals required in one cage.

The second line contains $$$N$$$ integers $$$a_1, a_2, …, a_N\ (0 \leq a_1, a_2, …, a_N \leq 10^9)$$$ — the number of animals for each type.

The third line contains an integer $$$Q\ (1 \leq Q \leq 10^5)$$$ — the number of days in a circus show.

The $$$(3 + i)^{th}$$$ line $$$(1 \leq i \leq Q)$$$ contains an integer $$$x_i\ (1 \leq x_i \leq N)$$$ and $$$y_i\ (-10^9 \leq y_i \leq 10^9)$$$ — the animal type and number of animals that got changed in $$$i^{th}$$$ day.

The $$$x_i\ (1 \leq i \leq Q)$$$ has to pass the equation: $$$ x_{new} = (answer \oplus x_{i}) \%\ N + 1 $$$ where $$$x_{new}$$$ is the $$$x_i$$$ of the next question, $$$answer$$$ is the latest output. Initially, $$$answer$$$ is set as zero.

You can assume that you will never have a negative number of animals (i.e., the number of animals of each type will never go negative).

Output

The output should have $$$Q$$$ line. The $$$i^{th}$$$ line contains the maximum number of cages you can bring to the show on the $$$i^{th}$$$ day.

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

Bitwise XOR

A bitwise XOR is a binary operation that takes two bit patterns of equal length and performs the logical exclusive OR operation on each pair of corresponding bits. The result in each position is $$$1$$$ if only one of the bits is $$$1$$$, but will be $$$0$$$ if both are $$$0$$$ or both are $$$1$$$. In this we perform the comparison of two bits, being $$$1$$$ if the two bits are different, and $$$0$$$ if they are the same. For example:

         $$$0101$$$ (decimal $$$5$$$)

XOR $$$0011$$$ (decimal $$$3$$$)

$$$=$$$      $$$0110$$$ (decimal $$$6$$$))

E. Hidden Project
time limit per test
4 seconds
memory limit per test
64 megabytes
input
standard input
output
standard output

There are $$$N$$$ days. Your normal income is $$$a$$$ baht per day. There are some hidden projects, of which you can conduct as many as you want.

To conduct a project, the rules are as follows:

$$$\scriptsize{\bullet}$$$ Choose integers $$$l$$$, $$$r$$$ such that $$$1 \leq l \leq r \leq N$$$ which represent the days the project starts and finishes, respectively.

$$$\scriptsize{\bullet}$$$ On the $$$i^{th}$$$ day, $$$l \leq i \leq r$$$, you will receive $$$i-l+1$$$ baht.

$$$\scriptsize{\bullet}$$$ You will not receive your normal income on the day between $$$l^{th}$$$ and $$$r^{th}$$$, inclusive.

$$$\scriptsize{\bullet}$$$ There must not be any days more than one project is conducted. For a little clarity, you cannot start a project on the same day you finish another project.

Please find the maximum amount of money that can be earned in $$$N$$$ days.

Input

Each test contains multiple test cases. The first line contains the number of test cases, $$$t$$$ ($$$1 \leq t \leq 10^6$$$). The description of the test cases follows.

The only line of each test case consists of two integers $$$N$$$, $$$a$$$ ($$$1 \leq N \leq 10^4$$$, $$$0 \leq a \leq 10^5$$$) — the number of days and the amount of your normal income, respectively.

Output

For each test case, print a single integer — the maximum amount of money that can be earned in $$$N$$$ days.

Example
Input
3
3 1
3 3
20 21
Output
6
9
420

F. Portal Maintenance
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

In a galaxy far, far away, humanity has successfully established civilization across multiple planets within a star system. This system, which contains $$$n$$$ planets, is connected by a network of $$$n-1$$$ portals. These portals enable swift and efficient travel between the planets, ensuring that every planet is accessible from any other. The technology that powers these portals is sophisticated and advanced, but like all technology, it requires maintenance and upgrades to keep functioning optimally.

Isaac, a skilled electrician known for his expertise in portal technology, is tasked with a crucial mission. The central administration unit has decided that it's time to upgrade the firmware of all the portals in the star system to improve their performance and security. To accomplish this, all the portals need to be disabled temporarily first, which is precisely his mission.

Before disabling a portal, Isaac must first test its functionality by traveling through it at least once. But to avoid the risk of forgetting to disable any portal, Isaac decides that once he travels through a portal, he will immediately disable that portal. And since a disabled portal cannot be used for traveling, it means that he can only use each portal once. This creates a problem: how can he disable all the portals and still return back? The more he thinks about it, the more he realizes that it's impossible to achieve this without some form of intervention.

Fortunately, Isaac has a powerful tool at his disposal: a jammer. This device allows him to travel through portals even after they've been disabled. However, the jammer comes with a significant cost. Each time Isaac activates the jammer, it costs $$$c$$$, and for every portal he travels through while the jammer is activated, it costs $$$w_i$$$ to travel through the $$$i$$$-th portal (even if the portal is not disabled). Importantly, while the jammer is active, Isaac cannot disable any portals, meaning he must deactivate the jammer before he can continue disabling portals. Moreover, Isaac can only activate the jammer a limited number of times — at most $$$\frac{n}{2}$$$ times.

You are tasked to help Isaac find the minimum possible cost to disable all the portals, starting and ending at planet $$$1$$$. Additionally, you need to identify the optimal planets where he should activate and deactivate the jammer to achieve this goal.

Input

The first line of input contains two integers $$$n$$$, $$$c$$$ ($$$1 \le n,c \le 10^6$$$), denoting the total number of planets in the system and the initiation cost for each activation.

Each of the next $$$n-1$$$ lines contains three integers, $$$u$$$, $$$v$$$, and $$$w$$$, denoting a portal between planet $$$u$$$ and planet $$$v$$$ with cost $$$w$$$ to use it during the jammer activation. ($$$1\le u \lt v\le n , 1 \le d \le 10^6$$$).

Output

On the first line, output $$$W$$$ and $$$k$$$, the minimum cost and how many times the jammer is used, respectively.

On each of the next $$$k$$$ lines, output two integers $$$u, v$$$ denoting for Isaac to activate the jammer on planet $$$u$$$ and deactivate on planet $$$v$$$ ($$$1\le u, v\le n$$$).

If there are more than one possible way to use the jammer, output any of them. Print -1 if it is impossible to do so.

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

In the second example, one of the possible routes is as follows:

  • From planet $$$1$$$, he uses portal $$$1$$$ to travel to planet $$$2$$$ and disables the portal.
  • From planet $$$2$$$, he uses portal $$$4$$$ to travel to planet $$$5$$$ and disables the portal.
  • At planet $$$5$$$, he activates the jammer, then travels to planet $$$2$$$ through portal $$$4$$$, then to planet $$$4$$$ through portal $$$3$$$ and deactivates the jammer at planet $$$4$$$, costing $$$2+3+1=6$$$.
  • From planet $$$4$$$, he uses portal $$$3$$$ to travel to planet $$$2$$$ and disables the portal.
  • From planet $$$2$$$, he uses portal $$$2$$$ to travel to planet $$$3$$$ and disables the portal.
  • At planet $$$3$$$, he activates the jammer, then travels to planet $$$2$$$ through portal $$$2$$$, then to planet $$$1$$$ through portal $$$1$$$ and deactivates the jammer at planet $$$1$$$, costing $$$2+2+1=5$$$. The total cost is $$$6+5=11$$$.

G. Ki Chang Jab Takkataen
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

One evening, Jack decides to ride his elephant in a straight line to catch grasshoppers. His elephant is $$$H$$$ units tall.

There are $$$N$$$ grasshoppers in his path. He will encounter the $$$i^{th}$$$ grasshopper when his elephant runs for $$$x_i$$$ units, and its altitude will be $$$y_i$$$ units. He also has $$$M$$$ miracle nets for catching insects. The $$$j^{th}$$$ net is $$$l_j$$$ units long.

He will be able to catch the $$$i^{th}$$$ grasshopper with the $$$j^{th}$$$ net if and only if his elephant runs for $$$\textbf{exactly}$$$ $$$x_i$$$ units and $$$ H - l_j \leq y_i \leq H + l_j$$$. Each net will $$$\textbf{disappear}$$$ when he $$$\textbf{catch a grasshopper with it}$$$ (exchanging for transporting the grasshopper to his kitchen).

Jack has $$$Q$$$ questions for you. For the $$$i^{th}$$$ question, Jack wants to know if he wants to catch $$$q_i$$$ grasshoppers, what is the minimum distance he will have to travel.

Input

The first line contains four integers $$$N$$$, $$$M$$$, $$$H$$$, and $$$Q$$$($$$1 \leq N,\ M,\ Q \leq 2\cdot10^5,\ 1 \leq H \leq 10^9$$$) — the number of grasshoppers, the number of nets, the height of Jack's elephant, and the number of questions, respectively.

The $$$i^{th}$$$ line of the following $$$N$$$ lines contains two integers $$$x_i$$$ and $$$y_i$$$ ($$$1 \leq x_i,\ y_i \leq 10^9, x_i \lt x_{i +1}$$$ for all $$$i \lt N$$$) — the distance the elephant has to run to encounter the $$$i^{th}$$$ grasshopper, and the altitude of the $$$i^{th}$$$ grasshopper.

The next line contains M integers $$$l_1,l_2,\dots,l_M\ (1 \leq l_i \leq 10^9)$$$ — the length of nets.

The $$$i^{th}$$$ line of the following $$$Q$$$ lines contains an integer $$$q_i$$$ ($$$1 \leq q_i \leq N$$$) — the number of grasshoppers asked in the $$$i^{th}$$$ question.

Output

For each question, output $$$-1$$$ if the number of grasshoppers exceeds the maximum he can catch. Otherwise, output one integer — the minimum distance his elephant has to run.

Examples
Input
1 1 1 1
2 5
10
1
Output
2
Input
3 3 5 3
2 6
5 15
10 10
1 3 5
3
2
1
Output
-1
10
2

H. Final Quiz
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Professor H is planning out a quiz for his subject. He wants to create $$$n$$$ multiple-choice questions, each with $$$k$$$ possible options.

At first, the professor desires to make a quiz such that all the problems have the same answer. However, the university has stated a regulation that all quizzes cannot contain any instance of four consecutive problems with the same answer. Formally, let $$$a_i$$$ be the answer of the $$$i$$$-th question in the quiz. For all $$$i$$$ from $$$1$$$ to $$$n-3$$$, if $$$a_i = a_{i+1} = a_{i+2}$$$, then $$$a_{i+3} \neq a_i$$$.

Still, the professor wants to follow his original idea. So instead, he aims to maximize the instances where three consecutive problems have the same answer.

You have been tasked to find out how many quizzes the professor can come up with. Two quizzes are different if there exists a problem that has different answer.

Input

The input consists of a single line containing integers $$$n$$$ and $$$k$$$ ($$$1 \le n, k \le 10^6$$$).

Output

Output the answer as an integer modulo $$$10^9 + 7$$$.

Examples
Input
5 2
Output
10
Input
2 4
Output
16
Note

In the first example, ten possible quizzes are $$$(1, 1, 1, 2, 1)$$$, $$$(1, 1, 1, 2, 2)$$$, $$$(1, 1, 2, 2, 2)$$$, $$$(1, 2, 1, 1, 1)$$$, $$$(1, 2, 2, 2, 1)$$$, $$$(2, 1, 1, 1, 2)$$$, $$$(2, 1, 2, 2, 2)$$$, $$$(2, 2, 1, 1, 1)$$$, $$$(2, 2, 2, 1, 1)$$$, and $$$(2, 2, 2, 1, 2)$$$.

I. Lulu And The Magical Array
time limit per test
4 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

This is an interactive problem.

"You'll see more with your eyes closed" — Lulu

On Lulu's birthday, Pix comes up with a great present, a magical integer array $$$A$$$ of length $$$n$$$. The number of this array cannot be seen by eyes, but Lulu can cast a magical query asking for the bitwise XOR of 2 distinct elements of the array. More formally, Lulu can ask for the value of $$$A[i] \oplus A[j] (i\neq j)$$$ where $$$\oplus$$$ denotes the bitwise XOR operator. (You can find more details about the bitwise XOR operator in the notes.)

With her eyes closed, Lulu is certain that she can tell the minimum of the bitwise XOR between any 2 distinct elements of the array $$$A$$$ by casting no more than $$$n$$$ magical queries.

More formally, Lulu needs to find $$$min_{1 \leq i,j \leq n, i\ne j} A[i] \oplus A[j]$$$

Please help Lulu find this number!

Input

Each test contains multiple test cases. The first line contains a single integer $$$t(1\leq t \leq 10000)$$$ — the number of test cases. The description of test cases follows.

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

It's guaranteed that $$$0 \leq A[i] \lt 2^{30}$$$ $$$(1 \le i \le n)$$$ and the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.

Interaction

You can ask no more than $$$n$$$ magical queries by outputting a single line of the form

$$$\texttt{?}~i~j$$$ $$$(1 \leq i,j \leq n, i \neq j)$$$.

The interactor will respond with a single integer, the value of $$$A[i] \oplus A[j]$$$ or -1 if you ask more than $$$n$$$ queries or make an invalid query.

Output

When you are ready to report the answer, output a single line of the form

$$$\texttt{!}~x$$$

where $$$x=$$$ $$$min_{1 \leq i,j \leq n, i\ne j} A[i] \oplus A[j]$$$.

After printing a query or an answer do not forget to output the end of line and flush the output. Otherwise, you may get an Idleness limit exceeded verdict. To do this, use:

  • fflush(stdout) or cout.flush() in C/C++;
  • System.out.flush() in Java and Kotlin;
  • sys.stdout.flush() in Python;
Examples
Input
1
5
 
13
 
7
 
3
 
11
 
9
Output
 
 
? 1 2
 
? 2 4
 
? 1 3
 
? 5 2
 
? 3 4
 
! 3
Note

Explanation of an example

The array in the example is $$$[12,1,15,6,10]$$$.

For the first two inputs, the number of test cases(1) and $$$n$$$ for the first test case are given.

Then, Lulu makes queries for $$$5$$$ times in this order.

  1. $$$?$$$ $$$1$$$ $$$2$$$ and the interactor responds with $$$12 \oplus 1 = 13$$$.
  2. $$$?$$$ $$$2$$$ $$$4$$$ and the interactor responds with $$$1 \oplus 6 = 7$$$.
  3. $$$?$$$ $$$1$$$ $$$3$$$ and the interactor responds with $$$12 \oplus 15 = 3$$$.
  4. $$$?$$$ $$$5$$$ $$$2$$$ and the interactor responds with $$$10 \oplus 1 = 11$$$.
  5. $$$?$$$ $$$3$$$ $$$4$$$ and the interactor responds with $$$15 \oplus 6 = 9$$$.

After Lulu is certain that the answer is $$$3$$$, she reports the answer by outputting $$$!$$$ $$$3$$$.

Bitwise XOR

A bitwise XOR is a binary operation that takes two bit patterns of equal length and performs the logical exclusive OR operation on each pair of corresponding bits. The result in each position is $$$1$$$ if only one of the bits is $$$1$$$, but will be $$$0$$$ if both are $$$0$$$ or both are $$$1$$$. In this, we perform the comparison of two bits, being $$$1$$$ if the two bits are different, and $$$0$$$ if they are the same. For example:

         $$$0101$$$ (decimal $$$5$$$)

XOR $$$0011$$$ (decimal $$$3$$$)

$$$=$$$      $$$0110$$$ (decimal $$$6$$$)

J. Rook Placement
time limit per test
5 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

You are given a chessboard of size $$$r \cdot c$$$ and a sequence of $$$q$$$ queries.

Each query consists of two integers $$$x$$$ and $$$y$$$, representing the coordinates of a cell on the chessboard. For each query:

  • If the cell ((x, y)) is empty, place a pawn in that cell.
  • If the cell ((x, y)) already contains a pawn, remove the pawn from that cell.

After executing each query, report the number of cells on the chessboard where a rook can be placed such that it can attack exactly one pawn. A rook can attack a pawn if they are in the same row or column and there is no other pawn between them. The rook cannot be placed in a cell that already contains a pawn.

For example, in the image above, the rook placing at $$$(6,3)$$$ can attack the pawn at $$$(2,3)$$$ and $$$(6,5)$$$ but not the one at $$$(6,8)$$$ because there is a pawn between them.

Input

Each test contains multiple test cases. The first line contains a single integer $$$t(1\leq t \leq 10000)$$$ — the number of test cases. The description of test cases follows.

For each test case, the first line contains $$$3$$$ integers $$$r,c,$$$ and $$$q$$$ $$$(1 \leq r, c \leq 10^9, 1 \leq q \leq 3 \cdot 10^5)$$$.

Then $$$q$$$ lines follow, in the $$$i^{th}$$$ line two integers $$$x_i, y_i (1 \leq x_i \leq r, 1 \leq y_i \leq c)$$$ are given, the cell we will place the pawn or remove it if it's already existed in the chess board.

The sum of $$$q$$$ over all test cases does not exceed $$$3 \cdot 10^5$$$.

Output

After each query, print the number of cells in the board that a rook can be placed and can attack exactly one pawn in the board.

Examples
Input
1
6 1 7
1 1
2 1
4 1
4 1
5 1
4 1
1 1
Output
5
4
2
4
1
1
2
Input
3
7 3 4
5 2
5 3
4 3
3 3
2 5 4
2 4
2 5
1 1
2 1
1 4 4
1 1
1 3
1 4
1 4
Output
8
13
12
11
5
5
4
2
3
1
0
1
Note

In the second example, the board in the first test case can be visualized as follows:

From left to right, the image displays the state of the chessboard after each query. The dots on each board represent the cell that the rook can be placed to satisfy the condition.

K. A Potion Shopping On This Wonderful World!
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

In another world, aside from luck, there are $$$K$$$ types of stat for each adventurer.

In that world, there is a shop run by an archwizard lady and a masked man. The shop is likely going to go bankrupt in the near future because the archwizard always purchases useless stuff as an investment. However, she is certain that the investment this time will surely return profit, with buff potions!

The shop plans to sell $$$N$$$ kinds of potions. The $$$i^{th}$$$ kind potion buffs $$$n_i$$$ types of stat: $$$t_1,t_2,\dots,t_{n_i}$$$ and costs $$$c_i$$$ units. When the $$$i^{th}$$$ kind potion is consumed, if the consumer is lucky, each stat type of all $$$n_i$$$ types will be maximized, or else, if they are not lucky enough, the consumer will not get any buff and will have diarrhea instead. The effect lasts one day for both cases. Different kinds of potions can be consumed together. The shop stocks each kind of potion abundantly such that it is guaranteed that there is no kind of potion that will run out of stock.

A certain adventurer with extreme luck makes a plan to buy potions because he suffers too much headache from his party in each and every mission. "Last mission, my archpriest's healing almost killed me. My archwizard blows up everything — including the money from the mission. And my crusader...she was too busy enjoying the pain." — he facepalms while writing the plan.

With his luck, he knows that every potion he consumes will definitely be effective. There are currently $$$M$$$ days in his plan. On the $$$i^{th}$$$ day, he wants to maximize $$$m_i$$$ types of stat: $$$w_1,w_2,\dots,w_{m_i}$$$. He doesn't care if the potions buff other stat types that are not listed. He wants to know the minimum cost of potions he needs to buy to achieve that goal.

Input

The first line contains three integers $$$N$$$, $$$M$$$, and $$$K(1 \leq N,\ M \leq 2 \cdot 10^4,\ 1 \leq K \leq 14)$$$ — the number of kinds of potion, the number of planned days, and the number of types of stat, respectively.

The following $$$2N$$$ lines describe potion kind $$$1,2,\dots,N$$$ respectively. Each kind of potion is described by two lines.

The first line contains two integers $$$n$$$ and $$$c(1 \leq n \leq K, 1 \leq c \leq 10^5)$$$ — the number of stat types the potion buffs, and the cost of the potion respectively.

The second line contains $$$n$$$ integers $$$t_1,t_2,\dots,t_n(1 \leq t_1 \lt t_2 \lt \dots \lt t_n \leq K)$$$ — the types of stat buffed.

The following $$$2M$$$ lines describe plans in $$$M$$$ days. A plan in each day is described by two lines.

The first line contains one integer $$$m(1 \leq m \leq K)$$$ — the number of stat types required that day.

The second line contains $$$m$$$ integers $$$w_1,w_2,\dots,w_m(1 \leq w_1 \lt w_2 \lt \dots \lt w_m \leq K)$$$ — the types of stat required that day.

Output

Output $$$M$$$ lines – the cost for each day from day $$$1$$$ to $$$M$$$.

Each line, output $$$-1$$$ if that day the adventurer cannot maximize all the required types, else output the minimum cost he needs for that day.

Examples
Input
3 3 3
2 4
1 2
2 4
2 3
3 9
1 2 3
1
1
1
3
3
1 2 3
Output
4
4
8
Input
3 1 3
2 4
1 2
2 4
2 3
3 7
1 2 3
3
1 2 3
Output
7
Input
1 1 2
1 2
1
1
2
Output
-1
Note

In example $$$1$$$, for the first day, he can buy the first kind for stat type $$$1$$$ and $$$2$$$, which cover his requirement (type 1).

For the third plan, he can buy the first and second kind which, together, cover stat type $$$1$$$, $$$2$$$, and $$$3$$$ for $$$8$$$ units. It is cheaper than buying the third kind, which also covers the requirement.

In example $$$3$$$, there is no potion which buffs stat type $$$2$$$.