Rutgers University Programming Contest Fall 2025 - Open Division
A. Hinge Arch
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You have a toy consisting of $$$n$$$ sticks of varying lengths connected in order in a row by hinges. The sticks can be rotated arbitrarily around the hinges and are allowed to overlap.

You want to create an arch with these sticks. To do this, you will place the two endpoints of the structure anywhere on the surface of a table and position the rest of the sticks and hinges arbitrarily. You can ignore gravity and assume the sticks and hinges have zero width. Sticks are allowed to overlap and/or intersect with each other but may not intersect with the table. Note that the order of the sticks is fixed and cannot be changed. One possible arch is shown here:

You want to maximize the height of the arch, defined as the maximum distance from the surface of the table to a point on the arch (this is shown as the dotted red line above). What is the maximum height you can achieve?

Input

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

The first line of each test case contains a single integer $$$n$$$ ($$$2\le n \le 1000$$$) — the number of sticks in the structure.

The second line of each test case will contain $$$n$$$ integers $$$l_1, l_2, \cdots l_n$$$ ($$$1 \le l_i \le 1000$$$) — the lengths of each of the sticks, in order.

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

Output

For each test case, output one line containing a single number — the maximum possible height of the arch.

Your answer is considered correct if its absolute or relative error doesn't exceed $$$10^{-6}$$$. Namely, if your answer is $$$a$$$, and the judge's answer is $$$b$$$, then your answer is accepted if and only if $$$\frac{|a-b|}{\max(1,|b|)} \le 10^{-6}$$$.

Example
Input
5
5
1 2 4 2 3
3
1 4 1
2
4 4
6
1 6 13 4 100 2
3
1 2 3
Output
5.000
1.000
4.000
24.000
3.000
Note

The first test case uses the sticks from the picture above. The arch shown above is not optimal.

This is one possible maximal arch for the second test case:

This is one possible maximal arch for the third test case:

Note that the sticks are allowed to fully overlap.

B. Partition Addition
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given an integer $$$n$$$. In one operation, you can split the digits of $$$n$$$ into two nonempty parts and replace $$$n$$$ with their sum. For example, if $$$n=12526$$$, you could split it into $$$125$$$ and $$$26$$$ and set $$$n = 125 + 26 = 151$$$, or if $$$n=10001$$$, you could split it into $$$1$$$ and $$$0001$$$ and set $$$n=1 + 0001 =2$$$.

If you can perform this operation any number of times (including zero), what is the minimum $$$n$$$ you can achieve?

Input

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

Each test case consists of a single line containing an integer $$$n$$$ ($$$1\le n \le 10^9$$$) — the initial value of $$$n$$$.

Output

For each test case, print a single integer — the minimum possible value of $$$n$$$ after performing any number of operations.

Example
Input
5
12526
2
123456789
347347
1000000000
Output
7
2
9
1
1
Note

In the first test case, you can use this series of operations:

$$$$$$12526 \longrightarrow 125 + 26 = 151 \longrightarrow 1 + 51 = 52 \longrightarrow 5 + 2 = 7$$$$$$

In the second test case, since $$$n$$$ only has one digit, it is impossible to perform any operations.

C. Divisor Lattice
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

There is a hidden positive integer $$$k$$$ that has exactly $$$n$$$ divisors. These divisors were put into an array $$$a$$$ which was then shuffled into a hidden order.

You can ask the interactor questions of the form ? i j, and you will receive the position of $$$\gcd(a_i, a_j)$$$ in $$$a$$$. Note that since $$$a$$$ contains all divisors of $$$k$$$, this value is guaranteed to exist and be unique.

Using at most $$$3n$$$ queries, determine which indices of $$$a$$$ contain prime numbers.

Input

The input consists of one line containing an integer $$$n$$$ ($$$1\leq n\leq 10^4$$$) — the number of divisors of $$$k$$$.

Note that we make no guarantees about the magnitude of $$$k$$$.

Interaction

After reading in $$$n$$$, you can make up to $$$3n$$$ queries.

For each query, print a line of the form ? i j ($$$1 \le i, j \le n$$$).

After printing the query, you can then read in an integer $$$x$$$, where $$$a_x = \gcd(a_i, a_j)$$$.

