[Unofficial Mirror] 2025 ICPC Shaanxi National Invitational Contest
A. Vote
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

The first major village, Zhangjia Village, has reached its once-in-a-decade election for village chief.

This year, there are two candidates for village chief: Wang Xiaochang and Liu Xiaopeng.

Each villager has their own preference level for the two candidates, represented by an integer $$$a$$$. Specifically:

  • $$$a \gt 0$$$ means the villager will vote for Xiaochang.
  • $$$a \lt 0$$$ means the villager will vote for Xiaopeng.
  • $$$a = 0$$$ means the villager may vote for either candidate.

To secure the election, both candidates can use their "money power" to sway votes. This ability can be used infinitely, and each use costs 1 yuan to increase or decrease someone's preference level by 1.

Xiao Ming, as an observer, happily recorded the highlights of the election in the $$$Q$$$ minutes leading up to it.

Specifically, from $$$Q$$$ minutes before the election to 1 minute before the election, one of the following three events occurred every minute:

  1. '1 x y': A group of $$$x$$$ villagers with a preference level of $$$y$$$ arrives at the polling station.
  2. '2 k': Wang Xiaochang repeatedly uses his money power on the villager whose preference level is closest to 0 and not currently voting for him until at least $$$k$$$ villagers are confirmed to vote for him. If at the start of the minute, there are already at least $$$k$$$ villagers voting for him, nothing is done.
  3. '3 k': Liu Xiaopeng repeatedly uses his money power on the villager whose preference level is closest to 0 and not currently voting for him until at least $$$k$$$ villagers are confirmed to vote for him. If at the start of the minute, there are already at least $$$k$$$ villagers voting for him, nothing is done.

Now, Xiao Ming has returned home and is organizing his records. Please help Xiao Ming calculate how much money each candidate spent in each minute for records of type '2' or '3'.

Input

The first line of input contains a positive integer $$$Q$$$, representing the number of records.

The next $$$Q$$$ lines each begin with an integer $$$opt$$$ ($$$1 \le opt \le 3$$$) indicating the record type.

  • For $$$opt = 1$$$, it is followed by $$$x$$$ and $$$y$$$.
  • For $$$opt = 2$$$ or $$$3$$$, it is followed by $$$k$$$.

$$$1 \le Q \le 2 \times 10^5$$$

$$$1 \le x \le 10^5$$$

$$$-10^8 \le y \le 10^8$$$

$$$k \ge 1$$$

It is guaranteed that for any record with $$$opt = 2$$$ or $$$3$$$, there are at least $$$k$$$ voters who arrived before that record.

Output

For each record with $$$opt = 2$$$ or $$$opt = 3$$$, output a single integer on a new line representing the amount of money spent.

Example
Input
6
1 10 10
1 10 -10
1 10 0
2 30
3 30
3 20
Output
120
150
0

B. LCM Gifts
time limit per test
1.5 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

Tomorrow is Xiao Peng's birthday, so Xiao Chang wants to give some gifts to Xiao Peng.

Xiao Chang buys $$$n$$$ gifts from the Internet and arranges them in a row from left to right. Each gift has a positive integer eigenvalue, where the $$$i$$$th gift from left to right has eigenvalue $$$a_i$$$. Since it is a bit scary to give all these gifts in one go, he decides to choose an interval in which to give these gifts to his little friend.

However, Xiao Chang, who is extremely sensitive to duplicates, acutely realizes that there are intervals $$$[l,r]$$$ that are with duplicates! Specifically, if Xiao Chang can select at least two distinct non-empty subsequences (not necessarily consecutive) from the interval $$$[l,r]$$$ such that the Least Common Multiple of every number in these two subsequences is equal to the Least Common Multiple of every number in the interval $$$[l,r]$$$. Formally, for the subscripted full set $$$U = [l,r] \cap \mathbb{Z}$$$, if two non-empty sets $$$S,T$$$ can be found that satisfy $$$S \ne T$$$, $$$S \subseteq U$$$, $$$T \subseteq U$$$, $$$\operatorname{lcm}_{x \in S} a_x = \operatorname{lcm}_{x \in T} a_x = \operatorname{lcm}_{x \in U} a_x$$$, then the interval $$$[l,r]$$$ is said to be with duplicates.

