IV DCC CODE CUP
A. Donuts and Cupcakes
time limit per test
1 second
memory limit per test
1024 megabytes
input
standard input
output
standard output

Bira's cooking skills have made him famous across the country, earning him an invitation to compete in the prestigious MasterChef: Pastry Edition! For the Halloween episode, he has prepared a mouthwatering tray of donuts and cupcakes. The $$$n$$$ treats are arranged in a single row, represented by a string $$$s$$$: each D represents a donut and each C represents a cupcake.

Just as he finishes, Bira remembers that the judges also care about presentation! This time, they require every segment of five consecutive treats to contain exactly one donut and four cupcakes.

Bira must hurry to rearrange his tray. He may reorder the treats however he likes, but must use every treat he has made, without adding, removing, or changing any of them. Help him find an arrangement that meets the judges' requirement, or determine that it is impossible.

Input

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

Each test case consists of two lines. The first contains an integer $$$n$$$ ($$$5 \leq n \leq 2 \times 10^{5}$$$), the number of treats. The second contains a string $$$s$$$ of length $$$n$$$, consisting only of D and C, describing their initial arrangement.

The sum of $$$n$$$ over all test cases does not exceed $$$2 \times 10^{5}$$$.

Output

For each test case, print -1 on a single line if no arrangement of Bira's treats meets the requirement.

Otherwise, print a string of length $$$n$$$ representing a valid arrangement. It must contain the same number of each character as $$$s$$$. If several valid answers exist, print any of them.

Example
Input
5
10
DDCCCCCCCC
6
CCCCDC
6
DDCCCC
5
DDCCC
11
DCCCCCCCCCC
Output
DCCCCDCCCC
CCCCDC
DCCCCD
-1
-1
B. Catan
time limit per test
1 second
memory limit per test
1024 megabytes
input
standard input
output
standard output

Fred is a professional Catan player. In this board game, players collect resources to build cities and roads. After so many games, he is confident he could find a job in construction if he ever wanted a career change. Today, he wants to broaden his experience by building as many roads as possible!

To build a road, Fred must spend one lumber card and one brick card. He currently has $$$m$$$ lumber cards and $$$t$$$ brick cards. If he runs short of either resource, he can trade with the bank: four lumber cards for one brick card, or four brick cards for one lumber card. He may make as many of these trades as he likes.

Help Fred determine the maximum number of roads he can build using his cards and these trades.

Input

The input consists of one line containing two integers $$$m$$$ and $$$t$$$ ($$$0 \le m,t \le 10^{9}$$$), Fred's numbers of lumber and brick cards, respectively.

Output

Print a single integer: the maximum number of roads Fred can build.

Examples
Input
8 3
Output
4
Input
4 0
Output
0
Note

Explanation for example 1

Fred builds three roads, trades four lumber cards for one brick card, and builds a fourth road.

Explanation for example 2

If Fred trades all four lumber cards, he will have no lumber left to build a road.

C. Pilates
time limit per test
1 second
memory limit per test
1024 megabytes
input
standard input
output
standard output

After years of training for programming contests, MkVeio wants a more physically active routine. While looking for an enjoyable activity, he discovers Pilates!

Pilates is an exercise method developed by Joseph Pilates that emphasizes the connection between mind and body. At MkVeio's studio, workouts cycle through four pieces of equipment in this order:

  1. Reformer;
  2. Cadillac;
  3. Chair;
  4. Barrel.
1. Reformer2. Cadillac
3. Chair4. Barrel

The first workout uses the Reformer, the second the Cadillac, the third the Chair, and the fourth the Barrel. The fifth returns to the Reformer, and the cycle continues.

MkVeio has signed up for $$$n$$$ workouts before the end of the year. He is very disciplined and is certain he will attend them all. Which piece of equipment will he use in his last workout of the year?

In his prime, MkVeio would have written a program to answer this in under a minute, but he is tired and would rather ask for your help.

Input

The input consists of a single line containing an integer $$$n$$$ ($$$1 \leq n \leq 10^{5}$$$), the number of workouts MkVeio will attend before the end of the year.

Output

Print the name of the equipment used in his $$$n$$$-th workout: Reformer, Cadillac, Chair, or Barrel.

Examples
Input
1
Output
Reformer
Input
7
Output
Chair
D. Matryoshkas
time limit per test
1 second
memory limit per test
1024 megabytes
input
standard input
output
standard output

