CAMA 2024
A. Note-takers
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Esomer doesn't want to go to class at university, but he needs notes from his lectures to be able to study. Therefore, he has come up with the following system.

The university can be represented as a tree of $$$n$$$ nodes, and on a single day, Esomer will have $$$k$$$ classes on consecutive hours. Additionally, Esomer will hire note-takers. A note-taker can be hired at node $$$i$$$ at any time for a cost of $$$c_i$$$. Additionally, all note-takers can move to any neighbor node of their current node between hours. Note that Esomer can hire several note-takers at the same node and at the same time, and at any point in time, several note-takers can be at the same node.

Esomer's demand is simple, for each of his $$$k$$$ classes at nodes $$$v_1, v_2, \ldots, v_k$$$, there needs to be at least one note-taker at node $$$v_i$$$ at hour $$$i$$$, so that Esomer will get the notes for the class.

Unfortunately, Esomer is not rich, so he wants to pay the least possible amount in total, and he has asked for your help in calculating that quantity.

Input

The first line contains a single integer $$$t$$$, the number of test cases $$$(1 \le t \le 50)$$$.

The first line of each test case consists of two integers $$$n$$$ and $$$k$$$, the number of nodes of the tree and the number of classes Esomer has to attend, respectively $$$(1 \le n \le 10^5, 1 \le k \le 200)$$$.

The second line contains $$$n$$$ integers $$$c_1, c_2, \ldots, c_n$$$, where $$$c_i$$$ denotes the cost of hiring a note-taker at node $$$i$$$ $$$(1 \le c_i \le 10^9)$$$.

Then follow $$$n-1$$$ lines, each with a pair of integers $$$a_i, b_i$$$, describing the edges of the tree $$$(1 \le a_i, b_i \le n, a_i \neq b_i)$$$.

Each test case ends with a line with $$$k$$$ integers, $$$v_1, v_2, \ldots, v_k$$$, such that Esomer needs to have a note-taker at $$$v_i$$$ at hour $$$i$$$ for all $$$1 \le i \le k$$$ $$$(1 \le v_i \le n)$$$.

It is guaranteed that the sum of $$$n$$$ over all test cases is at most $$$10^5$$$ and that the sum of $$$k$$$ over all test cases is at most $$$200$$$.

Output

For each test case, output a single integer equal to the minimum possible amount that Esomer must pay.

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

B. grippy
time limit per test
1 second
memory limit per test
512 megabytes
input
standard input
output
standard output

We have a matrix with $$$n$$$ rows and $$$m$$$ columns, where all its values are initially set to $$$0$$$.

Additionally, we have $$$q$$$ operations of two types:

$$$*$$$ The first type of operation flips (changes $$$0$$$ to $$$1$$$ and $$$1$$$ to $$$0$$$) an entire row or column.

$$$*$$$ The second type of operation queries the sum of a submatrix.

The top-left corner of the matrix corresponds to position $$$(1, 1)$$$, and the bottom-right corner corresponds to position $$$(n, m)$$$.

Input

The first line contains a natural number $$$t$$$ $$$(1 \le t \le 10^5)$$$, which is the number of cases.

The first line of each case contains three integers $$$n, m, q$$$ $$$(1 \le n, m, q \le 10^5)$$$, where $$$n$$$ is the number of rows, $$$m$$$ the number of columns, and $$$q$$$ the number of operations.

For each query, the first number is $$$1$$$ if a row or column is flipped, or $$$2$$$ if the sum of a submatrix is requested.

If the query is of type $$$1$$$, two more numbers $$$c$$$ and $$$d$$$ are provided. If $$$c = 1$$$, a column will be flipped, and if $$$c = 2$$$, a row will be flipped. Finally, $$$d$$$ corresponds to the index of the row or column that will be flipped, $$$(1 \le d \le n)$$$ or $$$(1 \le d \le m)$$$.

Queries of type $$$2$$$ are given by four numbers $$$a, b, c, d$$$ where $$$(a, b)$$$ represents the top-left corner and $$$(c, d)$$$ the bottom-right corner of the submatrix, $$$(1 \le a \le c \le n)$$$ and $$$(1 \le b \le d \le m)$$$.