Since it's one day before Xiao Peng's birthday, now, a bored Xiao Chang wants to know how many intervals $$$[l,r]$$$ are with duplicates.

Input

The first line contains one positive integer $$$n$$$, indicating the number of the gifts. It is guaranteed that $$$1 \le n \le 2 \times 10^5$$$.

The second line contains $$$n$$$ positive integers, the $$$i$$$th number $$$a_i$$$ indicating the eigenvalue of the $$$i$$$th gift. It is guaranteed that $$$1 \le a_i \le 10^6$$$.

Output

A non-negative integer in a single line indicating the number of the intervals with duplicates.

Example
Input
4
2 3 4 6
Output
3
Note

Explanation to Sample 1

The intervals of with duplicates are $$$[1,3]$$$, $$$[1,4]$$$, $$$[2,4]$$$ respectively. Take the interval $$$[1,4]$$$ as an example, choose the subscript set $$$S = \{2,3\}$$$, $$$T = \{1,3,4\}$$$, i.e., the subsequences $$$[3,4]$$$ and $$$[2,4,6]$$$ satisfy the condition.

C. Electronic
time limit per test
4 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

Electrician Master Xiao Ming is repairing an electrical box.

There are $$$n$$$ components arranged in a row in the electrical box, numbered from left to right as $$$1,2,3,\ldots,n$$$. There are also $$$m$$$ wires connecting pairs of components. The $$$i$$$-th wire ($$$1 \le i \le m$$$) connects components numbered $$$x_i$$$ and $$$y_i$$$ ($$$1 \le x_i \lt y_i \le n$$$), and any two components are directly connected by at most one wire, i.e., $$$\forall 1 \le i \lt j \le m$$$, $$$x_i \ne x_j$$$ or $$$y_i \ne y_j$$$. Current can flow through the wires in any direction. We refer to a wire connecting components $$$a$$$ and $$$b$$$ as an unordered pair $$$(a,b)$$$, meaning $$$(a,b)$$$ and $$$(b,a)$$$ represent the same wire.

To facilitate maintenance, the electrical box must satisfy the requirement:

no two wires intersect, i.e., $$$\forall 1 \le i \lt j \lt k \lt l \le n$$$, wires $$$(i,k)$$$ and $$$(j,l)$$$ cannot coexist.

Xiao Ming has a detection tool with four probes. When these probes are connected to four distinct components $$$i,j,k,l$$$ in sequence, if the wires $$$(i,j)$$$, $$$(j,k)$$$, and $$$(k,l)$$$ all exist, the tool can detect whether this circuit segment is functioning properly.

Xiao Ming is in a hurry to leave work, so he wants to know how many such circuit segments need to be tested. Specifically, he needs to count the number of ordered quadruples $$$(i,j,k,l)$$$ satisfying:

  1. $$$i,j,k,l$$$ are pairwise distinct.
  2. Wires $$$(i,j)$$$, $$$(j,k)$$$, and $$$(k,l)$$$ all exist.

During the repair process, Xiao Ming will sequentially add $$$q$$$ wires. The $$$i$$$-th wire ($$$1 \le i \le q$$$) connects components $$$u_i$$$ and $$$v_i$$$ ($$$1 \le u_i \lt v_i \le n$$$). As a qualified electrician, after adding each wire, the electrical box must still satisfy all the aforementioned requirements. After each addition, Xiao Ming must perform a full inspection. Therefore, he needs to determine the number of valid ordered quadruples in the initial electrical box and after each wire addition.

Input

The first line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \le n \le 5 \times 10^5$$$, $$$0 \le m \le 1 \times 10^6$$$), representing the number of components and the initial number of wires.

The next $$$m$$$ lines each contain two integers $$$x_i$$$ and $$$y_i$$$ ($$$1 \le x_i \lt y_i \le n$$$), describing the $$$i$$$-th wire.