Matryoshkas are traditional Russian wooden dolls that nest inside one another. Mathias has found a box full of them and wants to put together an exhibition!

The box contains $$$n$$$ separate dolls. Doll $$$j$$$ has size $$$a_j$$$, an integer between $$$1$$$ and $$$n$$$. Several dolls may have the same size.

To assemble a set of size $$$i$$$, Mathias must choose exactly one doll of each size $$$1,2,\ldots,i$$$ and nest them from smallest to largest. Such a set uses $$$i$$$ dolls, and its outermost doll has size $$$i$$$. A single doll of size $$$1$$$ also forms a valid set.

To make the exhibition varied, Mathias wants all the sets to have distinct sizes. Each doll may belong to at most one set, and he does not have to use every doll in the box.

What is the largest number of sets he can assemble?

Input

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

For each test case, the first line contains an integer $$$n$$$ ($$$1 \leq n \leq 10^{5}$$$), the number of dolls. The second line contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \leq a_j \leq n$$$), their sizes.

The sum of $$$n$$$ over all test cases does not exceed $$$10^{5}$$$.

Output

For each test case, print a single integer on a separate line: the maximum number of sets of distinct sizes that can be assembled.

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

In the second test case, sets of sizes $$$1$$$ and $$$2$$$ can be assembled, leaving the doll of size $$$3$$$ unused. In the fourth test case, only one set of size $$$1$$$ can be assembled, since all set sizes must be distinct. In the fifth test case, sets of sizes $$$1$$$, $$$2$$$, and $$$3$$$ use all the dolls.

E. Antisocial Theatergoers
time limit per test
1 second
memory limit per test
1024 megabytes
input
standard input
output
standard output

Laila is going to the theater to see a performance by Clube da Esquina, a celebrated Brazilian musical collective. Unfortunately, something has come up, and she will be the last person to arrive.

The theater has a single row of $$$n$$$ seats, numbered from $$$1$$$ to $$$n$$$, with an aisle at each end. The aisles are represented by positions $$$0$$$ and $$$n+1$$$. The distance between positions $$$i$$$ and $$$j$$$ is $$$|i-j|$$$.

The show is sold out. The $$$n-1$$$ people arriving before Laila enter one at a time and are extremely antisocial: each wants to sit as far as possible from both the people already seated and the aisles.

The comfort of an empty seat is the minimum distance from that seat to any occupied seat or aisle, measured when the person chooses their seat. Each person chooses an empty seat with maximum comfort. If several seats tie, they may choose any of them.

On her way to the theater, Laila can think of only one thing: which seat will be left for her?

For each seat $$$k$$$, from $$$1$$$ to $$$n$$$, determine how many sequences of choices by the $$$n-1$$$ people leave that seat empty. Two sequences are different if the seat chosen at some step differs. Since the answers may be large, print each answer modulo $$$998244353$$$.

Input

The input consists of a single line containing an integer $$$n$$$ ($$$2 \le n \le 10^{5}$$$), the number of seats.

Output

Print $$$n$$$ space-separated integers on a single line. The $$$k$$$-th integer must be the number of valid sequences of choices that leave seat $$$k$$$ as the last available seat, modulo $$$998244353$$$.

Examples
Input
3
Output
1 0 1
Input
4
Output
4 2 2 4
Note

Explanation for example 1

The first person chooses seat $$$2$$$. The second may choose seat $$$1$$$ or $$$3$$$, leaving the other empty. Thus, the answers are $$$1$$$, $$$0$$$, and $$$1$$$.

F. DDDDC
time limit per test
1 second
memory limit per test
1024 megabytes
input
standard input
output
standard output

While preparing a problem for the $$$3\text{D}(\text{C}^{4})$$$ contest, Emanuel became interested in abbreviations. Some seem intuitive to him, while others do not. He has asked you to write a program to tell them apart.

A string $$$t$$$ is an intuitive abbreviation of a string $$$s$$$ if $$$t$$$ can be obtained by deleting zero or more characters from $$$s$$$ without changing the order of the remaining characters. Each occurrence of a letter in $$$t$$$ must correspond to a distinct occurrence in $$$s$$$.

For example, DDDDC is an intuitive abbreviation of DIRTYDEEDSDONEDIRTCHEAP, and DCC is an intuitive abbreviation of DINOSSAUROCODECUP.

For each test case, determine whether the second string is an intuitive abbreviation of the first.

Input