It is guaranteed that the total sum of $$$n, m$$$, and $$$q$$$ is less than $$$10^5$$$.

Output

For each query of type $$$2$$$, you must respond with the sum of the specified submatrix.

Example
Input
2
3 5 8
2 1 1 3 5
1 1 3
2 1 1 3 5
1 1 5
2 1 1 3 5
1 1 4
2 1 1 3 5
2 1 2 2 4
3 5 5
2 1 1 1 1
1 2 3
1 1 4
1 1 5
2 1 1 3 4
Output
0
3
6
9
4
0
5

C. Poniendo aristas
time limit per test
1 second
memory limit per test
512 megabytes
input
standard input
output
standard output

It's your first time visiting Bratislava, and because your reputation as a good programmer precedes you, the mayor of Bratislava appoints you to solve a problem.

Bratislava has $$$n$$$ villages, but there are no roads connecting any two villages. In other words, you cannot travel from one village to another; each village is initially isolated. Building roads is expensive, so the mayor has a maximum budget that allows for constructing up to $$$m$$$ roads.

Your task is to minimize the maximum possible distance between all the villages.

Input

The first line will consist of a single natural number $$$t$$$, ($$$1 \le t \le 10^5$$$) — the number of test cases.

The first line of each test case contains the natural numbers $$$n$$$ and $$$m$$$, ($$$2 \le n \le 5 \cdot 10^8$$$, $$$0 \le m \le 5 \cdot 10^8$$$).

Output

For each test case, print the minimum maximum distance if Bratislava has $$$n$$$ villages and you can build $$$m$$$ roads. If it is impossible to connect all the villages, print $$$-1$$$.

Example
Input
3
3 2
4 8
11 2
Output
2
1
-1

D. sumaXOR
time limit per test
1 second
memory limit per test
512 megabytes
input
standard input
output
standard output

We have an infinite matrix $$$A$$$ defined as: $$$A_{i,j} = i + j$$$ for all $$$i, j$$$ such that $$$0 \le i, j$$$.

An infinite matrix $$$B$$$ is defined as: $$$\forall i,j$$$ such that $$$0 \le i$$$ and $$$1 \le j$$$ we have $$$B_{i,j}$$$ $$$=$$$ $$$A_{i,j-1}$$$ $$$\oplus$$$ $$$A_{i,j}$$$ $$$\oplus$$$ $$$A_{i,j+1}$$$.

Here $$$\oplus$$$ denotes the bitwise XOR operator.

If you are unfamiliar with XOR, please make sure to check out Esomer's XOR ultimate guide.

You are now given $$$q$$$ queries of the type $$${a, b, c, d}$$$. You are asked to find the total sum of all $$$B_{i,j}$$$ such that $$$a \le i \le c$$$ and $$$b \le j \le d$$$.

Input

First line contains $$$q$$$, the number of queries $$$(1 \le q \le 2 \cdot 10^5)$$$.

Each query contains four numbers $$$a, b, c, d$$$, respectively. $$$(0 \le a \le c \le 2 \cdot 10^5)$$$, $$$(1 \le b \le d \le 2 \cdot 10^5)$$$.

Output

Output for each query the total sum of all $$$B_{i,j}$$$ contained in the range.

Example
Input
6
1 1 1 1
2 2 2 2
3 3 4 4
2 3 5 6
0 1 2 3
1 3 5 10
Output
0
2
28
128
29
380

E. Paths of Gridland
time limit per test
3 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

Gridland is a mysterious world which exists inside of the Cartesian plane.

According to tradition, perhaps superstition, all the roads in Gridland must be made of linear segments parallel to the $$$x$$$ axis or the $$$y$$$ axis. The trains of Gridland have a very interesting property: they can only turn at most twice in one journey. This used to be an inconvenience for its citizens, who sometimes had to take more than one train in order to travel between two cities.