Once you have determined which indices contain prime numbers, print a single line ! m p_1 p_2 ... p_m ($$$0 \le m \le n$$$, $$$1 \le p_i \le n$$$, where $$$m$$$ is the number of prime factors and $$$p_1$$$ through $$$p_m$$$ are their positions in $$$a$$$. This is not counted towards your total of $$$3n$$$ queries.

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

  • fflush(stdout) or cout.flush() in C++;
  • System.out.flush() in Java;
  • flush(output) in Pascal;
  • stdout.flush() in Python;
  • see the documentation for other languages.

The interactor is non-adaptive. This means that the array $$$a$$$ is fixed at the start of the interaction, and does not change after the queries.

Examples
Input
4

1
Output
? 2 4

! 2 2 4
Input
1
Output
! 0
Note

In the first test, after the first query, we know that $$$\gcd(a_2, a_4) = a_1$$$. It can be shown that since $$$n=4$$$, this query result implies that $$$a_2$$$ and $$$a_4$$$ are the prime factors of $$$k$$$.

In the second test $$$n=1$$$, and therefore $$$k=1$$$, because $$$1$$$ is the only positive integer with only one divisor. Since $$$1$$$ has no prime factors, we can simply output ! 0 without making any queries.

D. Thomas Trade
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You have a set $$$S$$$ containing the first $$$n$$$ powers of $$$2$$$. However, Thomas tampered with your set and negated some of its values. For example, for $$$n=4$$$, $$$S = \{1, 2, 4, 8\}$$$, and Thomas could negate the $$$2$$$ and $$$4$$$ to turn $$$S$$$ into $$$\{1, -2, -4, 8\}$$$.

You are given a positive integer $$$k$$$. Express $$$k$$$ as the sum of some subset of $$$S$$$, or state that this is impossible.

Input

The first line of the input contains a single integer $$$t$$$ ($$$1\le t\le 10^4$$$) — the number of test cases.

The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1\le n \le 30$$$, $$$1 \le k \lt 2^n$$$) — the size of $$$S$$$ and the target sum, respectively.

The next line consists of $$$n$$$ characters $$$+$$$ and $$$-$$$, where the $$$i$$$-th character is $$$-$$$ if Thomas negated $$$2^{i-1}$$$ and $$$+$$$ otherwise.

Output

For each test case, if there is no subset adding up to $$$k$$$, print $$$-1$$$.