The following line contains an integer $$$q$$$ ($$$0 \le q \le 1 \times 10^6$$$), the number of queries.

The next $$$q$$$ lines each contain two integers $$$u_i$$$ and $$$v_i$$$ ($$$1 \le u_i \lt v_i \le n$$$), describing the $$$i$$$-th wire added.

Output

Output $$$q + 1$$$ lines. The first line indicates the number of valid ordered quadruples in the initial electrical box. The $$$(i + 1)$$$-th line ($$$1 \le i \le q$$$) indicates the count after adding the first $$$i$$$ wires.

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

Initially, there are two valid ordered quadruples $$$(1,2,3,4)$$$ and $$$(4,3,2,1)$$$.

After the first addition, two new quadruples $$$(1,2,4,3)$$$ and $$$(3,4,2,1)$$$ are added, totaling 4.

After the second addition, there are 12 valid ordered quadruples.

Due to the large input/output data size, please use fast I/O methods.

D. Boxes
time limit per test
2 seconds
memory limit per test
1024 megabytes
input
standard input
output
standard output

Xiaopeng is a warehouse keeper. One day, he was idle and decided to arrange the boxes he manages.

There are now $$$n$$$ closely adjacent shelves, where the height of the $$$i$$$-th shelf from left to right is $$$a_i$$$. Specifically, $$$a_0 = a_{n+1} = +\infty$$$. There are $$$m$$$ boxes of size 1 to be placed on these shelves. Xiaopeng considers a placement scheme "good" if it satisfies the following conditions:

We model the warehouse as a two-dimensional Cartesian coordinate system. If a box is placed at coordinate $$$(x,y)$$$, then:

  • $$$a_x \lt y$$$.
  • $$$(x,y-1)$$$ either satisfies $$$a_x \ge y-1$$$ or has a box placed at $$$(x,y-1)$$$.
  • $$$(x-1,y)$$$ either satisfies $$$a_{x-1} \ge y$$$ or has a box placed at $$$(x-1,y)$$$.
  • $$$(x+1,y)$$$ either satisfies $$$a_{x+1} \ge y$$$ or has a box placed at $$$(x+1,y)$$$.

Xiaopeng wants to determine the total number of good placement schemes. The answer should be taken modulo $$$10^9+7$$$.

Input

The first line contains two positive integers $$$n,m$$$ ($$$1 \le n \le 10^3$$$, $$$1 \le m \le 10^4$$$), representing the number of shelves and boxes.

The second line contains $$$n$$$ non-negative integers, where the $$$i$$$-th number denotes the height $$$a_i$$$ ($$$0 \le a_i \le 10^5$$$) of each shelf.

Output

Output a single integer, representing the number of good schemes.

Example
Input
4 6
5 1 6 1
Output
5

E. Clever Game
time limit per test
2 s
memory limit per test
1024 MB
input
standard input
output
standard output

Xiao Chang and Xiao Peng want to play a game.

The game is played on a directed acyclic graph (DAG) with $$$2^n$$$ nodes (labeled $$$0$$$ to $$$2^n-1$$$) and $$$m$$$ edges. There are $$$k$$$ pieces placed on the graph, with the $$$i$$$-th piece initially located at node $$$a_i$$$. The two players take turns, with Xiao Chang moving first. On each turn, a player can freely choose any piece and move it one step along any outgoing edge from its current node. If a player cannot make any move on their turn (i.e., all pieces are on nodes with no outgoing edges), that player loses the game. Note: Multiple pieces can occupy the same node.

Naturally, Xiao Chang and Xiao Peng are perfectly rational: once the initial positions of the pieces are set, they both already know who will win or lose the game. So, they decide to manipulate the initial positions $$$a_i$$$! They observe that each $$$a_i$$$ can be uniquely represented as an $$$n$$$-bit binary number. If they write down the binary representations of all $$$k$$$ pieces row by row, they will form a $$$k \times n$$$ binary matrix, where the value at the $$$i$$$-th row and $$$j$$$-th column indicates whether the $$$j$$$-th bit (from least to most significant, with the least significant bit being the 1st bit) of $$$a_i$$$ is 0 or 1.