After a civil war broke out, the entire railway infrastructure of the country was destroyed. The new Minister of Transportation, Gustavo, has proposed an ambitious plan to build a new railway network that allows direct train travels between every city. However, Gustavo has to devise a budget to determine the project's feasibility. He needs your help to figure out the minimum total length of the new railway network.

Input

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

The first line of each test case is an integer $$$n$$$ $$$(1 \le n \le 2 \cdot 10^5)$$$, the number of cities in Gridland.

The second line of each test case contains $$$n$$$ pairs of integers $$$-10^9 \le x_i, y_i \le 10^9$$$, where $$$x_i$$$ and $$$y_i$$$ are the $$$x$$$ and $$$y$$$ coordinates, respectively, of the $$$i$$$-th city.

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

Output

For each test case, print one integer, the minimum length of roads needed to connect all of the cities such that it is always possible to travel between any two of them with at most two turns.

Example
Input
2
9
-3 0
-2 -2
-1 1
-1 4
0 0
1 3
2 -1
3 2
3 3
3
0 0
0 1
0 3
Output
19
3

F. AB
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given $$$l$$$ and $$$r$$$ and you are asked to calculate the number pairs $$$(a, b)$$$ in that interval interval such that $$$a \lt b$$$, $$$a \oplus b \gt b$$$ and $$$l \le a, b \le r$$$.

Here $$$\oplus$$$ denotes XOR bitwise operation.

If you are unfamiliar with XOR, please visit Esomer's XOR ultimate guide.

Input

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

The first line of each test case contains two integers $$$l$$$, $$$r$$$, $$$(1 \le l \le r \le 10^9)$$$.

Output

You should print the number of pairs that fulfil those conditions.

Example
Input
4
4 8
1 2
8 15
2 6
Output
4
1
0
4

G. enCAMAdo
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

We are playing "Twister" with Echomel, but he is a dirty cheater, and we believe not all his limbs are inside the game panel.

You are given two pairs of integers representing the coordinates of the bottom-left and top-right corners of the Twister panel, respectively. The game panel is a rectangle.

Next, you will be given 4 pairs of integers, representing the coordinates where Echomel has placed his left hand, right hand, left foot, and right foot, respectively.

Note 1: Echomel is VERY flexible.

Input

The first line contains an integer $$$t$$$, $$$(1 \le t \le 1000)$$$.

The first line for each case contains the coordinates of the rectangle $$$x1, y1, x2, y2$$$, $$$(-10^9 \le x1, y1, x2, y2 \le 10^9)$$$, $$$(x1 \lt x2)$$$ y $$$(y1 \lt y2)$$$.

The first line for each case contains the coordinates of Echomel's hands and feet $$$mi_x, mi_y, md_x, md_y$$$, $$$pi_x, pi_y, pd_x, pd_y$$$, $$$(-10^9 \le mi_x, mi_y, md_x, md_y$$$, $$$pi_x, pi_y, pd_x, pd_y \le 10^9)$$$ — The coordinates of each hand and foot.

$$$m = hand$$$, $$$p = foot$$$, $$$i = left$$$, $$$d = right$$$.

Output

Print YES if Echomel is playing legitimately; otherwise, he will need to be punished, so print NO if Echomel is being a bad boy, both responses without quotes.

Example
Input
2
1 1 5 5
1 5 5 5 1 1 1 5
2 2 3 4
2 2 3 4 4 4 0 1
Output
YES
NO

H. Alcarrian survey
time limit per test
2 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

Guadalajara is famous for its majestic mountains in the north and its well-known coastal town, Villanueva de la Torre. The regal, grand and dignifiedis danx is visiting Hectorungo, and during his visit, he will conduct a survey about the water in the alcarrian towns.

We have $$$2n-1$$$ towns, all associated with a river, organized in a tree structure where town $$$2n-1$$$ is the root.

In each town with index $$$i$$$ $$$(1 \leq i \leq n)$$$, a river originates with color $$$i$$$. Each town with index $$$j$$$ $$$(n+1 \leq j \leq 2n-1)$$$ connects two towns with smaller indices $$$a$$$ and $$$b$$$ $$$(a, b \lt j)$$$, and the rivers from these towns merge at $$$j$$$. The river of town $$$j$$$ will contain all the colors of the rivers from towns $$$a$$$ and $$$b$$$.