Otherwise, print a string of $$$n$$$ characters $$$\texttt{#}$$$ and $$$\texttt{.}$$$, where the $$$i$$$-th character is $$$\texttt{#}$$$ if you include the $$$i$$$-th element in the subset, and $$$\texttt{.}$$$ otherwise.

If there are multiple solutions, you can print any.

Example
Input
6
4 2
+--+
3 5
---
6 43
++++++
7 9
--+--+-
1 1
-
5 4
--+--
Output
.###
-1
##.#.#
######.
-1
..#..
Note

In the first test case, $$$S = \{1, -2, -4, 8\}$$$ and $$$k=2$$$. We can choose the subset $$$\{-2, -4, 8\}$$$, which adds up to $$$2$$$.

In the second test case, $$$S = \{-1, -2, -4\}$$$. Since all elements of $$$S$$$ are negative, we cannot choose a subset of $$$S$$$ that adds up to $$$k=5$$$.

E. Connected Squares
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

There is an infinite grid of squares. The squares at positions $$$(0, 0)$$$ through $$$(n-1, 0)$$$ are then colored red, blue, or green. You want to color additional squares such that for each of the three colors, the subset of squares of that color is connected, where two squares are considered connected if they share an edge.

In other words, for every pair of squares of the same color $$$c$$$, it should be possible to travel from one to the other, moving only through squares of color $$$c$$$ that share an edge.

Find a solution that colors at most $$$10^6$$$ additional squares (not including the squares that are currently present), or determine that there is no solution. It can be shown that under the given constraints, if there is a solution, there is one using at most $$$10^6$$$ additional squares.

You do not need to minimize the number of additional squares.

Input

The first line of input contains a single integer $$$n$$$ ($$$1 \le n \le 100$$$) — the number of currently-colored squares.

The second and final line of input contains $$$n$$$ characters $$$\texttt{R}$$$, $$$\texttt{B}$$$, or $$$\texttt{G}$$$, where the $$$i$$$-th of these indicates the color of the square at position $$$(i-1, 0)$$$.

Output

If there is no solution, print a single integer $$$-1$$$.

Otherwise, the first line of output should contain a single integer $$$k$$$ ($$$0 \le k \le 10^6$$$) — the number of additional squares you will color.

The next $$$k$$$ lines should contain two integers $$$x$$$ and $$$y$$$, and a character $$$c$$$ ($$$-10^6 \le x, y \le 10^6$$$, $$$c \in \{\texttt{R}, \texttt{B}, \texttt{G}\}$$$) — indicating that you are adding a square with color $$$c$$$ at position $$$(x, y)$$$.

No two squares in the output should have the same coordinates, and no square in the output should have the same coordinates as any square in the input.

If there are multiple solutions, print any.

Examples
Input
7
RBGRBGR
Output
32
1 1 B
0 1 B
-1 1 B
-1 0 B
-1 -1 B
-1 -2 B
0 -2 B
1 -2 B
2 -2 B
3 -2 B
4 -2 B
4 -1 B
0 -1 R
1 -1 R
2 -1 R
3 -1 R
3 1 R
4 1 R
5 1 R
6 1 R
2 1 G
2 2 G
3 2 G
4 2 G
5 2 G
6 2 G
7 2 G
7 1 G
7 0 G
7 -1 G
6 -1 G
5 -1 G
Input
6
GBGBGB
Output
10
1 -1 B
2 -1 B
3 -1 B
4 -1 B
5 -1 B
0 1 G
1 1 G
2 1 G
3 1 G
4 1 G
Input
4
RRRB
Output
0
Note

The first test case corresponds to the image above in the statement.

Here is a visualization for the second test case:

Notice that the set of red squares is considered connected even though there are no red squares in the solution.

In the third test case, each color is already connected, so there is no need to color additional squares.

F. XOR Sorting
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

There is a permutation$$$^\dagger$$$ $$$p$$$ of size $$$n$$$ that you are trying to sort. To do so, you may perform up to $$$20$$$ operations of the following form:

  • Select a subset of distinct indices $$$i_1, i_2, \cdots i_k$$$ such that the value of $$$p_{i_1} \oplus p_{i_2} \oplus \cdots \oplus p_{i_k}$$$, where $$$\oplus$$$ represents the bitwise XOR operation, is zero.
  • Sort the values of $$$p$$$ on indices $$$i_1, i_2, \cdots i_k$$$.

For example, for $$$p = [7, 3, 4, 1, 2, 6, 5]$$$, you could choose the indices $$$[2, 4, 5]$$$, since $$$p_2 \oplus p_4 \oplus p_5 = 1 \oplus 2 \oplus 3 = 0$$$. After this operation, $$$p$$$ would be equal to $$$[7, 1, 4, 2, 3, 6, 5]$$$.

It can be shown that if it is possible to sort the permutation in any number of operations, then it is possible in at most $$$20$$$.

$$$^\dagger$$$ A permutation of length $$$n$$$ is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2, 3, 1, 5, 4]$$$ is a permutation, but $$$[1, 2, 2]$$$ is not a permutation ($$$2$$$ appears twice in the array), and $$$[1, 3, 4]$$$ is also not a permutation ($$$n=3$$$ but there is $$$4$$$ in the array).

Input

The first line of the input contains a single integer $$$t$$$ ($$$1\le t\le 2\cdot 10^5$$$) — the number of test cases.

The first line of each test case contains a single integer $$$n$$$ ($$$1\le n \le 2\cdot 10^5$$$) — the size of the permutation.

The second line of each test case will contain $$$n$$$ distinct integers $$$p_1, p_2, \cdots p_n$$$ ($$$1 \le p_i \le n$$$) — the elements of $$$p$$$.

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

Output

For each test case, if there is no solution, print $$$-1$$$.

Otherwise, the first line of output should contain a single integer $$$m$$$ ($$$0 \le m \le 20$$$) — the number of operations you will perform.

Each of the next $$$m$$$ lines should contain an integer $$$k$$$, followed by $$$k$$$ distinct integers $$$i_1, i_2, \cdots i_k$$$ ($$$1 \le i_j \le n$$$) — a description of the operations you will perform, in order. After these operations, the permutation should be sorted.

If there are multiple solutions, you can print any.

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

In the first test case, $$$p=[8, 6, 3, 4, 9, 5, 2, 7, 1]$$$. We then perform the following operations, where the red numbers indicate the positions that we choose in the operation:

$$$$$$[8, 6, 3, 4, 9, 5, 2, 7, 1] \rightarrow [\color{red}8, \color{red}6, 3, \color{red}4, \color{red}9, 5, \color{red}2, 7, \color{red}1] \rightarrow [\color{red}1, \color{red}2, 3, \color{red}4, \color{red}6, 5, \color{red}8, 7, \color{red}9]$$$$$$ $$$$$$[1, 2, 3, 4, 6, 5, 8, 7, 9] \rightarrow [1, 2, 3, \color{red}4, \color{red}6, 5, \color{red}8, \color{red}7, \color{red}9] \rightarrow [1, 2, 3, \color{red}4, \color{red}6, 5, \color{red}7, \color{red}8, \color{red}9]$$$$$$ $$$$$$[1, 2, 3, 4, 6, 5, 7, 8, 9] \rightarrow [1, 2, \color{red}3, 4, \color{red}6, \color{red}5, 7, 8, 9] \rightarrow [1, 2, \color{red}3, 4, \color{red}5, \color{red}6, 7, 8, 9]$$$$$$

So this process successfully sorts the permutation.

In the second test case, $$$p$$$ is already sorted, so there is no need to perform any operations.

We can show that the permutation in the third test case cannot be sorted, no matter how many operations we perform.

G. Subsequence MEX II
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You are a member of the Rutgers Programming Contest problemsetting team, and you have just come up with a new problem for the Spring 2025 contest. However, you realized you're not yet sure how to write the judge program for this problem. Your task now is to implement that judge.

Define a number $$$b$$$ to be a subsequence of a number $$$a$$$ if, when you write out $$$a$$$ in decimal notation, you can erase some (but not all) of its digits so that the remaining digits, in order, form $$$b$$$. For example, $$$356$$$ is a subsequence of $$$1234567$$$, because you can erase the $$$1$$$, $$$2$$$, $$$4$$$, and $$$7$$$ to form $$$356$$$. However, $$$0$$$ is not a subsequence of $$$23527$$$, because the digit $$$0$$$ is not present in $$$23527$$$.

The problem you have just come up with requires the user to construct an integer $$$n$$$ such that the $$$\operatorname{MEX}$$$ of its subsequences is the input number $$$x$$$. Therefore, the judge program should read an integer $$$n$$$ from the user and compute the $$$\operatorname{MEX}$$$ of its subsequences, so that value can then be compared against the original input $$$x$$$.

The $$$\operatorname{MEX}$$$ of a set of integers is defined as the smallest non-negative integer which does not occur in the set. For example, the $$$\operatorname{MEX}$$$ of $$$\{0, 1, 2, 4\}$$$ is $$$3$$$, and the $$$\operatorname{MEX}$$$ of $$$\{1, 4, 6, 8\}$$$ is $$$0$$$.

Input

The first line of the input contains a single integer $$$t$$$ ($$$1\le t\le 10^4$$$) — the number of test cases.

Each test case consists of a single line containing an integer $$$n$$$ ($$$1 \le n \lt 10^{2\cdot 10^5}$$$) — the number chosen by the user's program. It is guaranteed that $$$n$$$ does not contain leading zeroes.

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

Output

For each test case, print a single line containing one integer $$$x$$$ — the $$$\operatorname{MEX}$$$ of all subsequences of $$$n$$$.

Example
Input
6
123
70
12836880457
2468013579
12013456789
23609713987621002462058
Output
0
1
9
10
22
41
Note

In the first test case, $$$n=123$$$ does not contain $$$0$$$ as a subsequence, and therefore the $$$\operatorname{MEX}$$$ of its subsequences is $$$0$$$.

In the third test case, $$$n$$$ contains all digits except for $$$9$$$, making the $$$\operatorname{MEX}$$$ of its subsequences equal to $$$9$$$.

H. World Emperor
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given a directed graph. For each vertex $$$v$$$, you can remove up to half of the edges directed away from $$$v$$$. Formally, if there are $$$x$$$ edges directed away from $$$v$$$, you can remove up to $$$\lfloor \frac{x}{2} \rfloor$$$ of them. Remove edges in such a way that vertex $$$n$$$ is not reachable from vertex $$$1$$$ or state that this is impossible.

For example, in the below graph, one solution is to remove the three red edges. Note that no vertex has more than half of the edges directed away from it colored red.

Input

The first line of the input contains a single integer $$$t$$$ ($$$1\le t\le 10^4$$$) — the number of test cases.

The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1\le n \le 2\cdot 10^5, 0 \le m \le 2\cdot 10^5$$$) — the number of vertices and edges in the graph, respectively.