They assign a position number $$$(i-1) \times n+j$$$ to each cell $$$(i,j)$$$ in the matrix. Starting from position 1, they take turns deciding whether each cell should be 0 or 1, with Xiao Chang going first, followed by Xiao Peng, until the entire matrix is filled (i.e., Xiao Chang decides the first position, Xiao Peng the second, Xiao Chang the third, and so on). In this way, they "scheme against each other" to determine all $$$a_i$$$.

Afterward, Xiao Chang makes the first move in the DAG-based game described earlier. However, the game is still too simple for them, so they want to ask you: Under these rules, who will be the ultimate winner?

Input

The first line contains three positive integers $$$n,m,k$$$. It is guaranteed that $$$1 \le n \le 14$$$, $$$1 \le m \le 8 \times 10^4$$$, and $$$1 \le k \le 20$$$.

The following $$$m$$$ lines each contain two positive integers $$$u$$$ and $$$v$$$, representing a directed edge in the graph. The input graph is guaranteed to be a directed acyclic graph (DAG).

Output

Output a single string: "First" if Xiao Chang wins in the end, or "Second" if Xiao Peng wins.

Examples
Input
2 2 2
1 0
3 2
Output
First
Input
2 2 2
3 1
2 0
Output
Second
Input
3 6 3
7 3
6 2
5 1
4 0
7 5
6 4
Output
First

F. Drink
time limit per test
1 second
memory limit per test
512 megabytes
input
standard input
output
standard output

Some unexpected incidents occurred during the village chief election. Due to Xiaochang and Xiaopeng violating election rules and being disqualified, Xiao Ming was ultimately elected as the new village chief. As a result, Xiao Ming decided to treat everyone to a meal together.

In Zhangjia Village, the local customs are quite bold; meals must involve drinking wine and playing a game called "Drink to the Finish".

To describe this game specifically: $$$n$$$ people sit in a circle, and the $$$i$$$-th person holds a die that can roll an integer between $$$[1,a_i]$$$ with equal probability.

The game proceeds in rounds. At the start of each round:

(1) All people who have not yet become "wine dazed" roll their dice once.

(2) Now, each person checks the numbers rolled by their left and right neighbors. Assuming their own rolled number is $$$x$$$, and their left and right neighbors' numbers are $$$a$$$ and $$$b$$$ respectively, then in this round they need to drink $$$\max(0,a-x)+\max(0,b-x)$$$ grams of wine.

(3) Those who roll $$$x=1$$$ in this round will become "wine dazed" and can no longer roll their dice. Their number is fixed at 1 thereafter.

(4) The game ends when everyone has become "wine dazed".

Xiao Ming is worried that everyone might drink too much, but also wants to know whether his wine tolerance can last until the game ends. So he asks you to calculate: In this game, how many grams of wine is each person expected to drink?

Input

The first line contains an integer $$$n$$$, representing the number of participants in the game.

The second line contains $$$n$$$ positive integers $$$a_1,\ldots,a_n$$$, listed clockwise to represent the parameters of each participant's die.

$$$3 \le n \le 10^5$$$

$$$1 \le a_i \le 10^5$$$

Output

Output one line containing $$$n$$$ integers, representing the expected amount of wine each person $$$1,2,\ldots,n$$$ will drink respectively.

It can be proved that each answer can be expressed as a fraction $$$\frac{x}{y}$$$ ($$$\gcd(x,y)=1$$$). You only need to output $$$x \times y^{-1} \bmod 998244353$$$, where $$$y^{-1}$$$ is the unique integer satisfying $$$0 \lt y^{-1} \lt 998244353$$$ and $$$y \times y^{-1} \bmod 998244353 = 1$$$.

Example
Input
3
1 1 2
Output
1 1 0
Note