The first line contains an integer $$$q$$$ ($$$1 \leq q \leq 10^{5}$$$), the number of test cases.

Each test case consists of one line containing two space-separated strings $$$s$$$ and $$$t$$$ ($$$1 \leq |s|,|t| \leq 10^{5}$$$). Both strings consist only of uppercase English letters.

The sum of the lengths of all strings $$$s$$$ and $$$t$$$ does not exceed $$$10^{6}$$$.

Output

For each test case, print SIM (Portuguese for "yes") if $$$t$$$ is an intuitive abbreviation of $$$s$$$, or NAO ("no") otherwise.

Example
Input
8
DCCCODECUP DCCCC
DCCCODECUP DDDDC
DIRTYDEEDSDONEDIRTCHEAP DDDDC
MVKAIO MK
HIGINOH HGH
DCCCCC DCCCC
ESTADOSUNIDOS EEUU
TREINAMENTOPARAAMARATONA TREM
Output
SIM
NAO
SIM
SIM
SIM
SIM
NAO
SIM
G. All Tied Up
time limit per test
1 second
memory limit per test
1024 megabytes
input
standard input
output
standard output

FIFA is organizing a football tournament with a group stage. One group contains $$$n$$$ national teams, numbered from $$$1$$$ to $$$n$$$. Each pair of teams in the group will play exactly once.

A win awards $$$3$$$ points to the winning team and $$$0$$$ to the losing team. A draw awards $$$1$$$ point to each team.

Your friend Pedro claims that this group will be so evenly matched that all $$$n$$$ teams will finish the group stage tied on exactly $$$k$$$ points. You want to find out whether this is possible.

"Stoppage time: until they equalize!"

Given $$$n$$$ and $$$k$$$, determine whether results can be assigned to the $$$\frac{n(n-1)}{2}$$$ matches so that every team finishes with exactly $$$k$$$ points. If so, print any set of results that demonstrates this.

Input

The input consists of a single line containing two integers $$$n$$$ and $$$k$$$ ($$$3 \leq n \leq 500$$$, $$$0 \leq k \leq 3n-3$$$), the number of teams and the desired final score of each team, respectively.

Output

If it is impossible for every team to finish with exactly $$$k$$$ points, print -1 on a single line.

Otherwise, print exactly $$$\frac{n(n-1)}{2}$$$ lines, one per match. Each line must have the form $$$a\ s\ b$$$, where $$$a$$$ and $$$b$$$ ($$$1 \leq a,b \leq n$$$, $$$a \neq b$$$) are teams and $$$s$$$ is one of the symbols $$$ \gt $$$, $$$=$$$, or $$$ \lt $$$.

Each pair of teams must appear on exactly one line. The symbols mean:

  • $$$ \gt $$$: team $$$a$$$ beat team $$$b$$$;
  • $$$ \lt $$$: team $$$a$$$ lost to team $$$b$$$;
  • $$$=$$$: the match ended in a draw.

You may print the matches in any order. If several valid answers exist, print any of them.

Examples
Input
4 6
Output
-1
Input
4 4
Output
1 = 2
3 = 4
2 < 3
2 > 4
1 > 3
4 > 1
H. Gepeto and Notation
time limit per test
1 second
memory limit per test
1024 megabytes
input
standard input
output
standard output

Professor G. Coutinho wrote his old proofs for Phantasmagorical Graph Theory in English, using the field's standard notation. For example, the Greek letter $$$\alpha$$$ always refers to independent sets, while $$$\chi$$$ refers to proper colorings. Now he is teaching the course again, this time in Portuguese.

Some frightening graphs from Phantasmagorical Graph Theory

Since he is very busy, he has asked his old friend Gepeto to help translate his notes. Gepeto, however, is rather absent-minded: he does not remember the notation well and tends to mix up the symbols as he translates. This has made the lectures very confusing, and Coutinho wants to decide whether he should translate the notes himself.

He asks each student which symbols they consider essential and which substitutions they could tolerate. He then wants to count the ways Gepeto could mix up the notation so that every student is confused: the worst possible outcome.

Formally, there are $$$n$$$ standard symbols, numbered from $$$1$$$ to $$$n$$$, and $$$m$$$ students. For each student $$$i$$$, the set $$$A_i$$$ contains the symbols that student considers essential. Gepeto replaces the symbols according to a permutation of the $$$n$$$ symbols. Student $$$i$$$ is confused if at least one symbol in $$$A_i$$$ is replaced by a different symbol. Count the permutations that confuse every student.