Each of the next $$$m$$$ lines contains two integers $$$u$$$ and $$$v$$$ ($$$1 \le u, v \le n, u \ne v$$$), indicating that there is a directed edge from $$$u$$$ to $$$v$$$. All $$$(u, v)$$$ pairs are guaranteed to be distinct, but it is possible for there to be both an edge $$$(u, v)$$$ and an edge $$$(v, u)$$$.

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

Output

For each test case, if there is no solution, print $$$-1$$$.

Otherwise, the first line of output should contain a single integer $$$k$$$ ($$$0 \le k \le m$$$) — the number of edges to remove.

Each of the next $$$k$$$ lines should contain two integers $$$u$$$ and $$$v$$$ ($$$1 \le u, v \le n$$$) — one of the edges $$$(u, v)$$$ that should be removed. $$$(u, v)$$$ must be present in the input graph, and no edge can be removed twice. Vertex $$$n$$$ should not be reachable from vertex $$$1$$$ after these removals.

If there are multiple solutions, print any.

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

The first test case corresponds to the image above. There are other solutions, such as choosing to only remove the edges $$$(1, 5)$$$ and $$$(4, 2)$$$.

In the second test case, the only valid solution is to remove only the edge $$$(2, 4)$$$.

This is the graph for the third test case. It can be shown there is no solution.

I. Grid Coloring
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

There is an $$$n$$$ by $$$n$$$ grid of white squares, each containing a bidirectional horizontal or vertical arrow. In one operation, you can choose any square, and based on the arrow in that square, color all squares in the corresponding row or column black. For example, here is one possible sequence of two operations on a grid with $$$n=4$$$:

What is the minimum number of operations needed to color all squares black, and which squares should you choose in the operations?

Input

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

The first line of each test case contains a single integer $$$n$$$ ($$$1\le n \le 1000$$$) — the number of rows and columns in the grid.

The $$$i$$$-th of the next $$$n$$$ lines contains $$$n$$$ characters each. The $$$j$$$-th of these is $$$\texttt{H}$$$ if the square in position $$$(i, j)$$$ contains a horizontal arrow, and $$$\texttt{V}$$$ if it contains a vertical arrow.

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

Output

The first line of output for each test case should contain a single integer $$$k$$$ ($$$0 \le k \le n^2$$$) — the number of operations you will perform.

Each of the next $$$k$$$ lines should contain two integers $$$i$$$ and $$$j$$$ ($$$1 \le i, j \le n$$$) — the row and column of the square you will choose for that operation, respectively.

If there are multiple solutions with minimal $$$k$$$, you can print any.

Example
Input
3
4
VHHV
VVHV
HHVH
VVHV
1
H
2
VH
VV
Output
4
2 3
4 3
1 2
3 2
1
1 1
2
1 1
2 2
Note

The first test case corresponds to the picture above. It can be shown that it is impossible to color everything black in less than $$$4$$$ operations.

In the second test case, since there is only one square, doing a single operation and choosing that square is sufficient to color everything black.

J. Lattice Triangles
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

A lattice triangle is a triangle in the plane whose vertices have integer coordinates. You are given a lattice triangle $$$S$$$. How many lattice triangles $$$T$$$ contain a vertex of $$$S$$$ on each of their sides, without sharing any vertices with $$$S$$$?

Input

Input consists of three lines, each containing two integers $$$x$$$ and $$$y$$$ ($$$0 \le x, y \le 100$$$) — the coordinates of one of the vertices of $$$S$$$. Vertices are given in counterclockwise order, and it is guaranteed that they are not collinear.

Output

Output a single integer — the number of valid lattice triangles $$$T$$$. If this number is infinite, print $$$-1$$$ instead.

Examples
Input
1 1
2 2
2 4
Output
5
Input
3 1
4 3
1 3
Output
29
Input
0 0
99 100
0 100
Output
28939
Note

Here are the five possible triangles for the first test case:

The triangle given in the second test case is the triangle from the image in the statement.

K. Chain of Suspicion
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

A suspicious string is a string of the form s$$$^n$$$u$$$^n$$$s$$$^n$$$ for $$$n \gt 0$$$. In other words, a suspicious string is made up of $$$n$$$ copies of the letter s, followed by $$$n$$$ copies of the letter u, and then another $$$n$$$ copies of the letter s. So sus and sssuuusss are examples of suspicious strings, but suuus and sssuus are not.