Clearly, the 3rd person won't drink any wine, while the 1st and 2nd persons drink equivalent amounts. All possible scenarios are:

$$$\frac{1}{2}$$$: $$$[1]$$$ – The game ends immediately (the left fraction represents probability).

$$$\frac{1}{4}$$$: $$$[2,1]$$$ – The 1st and 2nd persons need to drink $$$2-1=1$$$ unit of wine.

$$$\frac{1}{8}$$$: $$$[2,2,1]$$$ – The 1st and 2nd persons need to drink $$$2 \times (2-1)=2$$$ units of wine.

$$$\frac{1}{16}$$$: $$$[2,2,2,1]$$$ – The 1st and 2nd persons need to drink $$$3 \times (2-1)=3$$$ units of wine.

...

The answer can be easily calculated as $$$1 \times \frac{1}{4}+2 \times \frac{1}{8}+3 \times \frac{1}{16}+\ldots=1$$$.

G. Juan
time limit per test
1 second
memory limit per test
512 megabytes
input
standard input
output
standard output

Xiaochang and Xiaopeng are preparing for the postgraduate entrance exam together, sitting back-to-back in the study room day after day.

Originally, Xiaochang was better at studying than Xiaopeng, so he doesn't want Xiaopeng to surpass him. Xiaopeng is very hardworking himself, but he doesn't want Xiaochang to see him working too hard.

Specifically:

  • Xiaochang has a sequence $$$A$$$ of length $$$n$$$, where the $$$i$$$-th position ($$$0 \le i \lt n$$$) $$$A_i$$$ is either 1 or 0, representing whether he looks back to check if Xiaopeng is working hard (1) or not (0).
  • Xiaopeng has a sequence $$$B$$$ of length $$$m$$$, where the $$$j$$$-th position ($$$0 \le j \lt m$$$) $$$B_j$$$ is either 1 or 0, representing whether he is working hard (1) or playing with his phone (0) at that moment.

Over the next $$$n \times m$$$ days, at 11:00 am on the $$$k$$$-th day ($$$0 \le k \lt n \times m$$$):

  • If $$$A_{k \bmod n}=1$$$, Xiaochang will look back at Xiaopeng; otherwise, he won't.
  • If $$$B_{k \bmod m}=1$$$, Xiaopeng will be working hard at that moment; otherwise, he will be playing with his phone.

The trust level of Xiaochang towards Xiaopeng increases by 1 in the following two cases:

  1. Xiaochang looks back and sees Xiaopeng playing.
  2. Xiaochang doesn't look back, but Xiaopeng is working hard.

Given $$$n$$$, $$$m$$$, and Xiaochang's sequences, the question is: Assuming Xiaopeng constructs his sequence in the optimal way, what is the maximum trust level Xiaochang can have towards Xiaopeng after $$$n \times m$$$ days?

Input

The first line contains two positive integers $$$n$$$ and $$$m$$$, as described in the problem statement.

The second line contains a binary string of length $$$n$$$, representing Xiaochang's sequence $$$A$$$.

$$$1 \le n,m \le 10^5$$$

Output

Output a single number representing the answer.

Examples
Input
3 4
111
Output
12
Input
3 2
101
Output
4
Note

For the first sample case, Xiaopeng can set his sequence to 0000, so whenever Xiaochang looks back, Xiaopeng is playing, resulting in the answer $$$n \times m=3 \times 4=12$$$.

For the second sample case, during these 6 days, Xiaopeng's observation sequence is 101101, while Xiaochang's sequence expands to $$$B_0B_1B_0B_1B_0B_1$$$.

Each day's decisions correspond to: $$$\{1,B_0\}$$$, $$$\{0,B_1\}$$$, $$$\{1,B_0\}$$$, $$$\{1,B_1\}$$$, $$$\{0,B_0\}$$$, $$$\{1,B_1\}$$$.

It's easy to see that setting $$$B_0=B_1=0$$$ yields the maximum answer of 4.

H. Subsequense
time limit per test
6 seconds
memory limit per test
1024 megabytes
input
standard input
output
standard output