Each town has a population of $$$p_i$$$ inhabitants $$$(1 \leq p_i \leq 10^9)$$$.

Dani will ask $$$q$$$ questions. In each question, he will give a number $$$r$$$ followed by $$$r$$$ distinct numbers $$$c_1, c_2, \dots, c_r$$$ $$$(1 \leq c_i \leq n)$$$, where $$$c_i$$$ denotes a color.

Your task is to determine how many inhabitants live in towns whose river contains at least one of the colors given in the query.

Input

The first line contains an integer $$$t$$$ $$$(1 \leq t \leq 10^5)$$$, indicating the number of test cases. Each test case is defined as follows:

An integer $$$n$$$ $$$(1 \leq n \leq 10^5)$$$, the number of towns where a river originates.

A list of $$$2n-1$$$ integers $$$p_1, p_2, \dots, p_{2n-1}$$$ $$$(1 \leq p_i \leq 10^9)$$$, where $$$p_i$$$ is the number of inhabitants in town $$$i$$$.

$$$n-1$$$ additional lines, where the $$$j$$$-th line $$$(n+1 \leq j \leq 2n-1)$$$ contains two integers $$$a$$$ and $$$b$$$ $$$(1 \leq a, b \lt j)$$$, representing the indices of the towns whose rivers merge at town $$$j$$$.

A line with an integer $$$q$$$ $$$(1 \leq q \leq 2 \cdot 10^5)$$$, indicating the number of queries.

$$$2q$$$ additional lines, where each line describes a query.

Each query contains on the first line an integer $$$r$$$ $$$(1 \leq r \leq 10^5)$$$. The second line of each query will contain $$$r$$$ distinct integers $$$c_1, c_2, \dots, c_r$$$ $$$(1 \leq c_i \leq n)$$$, representing the river colors to query.

The total sum of $$$r$$$ in all queries across all cases will not exceed $$$2 \cdot 10^5$$$ and the sum of $$$n$$$ across all casses will not exceed $$$1 \cdot 10^5$$$.

Output

For each query, you must tell Dani how many inhabitants live in a town whose river contains at least one of the colors given in the query.

Example
Input
2
4
1 2 3 4 5 6 7
1 2
3 4
5 6
4
1
1
1
4
2
1 2
3
1 3 4
4
31 14 15 92 65 35 89
1 2
3 5
4 6
4
2
1 5
2
2 4
3
2 3 4
2
6 7
Output
13
17
15
26
220
295
310
124

I. CAMA's Ranking
time limit per test
1 second
memory limit per test
512 megabytes
input
standard input
output
standard output

Litusiano was solving a problem during the prestigious CAMA competition (the best competition in the world) when he came up with the following problem:

In CAMA, all participants deserve to be happy. You are given an integer $$$n$$$ and an array of integers $$$a$$$ of length $$$n$$$. The $$$i$$$-th participant will be happy if they are in one of the first $$$a_i$$$ positions in the final ranking. Your task is to find the lexicographically smallest ranking such that all $$$n$$$ participants are happy, i.e., each participant $$$i$$$ is placed within the first $$$a_i$$$ positions of the ranking.

Lexicographical order means that, given two arrays $$$x$$$ and $$$y$$$ of the same length, $$$x$$$ is smaller than $$$y$$$ if, at the first position $$$k$$$ where they differ, $$$x_k \lt y_k$$$. For example: - $$$[1, 2, 3]$$$ is smaller than $$$[1, 3, 2]$$$ because at the second position $$$2 \lt 3$$$. - $$$[2, 1, 3]$$$ is greater than $$$[1, 3, 2]$$$ because at the first position $$$2 \gt 1$$$.

In the spirit of CAMA, where everyone should leave the competition with a smile, your task is to ensure that all participants are happy. If it's not possible to construct such a ranking, return -1.

Input

The input consists of multiple test cases. The first integer $$$t$$$ ($$$1 \leq t \leq 100$$$) denotes the number of test cases. The description of each test case is as follows:

- The first line contains one integer $$$n$$$ ($$$1 \leq n \leq 69 \cdot 10^4$$$) — the number of participants. - The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \leq a_i \leq n$$$) — the values of the array $$$a$$$, where each value represents the maximum position in the ranking where the $$$i$$$-th participant will be happy.

The sum of $$$n$$$ won't exceed $$$69 \cdot 10^4$$$.

Output

For each test case, print the lexicographically smallest ranking such that all participants are happy. If it is not possible, print -1.

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

J. Cottage
time limit per test
1 second
memory limit per test
512 megabytes
input
standard input
output
standard output

After Héctor got his Architecture degree, he received a commission from Sheikh Artur. Since Artur is an extravagant individual, he has requested that his new house be shaped like a convex polygon with marble pillars in the countryside. He has also asked for a pillar at each vertex of the polygon, although he doesn't mind if there are extra columns inside that are not at the vertices.

However, Héctor forgot that the cottage is going to be built next to a lake with the shape of a convex polygon until after he had already placed all the marble columns, and the house he designs intersects with the lake. Obviously, he won't be able to build the cottage inside the lake, and since there's no time to redesign the building, Héctor has decided to improvise using the pillars that have already been placed. Help Héctor determine the maximum number of pillars that can be in Artur's house.

For this problem, you may consider a straight line segment or a single point as a convex polygon.

A few clarifications:

Even though some columns may not be in the water, part of the house built using them might end up inside the lake. This is not allowed.

Some of the vertices of the lake may be collinear.

We say that the house and the lake intersect only if their areas overlap. The house can be built even if its perimeter overlaps with the perimeter of the lake.

Input

You are first given an integer $$$t$$$ ($$$1 \le t \le 10$$$), the number of test cases.

The first line of each test case consists of two positive integers $$$n$$$ ($$$3 \le n \le 100$$$) and $$$m$$$ ($$$1 \le m \le 10^4$$$), the number of vertices of the lake and the amount of pillars, respectively. These are followed by $$$n$$$ lines containing the coordinates $$$x_i$$$, $$$y_i$$$ of the $$$i$$$-th of the lake ($$$1 \le i \le n$$$), which will be given in clockwise order. It is guaranteed that the vertices in the given order form a convex polygon.

After that, there will be $$$m$$$ lines with the coordinates $$$a_j$$$, $$$b_j$$$ ($$$1 \le j \le m$$$) of the $$$j$$$-th pillar.

It is guaranteed that the sum of $$$m$$$ over all test cases is at most $$$10^4$$$.

Output

Print an integer $$$x$$$, the maximum amount of pillars that Héctor can use to build the cottage while satisfying all of Sheikh Artur's requirements.

If it's impossible to build such a house, print 0.

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

K. Dibonacci Numbers
time limit per test
1 second
memory limit per test
512 megabytes
input
standard input
output
standard output

Litusiano was pooing when he came up with the following problem:

Given $$$l$$$ and $$$r$$$, you have to count how many dibonacci numbers are in the range $$$[l, r]$$$.

A dibonacci number is an integer whose digits are the sum modulo $$$10$$$ of the two previous digits.

For example, $$$123$$$ is a dibonacci number, because $$$(1 + 2)$$$ mod $$$10 = 3$$$, $$$471$$$ is also a dibonacci number because $$$(4 + 7)$$$ mod $$$10 = 1$$$ and $$$2169$$$ is not a dibonacci number because $$$(1 + 6)$$$ mod $$$10 = 7$$$, which is different from $$$9$$$.

Note that all the positive integers $$$\leq 99$$$ are also dibonacci numbers.

Input

Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ $$$(1 \leq t \leq 69 \cdot 10^{4})$$$. The description of the test cases follows.

For each test case:

There is a line consisting of two integers $$$l, r$$$ $$$(1 \leq l \leq r \leq 10^{18})$$$ — the asked range.

Output

For each test case, output how many dibonacci numbers are in the given range.

Example
Input
5
2 7
1 124
6 977733
7773 36969
100000000 1000000000
Output
6
102
452
49
90
Note