A chain of suspicion is a concatenation of zero or more suspicious strings. For example, the empty string, sus, and susssuusssus are chains of suspicion, and susus and sssusss are not.

Given a string $$$t$$$ consisting of the letters s and u, print the length of the longest (possibly empty) substring that is a chain of suspicion.

Input

The first line of the input contains a single integer $$$t$$$ ($$$1\le t\le 10^4$$$) — the number of test cases.

The first line of each test case contains a single integer $$$n$$$ ($$$1\le n \le 2\cdot 10^5$$$) — the length of $$$t$$$.

The second line of each test case contains $$$n$$$ characters, each of which is either s or u — the string $$$t$$$.

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

Output

For each test case, output one line containing a single integer — the length of the longest substring of $$$t$$$ that is a chain of suspicion.

Example
Input
4
2
us
3
sus
6
suussu
13
susususssuuss
Output
0
3
0
9
Note

In the first test case, the only substring of $$$t$$$ that is a chain of suspicion is the empty string, so the answer is $$$0$$$.

In the second test case, $$$t$$$ itself is a chain of suspicion, so the answer is $$$3$$$.

In the fourth test case, the longest substring of $$$t$$$ that is a chain of suspicion is susssuuss.

L. Not a Magic Square
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

The Rutgers scientific committee members don't like magic, and by extension, they don't like magic squares either. Thus, they like to construct grids of numbers that completely go against the principles of magic squares. Here is how they do it.

They start with an $$$n \times n$$$ grid of cells, and in each cell they put an integer from the range $$$[1, 4]$$$. The numbers are chosen such that the set formed by taking the $$$2n$$$ row and column sums consists of $$$2n$$$ distinct values (diagonal sums are not considered).

Given an integer $$$n$$$, your task is to construct an $$$n \times n$$$ grid according to the above rules. It can be proven that such a grid always exists.

Input

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

Each test case consists of a single line containing a single integer $$$n$$$ ($$$2\le n\le 1000$$$) — the side length of the grid.

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

Output

For each test case, print $$$n$$$ lines containing $$$n$$$ characters each. Every character should be one of 1, 2, 3, or 4. The values obtained by summing along any row or column in the output should all be pairwise distinct.

If there are multiple solutions, print any.

Example
Input
2
2
4
Output
12
33
1312
2323
2443
1211
Note

For the first test case, the row sums are $$$3$$$ and $$$6$$$, and the column sums are $$$4$$$ and $$$5$$$. All four of these numbers are distinct.

The second test case corresponds to the image above.

M. Cube Embedding
time limit per test
0.5 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Suppose we have a graph $$$G$$$ with vertex set $$$V$$$. A straight-line embedding of $$$G$$$ is a rendition of $$$G$$$ in 3D space such that the edges are straight line segments which do not intersect except possibly at their endpoints.

Formally, a function $$$f: V \to \mathbb{R}^3$$$ is called a straight-line embedding of $$$G$$$ if it satisfies the following conditions:

  • $$$f$$$ is injective; that is, $$$f(u) \neq f(v)$$$ when $$$u \neq v$$$.
  • For any pair of edges $$$(a, b)$$$ and $$$(c, d)$$$ in $$$G$$$, the line segment from $$$f(a)$$$ to $$$f(b)$$$ and the line segment from $$$f(c)$$$ to $$$f(d)$$$ do not intersect, except possibly by endpoints coinciding. In other words, no point of one segment may be in the interior of another segment.

Next, for $$$n \gt 0$$$, consider the cube of all points in $$$\mathbb{R}^3$$$ whose coordinates are in $$$[0, n+1]$$$. Denote this cube by $$$C_n$$$.

You are given a simple undirected graph $$$G$$$ (not necessarily connected) with vertices numbered $$$1$$$ through $$$n$$$ and the guarantee that for all vertices $$$v$$$, $$$v$$$ has degree at most $$$3$$$. Your task is to construct a straight-line embedding $$$f$$$ of $$$G$$$ such that for each $$$v$$$, the coordinates of $$$f(v)$$$ are all integers, and $$$f(v)$$$ lies on one of the 12 line segments comprising the edges of $$$C_n$$$.

It can be shown that, given the degree restriction on the vertices of $$$G$$$, such an embedding always exists.

Input

The first line of the input contains a single integer $$$t$$$ ($$$1\le t\le 10^4$$$) — the number of test cases.

The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1\le n \le 5000$$$, $$$0 \le m \le \frac{3}{2}n$$$) — the number of vertices and edges respectively in $$$G$$$.