Xiao Ming has a string $$$S$$$ of length $$$N$$$, where each character belongs to the set $$$\Sigma=\{a,b,c,d,e\}$$$.

The hash value of a string $$$T$$$ of length $$$M$$$ is defined as $$$\sum_{i=1}^{M}(T_i-\text{'a'}+1) \times 1331^{M-i} \pmod{2^{32}}$$$. Here, the subtraction is performed by converting the characters to their corresponding ASCII codes.

Now, Xiao Ming has $$$Q$$$ queries. Each query consists of three positive integers $$$l,r,k$$$. You need to consider all distinct subsequences within the interval $$$[l,r]$$$ that start with the character $$$S_l$$$, end with the character $$$S_r$$$, and have a length of at least 2. These subsequences should be sorted lexicographically from smallest to largest, and you should output the hash value of the $$$k$$$-th subsequence in this order. Specifically, if the number of distinct subsequences in the interval $$$[l,r]$$$ is less than $$$k$$$, output '-1'.

A subsequence within the interval $$$[l,r]$$$ is obtained by deleting some characters from that interval. Two subsequences with lengths $$$a$$$ and $$$b$$$, respectively, are considered distinct if and only if $$$a \ne b$$$, or there exists an integer $$$i$$$ such that $$$1 \le i \le a$$$ and the $$$i$$$-th characters of the two subsequences differ.

Input

The first line of input contains two positive integers $$$N,Q$$$, representing the length of the string and the number of queries.

The second line contains a string $$$S$$$ of length $$$N$$$.

The next $$$Q$$$ lines each contain three positive integers $$$l,r,k$$$.

$$$1 \le N \le 10^5$$$, $$$1 \le Q \le 10^5$$$, $$$1 \le l \le r \le N$$$, $$$1 \le k \le 10^9$$$, $$$\forall 1 \le i \le N$$$, $$$S_i \in \{a,b,c,d,e\}$$$.

Output

For each query, output a number on a separate line, representing the answer.

Example
Input
2 3
aa
1 2 1
1 2 2
1 1 1
Output
1332
-1
-1

I. NEWS
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Xiao Ming is obsessed with a strange game that takes place on a 2D coordinate plane. He controls a small airplane starting at the origin point $$$(0,0)$$$.

As a top pilot, he receives a flight command sequence, which is a permutation $$$P$$$ of the four letters 'NEWS'.

Xiao Ming will perform $$$n$$$ flights in total. For his $$$i$$$-th flight:

If $$$1 \le i \le 4$$$, the flight command is the $$$i$$$-th letter in $$$P$$$.

If $$$i \gt 4$$$, the flight command is the same as the $$$(i-4)$$$-th flight.

The meaning of each command is as follows:

'N': If the current position is $$$(x,y)$$$, move to $$$(x,y+1)$$$. Repeat this until reaching a new unvisited point.

'E': If the current position is $$$(x,y)$$$, move to $$$(x+1,y)$$$. Repeat this until reaching a new unvisited point.

'W': If the current position is $$$(x,y)$$$, move to $$$(x-1,y)$$$. Repeat this until reaching a new unvisited point.

'S': If the current position is $$$(x,y)$$$, move to $$$(x,y-1)$$$. Repeat this until reaching a new unvisited point.

After completing all $$$n$$$ flights, calculate the sum of the two coordinates of his final position.

Input

The first line contains a positive integer $$$T$$$ — the number of test cases.

Each of the next $$$T$$$ lines contains a test case: a positive integer $$$n$$$ and a 4-letter string $$$P$$$, separated by a space.

$$$1 \le T \le 10^5$$$, $$$1 \le n \le 10^9$$$, $$$P$$$ has exactly 4 letters, with each of 'N', 'E', 'W', 'S' appearing exactly once.

Output

For each test case, output one line with the sum of the two coordinates of the final position.

Example
Input
6
9 NEWS
9 NESW
9 NSEW
1000000000 SEWN
1000000000 SWNE
1000000000 SNWE
Output
1
-1
-3
-499999999
250000000
500000000
Note