Professor Coutinho is not known for his programming skills, so he has left this task to you. Help him!

Input

The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n \leq 1000$$$, $$$1 \leq m \leq 16$$$), the number of symbols and the number of students, respectively.

Each of the next $$$m$$$ lines describes one student's essential set. Line $$$i$$$ starts with an integer $$$k_i$$$ ($$$1 \leq k_i \leq n$$$), the size of $$$A_i$$$, followed by $$$k_i$$$ distinct integers $$$a_{i,j}$$$ ($$$1 \leq a_{i,j} \leq n$$$), its elements.

Output

Print the number of permutations of the $$$n$$$ symbols such that, for every student $$$i$$$, at least one element of $$$A_i$$$ is replaced by a different symbol. Since the answer may be large, print it modulo $$$998244353$$$.

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

Explanation for example 1

Represent a permutation of the three symbols as a list in which the $$$i$$$-th element is the symbol used in place of symbol $$$i$$$. The possibilities are:

  • $$$1,2,3$$$: neither student is confused;
  • $$$1,3,2$$$: only the first student is confused;
  • $$$2,1,3$$$: both students are confused;
  • $$$2,3,1$$$: both students are confused;
  • $$$3,1,2$$$: both students are confused;
  • $$$3,2,1$$$: only the second student is confused.
Thus, the answer is $$$3$$$.
I. Dependency Inversion
time limit per test
2 seconds
memory limit per test
1024 megabytes
input
standard input
output
standard output

Luisa Lopes is studying for her Software Engineering exam and has gotten stuck on the SOLID principles, which underpin the course.

To help the ideas sink in, she decides to examine the dependency graph of a legacy project she maintains as a freelancer. Each vertex represents a module, and a directed edge from $$$u$$$ to $$$v$$$ means that module $$$u$$$ depends directly on module $$$v$$$. A well-designed project should have no cyclic dependencies: no module should depend, directly or indirectly, on itself. Luisa suspects that her project has exactly this problem.

Thinking about the "D" in SOLID, the Dependency Inversion Principle, she starts wondering about reversing dependencies. Could reversing a single dependency eliminate all the cycles at once?

With little time left before her exam, Luisa asks for your help. Given a directed graph $$$G$$$, determine whether it can be made acyclic by reversing at most one edge. Reversing an edge $$$u \to v$$$ means replacing it with $$$v \to u$$$.

Input

The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 2 \times 10^{5}$$$), the number of test cases.

The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n \leq 2 \times 10^{5}$$$, $$$0 \leq m \leq \min(3 \times 10^{5},n(n-1)/2)$$$), the numbers of vertices and directed edges.

Each of the next $$$m$$$ lines contains two integers $$$u$$$ and $$$v$$$ ($$$1 \leq u,v \leq n$$$, $$$u \neq v$$$), describing a directed edge from $$$u$$$ to $$$v$$$. For each unordered pair of vertices $$$\{u,v\}$$$, there is at most one edge between them.

The sum of $$$n$$$ over all test cases does not exceed $$$2 \times 10^{5}$$$, and the sum of $$$m$$$ does not exceed $$$3 \times 10^{5}$$$.

Output

For each test case, print on the first line: 0 if the graph is already acyclic; 1 if it is not acyclic but reversing one edge can make it acyclic; or -1 otherwise.

If you print 1, print a second line containing two integers $$$u$$$ and $$$v$$$, identifying an edge $$$u \to v$$$ whose reversal makes the graph acyclic. Any such edge is accepted. Print its endpoints in their original input order.

Examples
Input
1
4 4
1 2
2 3
3 1
3 4
Output
1
1 2
Input
1
6 6
1 2
2 3
3 1
4 5
5 6
6 4
Output
-1
Input
4
2 1
1 2
1 0
3 3
1 2
2 3
3 1
6 6
1 2
2 3
3 1
4 5
5 6
6 4
Output
0
0
1
1 2
-1
Note

Explanation for example 1

In the first example, reversing edge $$$1 \to 2$$$ breaks the only cycle, so printing 1 followed by 1 2 is accepted. Any other edge whose reversal makes the graph acyclic would also be accepted.

Explanation for example 2

Here, the graph is not acyclic, and reversing one edge cannot make it acyclic. The expected output is therefore -1.