In the first test case, the answer is $$$6$$$ because all the numbers in the range $$$[2, 7]$$$ are dibonacci numbers.

In the second case, there are $$$102$$$ dibonacci numbers in the range $$$[1, 124]$$$, like $$$101$$$ or $$$69$$$.

L. pi-ip
time limit per test
1 second
memory limit per test
512 megabytes
input
standard input
output
standard output

Litusiano was lying in the CAMA while looking at a photo of Emilio when he came up with the following problem:

You are given a permutation $$$p$$$ with some elements missing. Your task is to count how many ways you can fill those missing elements such that the result is still a valid permutation, and the permutation $$$q$$$ obtained by the following process:

For each $$$i$$$ from $$$1$$$ to $$$n$$$ (going from left to right), set $$$q[p[i]] = i$$$.

The resulting permutation $$$q$$$ must be the same as the original permutation $$$p$$$.

You are asked to output the value of the count modulo $$$998244353$$$.

Explanation of permutation:

A permutation is an arrangement of a set of elements in a specific order. For example, for the set $$$\{1, 2, 3\}$$$, the following are some permutations:

- $$$[1, 2, 3]$$$ - $$$[2, 1, 3]$$$ - $$$[3, 1, 2]$$$ - $$$[2, 3, 1]$$$

In this problem, a permutation is defined for a sequence of integers from $$$1$$$ to $$$n$$$. The goal is to fill in missing elements in $$$p$$$ in such a way that it remains a valid permutation and satisfies the given condition for $$$q$$$.

Input

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

For each test case:

- The first line consists of one integer $$$n$$$ ($$$1 \leq n \leq 69 \cdot 10^{4}$$$) — the length of the array $$$a$$$ and the permutation. - The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-1 \leq a_i \leq n$$$) — the elements of the array $$$a$$$.

Output

For each test case, output a single integer: the number of ways to fill the missing elements of the permutation $$$p$$$ such that the permutation $$$q$$$ obtained from the process:

For each $$$i$$$ from $$$1$$$ to $$$n$$$ (going from left to right), set $$$q[p[i]] = i$$$,

is the same as the original permutation $$$p$$$. The result should be output modulo $$$998244353$$$.

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

M. Learning to Write CAMA
time limit per test
1 second
memory limit per test
512 megabytes
input
standard input
output
standard output

Litusiano was resting on his bed when he thought of the following problem:

You are given two integers, $$$n$$$ and $$$m$$$. Your task is to draw a letter "C" made of the character '#' with height $$$n$$$ and length $$$m$$$. After that, you need to print the word "AMA" at the bottom of the "C".

The "C" should have the following properties:

1. The "C" should have a height of $$$n$$$ rows. 2. The "C" should have a width of $$$m$$$ columns. 3. The top and bottom parts of the "C" should be fully filled with '#'. 4. The left part of the "C" should be a column of '#' characters, except for the top and bottom rows. 5. After printing the "C", print the word "AMA" in the next row.

Input

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

For each test case:

- The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n, m \leq 69$$$) — the height and length of the C of CAMA.

Output

For each test case, print the "C" followed by the word "AMA" on the next line.

- The "C" should have a height of $$$n$$$ rows and a width of $$$m$$$ columns. - The top and bottom parts of the "C" should be fully filled with '#'. - The left part of the "C" should be a column of '#' characters, except for the top and bottom rows. - After printing the "C", print the word "AMA" on the next line.

Example
Input
5
4 1
2 3
6 9
2 6
1 9
Output
#
#
#
#AMA
###
###AMA
#########
#
#
#
#
#########AMA
######
######AMA
#########AMA

N. First Problem of CAMA 2024
time limit per test
1 second
memory limit per test
512 megabytes
input
standard input
output
standard output

Are you excited?

Input

The first question of CAMA 2024

Output

Tell the truth ;)

Example
Input
Are you excited?
Output
Obviamente. Me gusta Esomer

O. Minimum Diameter Queries
time limit per test
2 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

Litusiano was listening to La Mafia Del Amor when he came up with the following problem:

You are given $$$n$$$ trees, where the $$$i$$$-th tree has $$$m_i$$$ nodes. You need to answer $$$q$$$ queries.

Each query consists of two integers $$$l$$$ and $$$r$$$. For each query, you are asked to find the minimum diameter of the resulting tree if you optimally merge all the trees in the range $$$[l, r]$$$.

To merge two trees, you add exactly one edge between a node from each tree. Refer to the sample explanation for a better understanding of the merging process.

Note: The diameter of a tree is defined as the longest path between any two nodes in the tree.

Input

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

For each test case:

The first line consists of a single integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^{5}$$$) — the number of trees.

Then follows the representation of the $$$n$$$ trees. For each tree:

  • The first line consists of one integer $$$m_i$$$ ($$$1 \leq m_i \leq 2 \cdot 10^{5}$$$) — the number of nodes of the $$$i$$$-th tree.
  • Then follow $$$m_i - 1$$$ lines, each one consisting of two integers $$$u$$$, $$$v$$$ ($$$1 \leq u, v \leq m_i$$$) — the edges of the $$$i$$$-th tree.

After the representation of all the trees, a line will follow with one integer $$$q$$$ ($$$1 \leq q \leq 2 \cdot 10^{5}$$$) — the number of queries.

The following $$$q$$$ lines will contain two integers $$$l$$$, $$$r$$$ ($$$1 \leq l \leq r \leq n$$$) — the ranges for the queries.

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

Output

For each query, output the minimum diameter of the resulting tree if you merge optimally all the trees in the range $$$[l, r]$$$.

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

In the first test case, the answer to the query is 4 because if you add an edge between node $$$2$$$ of the first tree and node $$$1$$$ of the second tree, the diameter is $$$4$$$, and it's impossible to merge both trees in such a way that results in a smaller diameter.

P. PELODE
time limit per test
1 second
memory limit per test
512 megabytes
input
standard input
output
standard output

Litusiano was reading a book called Rich Dad Poor Dad when he came across the following problem:

You are given an integer $$$n$$$ and an array $$$a$$$ of length $$$n$$$. Your task is to find a permutation $$$p$$$ of length $$$n$$$ (i.e., an arrangement of the numbers from $$$1$$$ to $$$n$$$) that satisfies the following conditions:

1. For every index $$$i$$$ ($$$1 \leq i \leq n$$$): - Let $$$a_i$$$ be the smallest index greater than $$$i$$$ ($$$i \lt a_i \leq n$$$) such that $$$p_i \lt p_{a_i}$$$, or $$$a_i = -1$$$ if no such index exists. 2. The permutation $$$p$$$ must be the lexicographically smallest one that satisfies this property, or it must be determined that no such permutation exists.

Note on lexicographical order:

Lexicographical order (or lexicographically smallest) means that, given two arrays $$$x$$$ and $$$y$$$ of the same length, $$$x$$$ is smaller than $$$y$$$ in this order if at the first position $$$k$$$ where they differ, $$$x_k \lt y_k$$$. For example: - $$$[1, 2, 3]$$$ is smaller than $$$[1, 3, 2]$$$ because at the second position, $$$2 \lt 3$$$. - $$$[2, 1, 3]$$$ is greater than $$$[1, 3, 2]$$$ because at the first position, $$$2 \gt 1$$$.

If no permutation satisfying the given property exists, it should be explicitly stated.

Input

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

For each test case:

- The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 69 \cdot 10^4$$$) — the length of the array $$$a$$$ and the permutation $$$p$$$. - The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-1 \leq a_i \leq n$$$) — the elements of the array $$$a$$$.

It is guaranteed that the sum of $$$n$$$ across all test cases does not exceed $$$69 \cdot 10^4$$$.

Output

For each test case, output the lexicographically smallest permutation that satisfies all the conditions or print "No hay PELODE :(", without the quotes, if such a permutation does not exist.