When $$$n=9$$$ and $$$P=$$$ 'NEWS', the positions after each flight are (starting from $$$(0,0)$$$): $$$(0,0)$$$, $$$(0,1)$$$, $$$(1,1)$$$, $$$(-1,1)$$$, $$$(-1,0)$$$, $$$(-1,2)$$$, $$$(0,2)$$$, $$$(-2,2)$$$, $$$(-2,1)$$$, $$$(-2,3)$$$.

For the 'W' command at $$$(1,1)$$$, the plane first tries to move to $$$(0,1)$$$. But $$$(0,1)$$$ is visited, so he continues moving to $$$(-1,1)$$$.

J. Message
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

There are $$$n$$$ children sitting in a circle playing a message-passing game. The children are numbered from $$$0$$$ to $$$n-1$$$, and each of them is facing the center of the circle.

Each child holds a card. The card of the $$$i$$$-th child has a number $$$a_i$$$ on it, where $$$a_i \in \{2,3\}$$$.

If the $$$i$$$-th child receives the message, they will pass it to child number $$$(i+a_i) \bmod n$$$.

The teacher wants to inform one child such that eventually every child receives the message. Your task is to help the teacher rearrange the positions of the children and choose one child to initially receive the message. Determine whether it is possible to make sure that all children will receive the message.

Input

The first line contains a positive integer $$$T$$$ ($$$1 \le T \le 10^5$$$), representing the number of test cases.

For each test case, the first line contains a positive integer $$$n$$$ ($$$1 \le n \le 10^5$$$), representing the number of children.

The next line contains $$$n$$$ positive integers $$$a_i$$$, each being either 2 or 3, representing the number on each child's card.

It is guaranteed that $$$\sum n \le 10^6$$$.

Output

For each test case, output a line containing "Yes" or "No" indicating whether it is possible to rearrange the children such that after telling the message to one of them, every child will receive the message.

Example
Input
3
3
2 3 2
4
2 2 2 2
6
3 2 3 2 3 3
Output
Yes
No
Yes
Note

For the first test case, no rearrangement is needed; simply choosing to tell the message to the first child achieves the goal.

For the second test case, it can be proven that the goal cannot be achieved.

For the third test case, by rearranging the sequence to $$$3\ 3\ 3\ 2\ 2\ 3$$$, and telling the message to the child numbered 1, the goal can be achieved.

K. Chess Game
time limit per test
2 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

Xiao Ming is a little girl who likes eating cucumbers.

She enjoys playing chess against herself, acting as both the black and white sides simultaneously.

The game is played on an undirected graph $$$G$$$ with $$$n$$$ nodes, containing $$$m$$$ undirected edges. The $$$i$$$-th edge connects nodes $$$u_i$$$ and $$$v_i$$$ with a length of $$$l_i$$$. It is guaranteed that the graph contains no self-loops but may have multiple edges between the same pair of nodes.

In each move, Xiao Ming can choose either side and place a chess piece of the corresponding color on any unoccupied node. After $$$n$$$ rounds, the game ends.

Black and white represent yin and yang, and Xiao Ming wishes to maintain their balance. Therefore, she wants to ensure that no two chess pieces of the same color are placed too close to each other on the graph. Specifically, we consider a simple path $$$p_1,p_2\ldots p_k$$$ that satisfies:

  1. $$$k \ge 2$$$
  2. $$$p_1,p_2\ldots p_k$$$ are all distinct.
  3. Nodes $$$p_1$$$ and $$$p_k$$$ are occupied by chess pieces of the same color.

Xiao Ming defines the balance degree $$$d$$$ of the game as the minimum path length among all such paths that meet the above conditions. If no such path exists, the balance degree is defined as positive infinity.

Please help Xiao Ming plan the game to maximize the balance degree $$$d$$$.

Input

The first line contains two integers $$$n,m$$$ ($$$1 \le n \le 5 \times 10^5$$$, $$$0 \le m \le 5 \times 10^5$$$).