Each of the next $$$m$$$ lines contains two integers $$$u$$$ and $$$v$$$ ($$$1 \le u, v \le n$$$) — the vertices of an edge in $$$G$$$. It is guaranteed that all vertices have degree at most $$$3$$$ and that $$$G$$$ does not contain duplicate edges or self-loops.

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

Output

Output $$$n$$$ lines, each consisting of three integers. The integers on the $$$i$$$-th line represent the $$$x$$$, $$$y$$$, and $$$z$$$ coordinates respectively of $$$f(i)$$$, the image of vertex $$$i$$$ under your embedding. $$$(x, y, z)$$$ should lie on one of the edges of the cube.

If there are multiple solutions, print any.

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

The first test case corresponds to the picture above.

Here is a visualization of the embedding for the second test case:

N. Solvable Problem
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given a zero-indexed array $$$a$$$ of size $$$2^k$$$. For each $$$x$$$ from $$$0$$$ through $$$2^k-1$$$, consider the array $$$b_x$$$ where $$$(b_x)_i = a_{i \oplus x}$$$, where $$$\oplus$$$ denotes the bitwise XOR operator. Note that because $$$a$$$ is of size $$$2^k$$$ and is zero-indexed, this array is well-defined.

Given an array $$$c$$$ of size $$$n$$$, let $$$\texttt{inv}(c)$$$ denote the number of inversions in $$$c$$$, in other words, the number of pairs $$$(i, j)$$$ where $$$0 \le i \lt j \lt n$$$ and $$$c_i \gt c_j$$$.

Your task is to compute $$$\sum_{x=0}^{2^k-1} \texttt{inv}(b_x)$$$.

Input

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

The first line of each test case contains a single integer $$$k$$$ ($$$1 \le k \le 18$$$), indicating that the length of the array $$$a$$$ is $$$2^k$$$.

The second line of each test case contains $$$2^k$$$ integers $$$a_0, a_1, \cdots a_{2^k-1}$$$ ($$$1 \le a_i \le 2^k$$$) — the contents of the array $$$a$$$.

It is guaranteed that the sum of $$$2^k$$$ over all test cases does not exceed $$$2^{18}$$$.

Output

For each test case, print a single integer — the total number of inversions across all $$$b_x$$$.

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

In the first test case, we have

  • $$$b_0 = [4, 4, 1, 4]$$$ with $$$2$$$ inversions $$$(0, 2)$$$ and $$$(1, 2)$$$
  • $$$b_1 = [4, 4, 4, 1]$$$ with $$$3$$$ inversions $$$(0, 3)$$$, $$$(1, 3)$$$, and $$$(2, 3)$$$
  • $$$b_2 = [1, 4, 4, 4]$$$ with no inversions
  • $$$b_3 = [4, 1, 4, 4]$$$ with one inversion $$$(0, 1)$$$

So the total number of inversions is $$$6$$$.

In the second test case, all elements of $$$a$$$ are equal, so none of the $$$b_i$$$ will have any inversions.

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

You are given an integer $$$n$$$. Construct any string $$$s$$$ that has exactly $$$n$$$ distinct nonempty substrings. It can be shown that such a string always exists.

A string $$$t$$$ is a substring of a string $$$s$$$ if $$$t$$$ can be obtained from $$$s$$$ by the deletion of several (possibly zero) characters from the beginning and several (possibly zero) characters from the end.

For example, the string $$$aba$$$ has $$$5$$$ distinct nonempty substrings — $$$a$$$, $$$b$$$, $$$ab$$$, $$$ba$$$, and $$$aba$$$.

Input

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

Each test case consists of a single line containing an integer $$$n$$$ ($$$1\le n \le 2\cdot 10^5$$$) — the desired number of distinct nonempty substrings.

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

Output

For each test case, output a single line containing a string $$$s$$$ — a string $$$s$$$ consisting of lowercase letters with exactly $$$n$$$ distinct nonempty substrings. You do not need to minimize the length of $$$s$$$.

If there are multiple solutions, print any.

Example
Input
4
5
1
15
54
Output
aba
h
vwxyz
abracadabra
Note

For the second test case, $$$h$$$ only has one distinct nonempty substring — $$$h$$$, so the answer is $$$1$$$.

For the third test case, all substrings of $$$s$$$ are distinct, so there are $$$15$$$ distinct substrings in total.