Example
Input
2
5
-1 5 4 5 -1
5
-1 4 5 5 -1
Output
5 3 1 2 4 
No hay PELODE :(

Q. Rigo's Bathtub
time limit per test
2 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

Rigo wants to take a bath, and for that, he needs to fill his bathtub. Rigo has $$$n$$$ buckets of water, each containing $$$p_i$$$ liters, where $$$p$$$ is a permutation of size $$$n$$$. Additionally, his bathtub can hold $$$2n-1$$$ liters if the water is cold and $$$2n+1$$$ liters if it is hot. Although Rigo is a very good mathematician, he is disorganized and has forgotten to label each bucket with its volume, so he asks for your help to assign each bucket its corresponding label in liters.

To do this, you can ask two types of questions:

  1. You provide a set of buckets $$$a_0, a_1, \ldots, a_m$$$ $$$(1 \le a_i \le n$$$, and such that there are no $$$i \neq j$$$ with $$$a_i = a_j$$$) and the temperature, cold or hot. In response, Rigo will tell you if you can fit the $$$m$$$ buckets of water at that temperature in the bathtub.
  2. You can ask Javi for help. If you give Javi a number $$$r$$$ $$$(1 \le r \le n)$$$, Javi will tell you which $$$i$$$ satisfies $$$p_i = r$$$, but since he is busy playing CS:GO, it will cost you dearly to ask him.

Write a program that returns the permutation $$$p$$$ without running out of coins, making at most $$$2$$$ questions of type $$$2$$$ and $$$100000$$$ questions of type $$$1$$$.

Input

The first line of the input contains a single integer $$$T$$$, the number of cases to solve. $$$(1 \le T \le 10)$$$.

Each case starts with a single integer $$$n$$$, the number of buckets, followed by the interaction. $$$(1 \le n \le 6000)$$$.

It is guaranteed that the sum of $$$n$$$ over all tests does not exceed $$$6000$$$.

Interaction

To make a type $$$1$$$ question, the program must have the following format.

  • "$$$1$$$ $$$m$$$ $$$c$$$ $$$a_0$$$ $$$a_1$$$ $$$\ldots$$$ $$$a_m$$$" (without quotes). Where $$$m$$$ is the number of buckets you want to include, $$$a_i$$$ are the indices of the buckets you include, and $$$c$$$ is a character that must be 'F' or 'C', where 'F' denotes that the temperature of the water is cold and 'C' that it is hot. There cannot be repeated elements in $$$a$$$.
To a type $$$1$$$ question, the judge will respond with an integer $$$x$$$. $$$x$$$ is $$$1$$$ if the water from all the buckets at that temperature fits in Rigo's bathtub, and $$$0$$$ otherwise.

To make a type $$$2$$$ question, the format must be as follows:

  • "$$$2$$$ $$$r$$$" (without quotes). Where $$$r$$$ is the amount of water for which you want to know which bucket has exactly $$$r$$$ liters.
To a type $$$2$$$ question, the judge will respond with an integer $$$i$$$, indicating that $$$p_i = r$$$.

Finally, to provide the answer, the program must print a single line in the following format:

  • "! $$$p_0$$$ $$$p_1$$$ $$$\ldots$$$ $$$p_n$$$" (without quotes).
After that, the program should continue to the next case or terminate if it was already at the last case.

If your program asks more than $$$2$$$ questions of type $$$2$$$ or $$$10^5$$$ questions of type $$$1$$$ for a test case, it must terminate immediately to receive the verdict Wrong Answer. Otherwise, you could receive an arbitrary verdict because your solution will attempt to continue reading from a closed stream.

Additionally, the sum of $$$m$$$ over all questions of type $$$1$$$ over all tests must not exceed $$$10^7$$$.

After printing a question or the answer, do not forget to write the end-of-line character and flush the output buffer. Otherwise, you might get the verdict Time limit exceeded. To do this, use:

  • fflush(stdout) or cout.flush() in C++.
  • System.out.flush() in Java.
  • stdout.flush() in Python.
  • Check the documentation for other languages.
Example
Input
1
5
1
0
1
3
Output
2 3
1 3 F 1 4 2
1 3 C 1 4 2
2 4
! 3 2 4 5 1
Note

Note that, with the questions asked, you are not guaranteed that the answer is correct. This sample is just for illustrative effect.