The next $$$m$$$ lines each contain three integers $$$u_i,v_i,l_i$$$ ($$$1 \le u_i,v_i \le n$$$, $$$1 \le l_i \le 10^8$$$, $$$u_i \ne v_i$$$).

Note that the input size for this problem is very large, so please use fast input methods.

Output

Output a single integer representing the maximum possible balance degree $$$d$$$ of the game. If the answer is infinity, output $$$-1$$$ instead.

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

One possible optimal arrangement is as follows:

Xiao Ming places black pieces on nodes $$$1,4,3,5$$$ and a white piece on node $$$2$$$. Then, one of the shortest valid simple paths is $$$1-5$$$ with a length of 4.

It can be shown that no better solution exists.

L. Clothes
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Xiao Ming is going hiking and needs to pack some clothes.

He has $$$n$$$ clothes, and the $$$i$$$-th one keeps him warm by $$$a_i$$$ points. He can choose to bring $$$1 \sim n$$$ clothes to meet the weather for the next $$$m$$$ days.

Each day has different rules:

If $$$t_i=1$$$, the total warmth of the clothes he wears that day must be at least $$$x_i$$$.

If $$$t_i=2$$$, the total warmth must not exceed $$$x_i$$$.

Each day, he can pick some of the clothes he packed (but must wear, so at least one).

What's the smallest number of clothes he needs to bring to meet all days' rules? If it's impossible, output $$$-1$$$.

Input

First line: $$$n$$$, number of clothes.

Second line: $$$a_1 \sim a_n$$$, warmth of each clothes.

Third line: $$$m$$$, number of days.

Next $$$m$$$ lines: $$$t_i$$$ and $$$x_i$$$, rules for each day.

$$$1 \le n \le 3$$$, $$$1 \le a_i \le 2 \times 10^5$$$, $$$1 \le m \le 100$$$, $$$1 \le t_i \le 2$$$, $$$1 \le x_i \le 10^9$$$.

Output

The smallest number of clothes he needs to bring , or $$$-1$$$ if impossible.

Examples
Input
3
2 3 5
1
1 10
Output
3
Input
3
2 3 5
2
1 5
2 2
Output
2
Input
3
7 11 13
2
1 24
2 7
Output
3

M. Kingdom
time limit per test
2 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

Xiao Chang and Xiao Ming live in a tree with $$$n$$$ nodes numbered from $$$1$$$ to $$$n$$$. There are a number of kingdoms in the tree.

If a node has more than two edges, then a kingdom appears at that node with the same number as the node (at least one such node is guaranteed to exist). The capital of the kingdom is this node. The kingdom expands its territory as much as possible on a "first come, first served" basis. In other words, an unoccupied node will be occupied by the kingdom whose capital is the closest to the node (if there is more than one, the one with the smallest number).

Xiao Chang and Xiao Ming are friends, so Xiao Chang will often visit Xiao Ming, and on the way, he may pass the territories of several kingdoms. Also, to save time, Xiao Chang will follow the shortest path to reach Xiao Ming's home. Now you need to answer $$$q$$$ queries, given $$$u$$$ and $$$v$$$, assuming that Xiao Chang lives in $$$u$$$ and Xiao Ming lives in $$$v$$$, how many kingdoms will Xiao Chang visit on his way to visit Xiao Ming?

Input

The first line contains a positive integer $$$n$$$, indicating the number of nodes in the tree.

The next $$$n-1$$$ lines, each containing two positive integers $$$x,y$$$, indicate the existence of an edge between node $$$x$$$ and node $$$y$$$.

The $$$n+1$$$th line contains a positive integer $$$q$$$, indicating the number of queries.

The next $$$q$$$ lines, each containing two positive integers $$$u,v$$$, describe a single query.

$$$4 \le n \le 2 \times 10^5$$$, $$$1 \le q \le 2 \times 10^5$$$, $$$1 \le x,y \le n$$$, $$$1 \le u,v \le n$$$.

Output

For each query, output a line containing an integer indicating the answer.

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