J. Server
time limit per test
1 second
memory limit per test
1024 megabytes
input
standard input
output
standard output

Magner Weira has a server with $$$n$$$ components. Each component works only within a certain temperature range: component $$$i$$$ tolerates temperatures from $$$l_i$$$ to $$$r_i$$$, inclusive. If the temperature leaves this range, the component burns out permanently.

Magner has been so busy that he has neglected the server for $$$m$$$ days. Worried that some components may have burned out, he checks the room's temperature records. The only available record lists the minimum temperature $$$a_j$$$ and maximum temperature $$$b_j$$$ reached on each day $$$j$$$.

For each component, determine the first day on which it burned out, or report that it is still working after these $$$m$$$ days.

Input

The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n \leq 10^{5}$$$, $$$1 \leq m \leq 10^{5}$$$), the numbers of components and days.

Each of the next $$$n$$$ lines contains two integers $$$l_i$$$ and $$$r_i$$$ ($$$1 \leq l_i \leq r_i \leq 10^{5}$$$), the temperature limits of component $$$i$$$.

Each of the following $$$m$$$ lines contains two integers $$$a_j$$$ and $$$b_j$$$ ($$$1 \leq a_j \leq b_j \leq 10^{5}$$$), the minimum and maximum temperatures on day $$$j$$$.

Output

Print $$$n$$$ lines. Line $$$i$$$ must contain the first day on which component $$$i$$$ burned out, or $$$-1$$$ if it is still working after all $$$m$$$ days. Days are numbered from $$$1$$$ to $$$m$$$.

Examples
Input
6 4
10 20
1 100
15 16
12 18
5 11
9 18
12 16
14 20
9 17
10 26
Output
3
-1
1
2
1
2
Input
3 2
1 100000
5 5
1 1
5 5
1 5
Output
-1
2
1
Input
4 5
4 9
1 3
8 10
1 10
5 8
4 9
3 10
2 9
1 10
Output
3
1
1
-1
Note

Explanation for example 1

The first component tolerates temperatures between $$$10$$$ and $$$20$$$: it survives the first two days but burns out on the third, when the minimum is $$$9$$$. The third component burns out on the first day because the minimum, $$$12$$$, is below its lower limit of $$$15$$$. The second component tolerates all recorded temperatures.

K. Colored Dominoes
time limit per test
2 seconds
memory limit per test
1024 megabytes
input
standard input
output
standard output

Manuel loves puzzles and is trying to solve one involving a row of $$$n$$$ dominoes, each placed vertically. Initially, the positions are numbered from $$$1$$$ to $$$n$$$ from left to right.

The domino initially at position $$$i$$$ has number $$$a_i$$$ on its upper half and number $$$b_i$$$ on its lower half. Across all $$$2n$$$ halves, each integer from $$$1$$$ to $$$2n$$$ appears exactly once.

Each domino also has a color, represented by an integer. The domino initially at position $$$i$$$ has color $$$c_i$$$. Manuel may perform either of the following operations any number of times:

  • Choose a domino and swap the numbers on its upper and lower halves.
  • Choose two dominoes of the same color and swap their positions in the row.

His goal is to arrange the dominoes so that, when read from left to right, both the upper numbers and the lower numbers form strictly increasing sequences.

Before performing any operations, Manuel may repaint some dominoes. Each repainted domino may be assigned any integer color from $$$1$$$ to $$$n$$$.

Determine the minimum number of dominoes he must repaint to solve the puzzle. If it is impossible regardless of which dominoes he repaints and which colors he chooses, print $$$-1$$$.

Input

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

Each test case consists of four lines.

The first contains an integer $$$n$$$ ($$$1 \le n \le 10^{6}$$$), the number of dominoes.

The second contains $$$n$$$ integers $$$a_1,\ldots,a_n$$$ ($$$1 \le a_i \le 2n$$$), the initial upper numbers.

The third contains $$$n$$$ integers $$$b_1,\ldots,b_n$$$ ($$$1 \le b_i \le 2n$$$), the initial lower numbers.

Together, $$$a$$$ and $$$b$$$ form a permutation of the integers from $$$1$$$ to $$$2n$$$.

The fourth contains $$$n$$$ integers $$$c_1,\ldots,c_n$$$ ($$$1 \le c_i \le n$$$), the initial colors.

The sum of $$$n$$$ over all test cases does not exceed $$$10^{6}$$$.

Output

For each test case, print the minimum number of dominoes Manuel must repaint on a separate line, or $$$-1$$$ if the goal is impossible.

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

Explanation for example 1

In the first test case, Manuel can repaint dominoes $$$2$$$ and $$$4$$$ with color $$$1$$$. All dominoes then have the same color, so any two may be swapped. After swapping and flipping some dominoes, he can obtain $$$1,2,3,4,5$$$ on the upper halves and $$$6,7,8,9,10$$$ on the lower halves. The minimum cost is $$$2$$$.

In the second test case, it is impossible to make both rows increasing, even if all dominoes could be freely rearranged. The answer is therefore $$$-1$$$.

L. Gula Mineira
time limit per test
10 seconds
memory limit per test
1024 megabytes
input
standard input
output
standard output

Today is an important day for Matheus "Mathias" Prates: he is taking his final exam in AL2 (Cool Algorithms 2). He arrived early at the Federal University of Minas Gerais (UFMG) to study in the science library, but got so caught up in the review exercises that he is now late for lunch at his favorite campus restaurant, Gula Mineira.

Gula Mineira serves a self-service buffet with $$$n$$$ dishes arranged on a circular table. The table is represented by a circular list: position $$$i$$$ holds the dish identified by the positive integer $$$p_i$$$. Each dish appears at exactly one position.

Matheus has written down a sequence $$$s$$$ of the dishes he would like to take, in the order he wants to add them to his plate. Unfortunately, the restaurant is crowded, and everyone must move around the table in the same direction. If he is at position $$$i$$$ at time $$$t$$$, he may move to the next position ($$$i+1$$$, or $$$1$$$ if $$$i=n$$$), arriving at time $$$t+1$$$, or leave the queue. Leaving and rejoining the queue takes $$$k$$$ seconds and allows him to rejoin at any position. Taking a serving of a dish, even repeatedly, takes no time.

Initially, Matheus is outside the queue and may join at any position for free. Every subsequent departure and reentry costs $$$k$$$ seconds.

He wants to take as many dishes from his wish list as possible, in the listed order, within $$$w$$$ seconds. More formally, he wants to take a longest possible subsequence of $$$s$$$ within this time limit. Dishes skipped in the subsequence need not be visited. Help him finish lunch in time for his exam!

Input

The first line contains four integers $$$n$$$, $$$m$$$, $$$w$$$, and $$$k$$$ ($$$1 \le n \le 2 \times 10^{5}$$$, $$$1 \le m \le 5000$$$, $$$0 \le w,k \le 5000$$$): the number of dishes on the table, the length of the desired sequence, the available time in seconds, and the time needed to leave and rejoin the queue.

The second line contains $$$n$$$ distinct integers $$$p_1,\ldots,p_n$$$ ($$$1 \le p_i \le n$$$), where $$$p_i$$$ identifies the dish at position $$$i$$$.

The third line contains $$$m$$$ integers $$$s_1,\ldots,s_m$$$ ($$$1 \le s_i \le n$$$), the desired sequence of dishes.

Output

Print a single integer: the maximum length of a subsequence of $$$s$$$ that Matheus can take within $$$w$$$ seconds.

Examples
Input
4 3 10 1
2 1 4 3
1 2 3
Output
3
Input
5 5 5 2
1 2 3 4 5
1 3 2 5 4
Output
4
Input
3 4 0 1000
1 2 3
2 1 2 3
Output
2
Note

Explanation for example 2

Here, $$$n=5$$$, and dish numbers coincide with table positions (dish $$$i$$$ is at position $$$i$$$), with $$$k=2$$$ and $$$w=5$$$. The diagram below shows the table, with arrows indicating the permitted direction of movement.

The desired sequence is $$$s=(1,3,2,5,4)$$$. Matheus can take $$$4$$$ dishes within $$$w=5$$$ seconds. It can be shown that no longer subsequence is possible.

Explanation for example 3

Since $$$w=0$$$ and $$$k=1000$$$, Matheus cannot move along the queue or leave it. However, he may join directly at dish $$$2$$$ and take it twice, using no time. This gives the subsequence $$$(2,2)$$$, formed by the first and third elements of $$$s$$$: skipped elements need not be visited. Since no other dish appears more than twice, the answer is $$$2$$$.

M. Vassals
time limit per test
2 seconds
memory limit per test
1024 megabytes
input
standard input
output
standard output

After conquering many cities, the king of Nlogonia has decided to reward his vassals for their loyalty and success in battle. The conquered territory consists of $$$n$$$ cities connected by bidirectional roads. He wants to divide it among $$$v$$$ vassals so that each vassal controls at least one city and each city is controlled by exactly one vassal.

However, the vassals are quick-tempered and will seize any excuse to quarrel. To keep the peace, the king imposes a rule: each city may have at most one road leading to a city controlled by a different vassal.

Fortunately, the cities form a tree: there is exactly one path between any pair of cities, and there are no road cycles.

The king is busy with his royal duties and has asked you, the finest programmer in Nlogonia, to determine whether such a division is possible. If it is, also provide a valid assignment of cities.

Input

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

The first line of each test case contains two integers $$$n$$$ ($$$2 \le n \le 3 \times 10^{5}$$$) and $$$v$$$ ($$$1 \le v \le n$$$), the numbers of cities and vassals.

Each of the next $$$n-1$$$ lines contains two distinct integers $$$a$$$ and $$$b$$$ ($$$1 \leq a,b \leq n$$$), indicating a bidirectional road between cities $$$a$$$ and $$$b$$$. The roads are guaranteed to form a tree.

The sum of $$$n$$$ over all test cases does not exceed $$$500000$$$.

Output

Answer the test cases in input order.

If no valid division exists, print NAO (Portuguese for "no").

Otherwise, print SIM ("yes") on the first line. On the second line, print $$$n$$$ integers $$$c_1,c_2,\ldots,c_n$$$, where $$$c_i$$$ ($$$1 \leq c_i \leq v$$$) is the vassal controlling city $$$i$$$. Every vassal must control at least one city. If several valid assignments exist, print any of them.

Example
Input
2
8 3
1 2
2 3
3 4
4 5
5 6
6 7
7 8
12 3
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10
1 11
1 12
Output
SIM
1 2 2 3 3 3 3 3
NAO
N. Think The Box Outside
time limit per test
1 second
memory limit per test
1024 megabytes
input
standard input
output
standard output

Luis Higino has just returned from a long trip to Campinas, Brazil. During his stay, he visited the Institute of Computing at the University of Campinas (Unicamp) and was struck by this wall:

He liked it so much that he decided to recreate the artwork at home! He hired Cláudio to print four stickers:

  1. A box (an unfilled rectangle).
  2. The word THINK.
  3. The words THE BOX, one above the other.
  4. The word OUTSIDE.

Treat the box's outline as having negligible thickness. Represent each text sticker by its bounding box, the smallest rectangle containing the text. The dimensions are shown below.

The box has height $$$a_1$$$ and width $$$l_1$$$; THINK has height $$$a_2$$$ and width $$$l_2$$$; THE BOX has height $$$a_3$$$ and width $$$l_3$$$; and OUTSIDE has height $$$a_4$$$ and width $$$l_4$$$. The dashed lines show the text bounding boxes.

Luis's wall has height $$$a_p$$$ and width $$$l_p$$$. The arrangement must satisfy these conditions:

  • The bounding boxes of THINK and THE BOX must fit entirely inside the box, without overlapping interiors.
  • The rectangle enclosed by the box and the bounding box of OUTSIDE must fit entirely on the wall, without overlapping interiors.
  • No element may be rotated.

Boundaries may touch. Subject to these rules, the stickers may be placed anywhere. Given the dimensions of the four stickers and the wall, determine whether Luis can recreate the artwork.

Input

The input consists of five lines, each containing two integers representing a height and a width:

  • $$$a_1,l_1$$$: the box;
  • $$$a_2,l_2$$$: the THINK sticker;
  • $$$a_3,l_3$$$: the THE BOX sticker;
  • $$$a_4,l_4$$$: the OUTSIDE sticker;
  • $$$a_p,l_p$$$: the wall.
Every dimension is between $$$1$$$ and $$$1000$$$, inclusive.
Output

Print SIM (Portuguese for "yes") if the artwork fits on Luis's wall, or NAO ("no") otherwise.

Examples
Input
10 10
4 5
4 4
2 6
13 10
Output
SIM
Input
5 5
3 4
3 3
2 6
8 6
Output
NAO
Input
6 5
3 5
3 4
2 5
9 5
Output
SIM
Note

Explanation for example 1

In the first example, the THINK and THE BOX stickers can be placed side by side inside the box, with the box above the OUTSIDE sticker on the wall. Everything fits without overlapping interiors, so the answer is SIM.