The 21st Hunan Provincial Collegiate Programming Contest
A. Customized Shortest Path
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

The English statement was translated by AI and may be slightly different from the original Chinese statement. Please refer to the Chinese statement if you have any questions.

Given an undirected graph with $$$n$$$ vertices and $$$m$$$ edges. The $$$i$$$-th edge connects vertex $$$u_i$$$ and vertex $$$v_i$$$ with traversal cost $$$w_i$$$.

Now, you may increase the traversal cost of all edges simultaneously by $$$k$$$, where $$$k$$$ is any non-negative real number. Question: how many distinct $$$^{\dagger}$$$ paths from vertex $$$1$$$ to vertex $$$n$$$ exist such that there is at least one value of $$$k$$$ for which that path is one of the shortest paths from $$$1$$$ to $$$n$$$?

$$$^{\dagger}$$$ Two paths are considered different if and only if they have different numbers of edges, or there exists an index $$$i$$$ such that the $$$i$$$-th edge on the two paths has a different edge index.

Input

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

The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq n \leq 5000$$$, $$$1 \leq m \leq 5000$$$), denoting the number of vertices and edges in the graph.

The next $$$m$$$ lines each contain three integers $$$u_i$$$, $$$v_i$$$ and $$$w_i$$$ ($$$1 \leq u_i, v_i \leq n$$$, $$$1 \leq w_i \leq 10^9$$$), describing an undirected edge between vertices $$$u_i$$$ and $$$v_i$$$ with traversal cost $$$w_i$$$.

For each test file, it is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$5000$$$, and the sum of $$$m$$$ over all test cases does not exceed $$$5000$$$.

Output

For each test case, output a single integer — the number of paths that can become shortest paths from vertex $$$1$$$ to vertex $$$n$$$. Since the answer may be large, output it modulo $$$998\,244\,353$$$.

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

B. Cut ellipse
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given an ellipse on a 2D plane defined by the standard equation:

$$$$$$ \frac{x^2}{a^2} + \frac{y^2}{b^2} = 1 $$$$$$

where $$$a$$$ and $$$b$$$ are positive real numbers representing the semi-major and semi-minor axes.

You are also given a line in the plane defined by the linear equation:

$$$$$$ y = kx + c $$$$$$

Your task is to compute the area of the larger part when the line divides the ellipse into two regions.

Input

There is only one test case in each test file.

The first line contains two integers $$$a$$$ and $$$b$$$ ($$$1 \leq a, b \leq 10^3$$$), the semi-axes of the ellipse.

The second line contains two integers $$$k$$$ and $$$c$$$ ($$$|k|, |c| \leq 10^3$$$), defining the line $$$y = kx + c$$$.

It is guaranteed that the line always intersects the ellipse at two different points.

Output

Output a single floating-point number representing the area of the larger part of the ellipse.

Your answer is considered correct if its absolute or relative error does not exceed $$$10^{-6}$$$.

Formally, let your answer be $$$a$$$, and the jury's answer be $$$b$$$. Your answer is accepted if and only if $$$\frac{|a - b|}{\max{(1, |b|)}} \leq 10^{-6}$$$.

Example
Input
2 3
1 1
Output
12.709803500

C. Expansion on Tree
time limit per test
5 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

The English statement was translated by AI and may be slightly different from the original Chinese statement. Please refer to the Chinese statement if you have any questions.

Given a tree with $$$n$$$ nodes, there are $$$q$$$ queries.

In the $$$i$$$-th query, a sequence $$$a=(a_1,a_2,\dots,a_k)$$$ without duplicate elements is given. Initially:

  • Node $$$a_j$$$ is labeled with $$$j$$$;
  • Other nodes are labeled with $$$0$$$ (this label does not expand).

Then labels keep propagating as follows:

  • Propagation proceeds in the order $$$1,2,\dots,k,1,2,\dots$$$ in an infinite cycle;
  • When label $$$x$$$ propagates: if a node has a neighbor labeled $$$x$$$, then that node is immediately overwritten to $$$x$$$; existing labels can be overwritten;
  • Propagation is sequential, i.e., only the current $$$x$$$ is processed at a time, the result takes effect immediately, and then the next $$$x+1$$$ is processed.

We are interested in, for each label $$$j$$$, the maximum number $$$t_j$$$ of nodes labeled $$$j$$$ at any time during the whole process (including the initial state and after any propagation step).

Input

There is only one test case in each test file.

The first line contains two positive integers $$$n$$$ and $$$q$$$ ($$$1 \leq n, q \leq 5 \times 10^5$$$), denoting the number of nodes in the tree and the number of queries.

The next $$$n−1$$$ lines each contain two positive integers $$$x,y$$$ ($$$1 \leq x,y \leq n$$$, $$$x \neq y$$$), representing an edge of the tree.

The next $$$q$$$ lines each contain a query. For the $$$i$$$-th query, the first integer $$$k_i$$$ denotes that there are $$$k_i$$$ labels in this query ($$$1 \leq k_i \leq n$$$, $$$\sum{k_i} \leq 10^6$$$). Then follow $$$k_i$$$ positive integers $$$a_1, a_2, \dots, a_{k_i}$$$ ($$$1 \leq a_i \leq n$$$, the $$$a_i$$$ in a single query are pairwise distinct), representing the initial positions of the $$$k_i$$$ labels.

Output

Output $$$q$$$ lines. The $$$i$$$-th line contains $$$k_i$$$ integers, representing for each label the maximum number of nodes it ever labels during the whole process.

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

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

Little-L has a box divided into an $$$n \times m$$$ grid of cells.

Initially, all cells are empty. You can perform several operations, each of which can be one of the following two types:

  1. Choose a cell and place a small ball in every empty cell in the same row and the same column as the chosen cell.
  2. Choose a cell and place a small ball in every empty cell on the two diagonals passing through the chosen cell.

For example, in a $$$5 \times 5$$$ box, choosing cell $$$(3,4)$$$ for either the first or the second operation would yield the following results:

The left figure shows the first operation, and the right figure shows the second operation.

Little-L wants to know: what is the minimum number of operations needed to fill all cells in the box with balls?

Note: You can perform an operation on a cell that already contains a ball.

Input

Each test file contains multiple test cases. The first line contains the number of test cases $$$T$$$ ($$$1 \leq T \leq 10^4$$$). The description of the test cases follows.

The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n, m \leq 10^3$$$), representing the number of rows and columns of the box.

In each test file, it is guaranteed that the sum of $$$n \times m$$$ across all test cases does not exceed $$$10^6$$$.

Output

For each test case:

The first line should contain a single integer $$$p$$$ ($$$1 \leq p \leq n \times m$$$), representing the minimum number of operations.

The next $$$p$$$ lines each contain three integers $$$op, x, y$$$ ($$$op \in \{1,2\}, 1 \leq x \leq n, 1 \leq y \leq m$$$), indicating one operation. If $$$op=1$$$, perform the first type of operation at cell $$$(x,y)$$$; if $$$op=2$$$, perform the second type of operation at cell $$$(x,y)$$$.

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

E. Matrix Construction
time limit per test
1 second
memory limit per test
1024 megabytes
input
standard input
output
standard output

The English statement was translated by AI and may be slightly different from the original Chinese statement. Please refer to the Chinese statement if you have any questions.

Today, Alice wants to fill a permutation of length $$$n \times m$$$$$$^{\dagger}$$$ into an $$$n \times m$$$ matrix $$$A$$$, such that the sum of numbers in any contiguous submatrix with both height and width at least $$$2$$$ is a composite number.

In other words, for any $$$1 \leq a \lt c \leq n, 1 \leq b \lt d \leq m$$$, Alice wants $$$\sum_{i=a}^{c} \sum_{j=b}^{d} A_{i,j}$$$ to be a composite number.

For example, when $$$n=2$$$ and $$$m=3$$$, matrix $$$B$$$ is a valid construction, while matrix $$$C$$$ is not, because $$$C_{1,2}+C_{1,3}+C_{2,2}+C_{2,3}=17$$$.

$$$$$$ B = \begin{bmatrix} 1 & 3 & 2 \\ 6 & 5 & 4 \end{bmatrix}, \quad C = \begin{bmatrix} 1 & 6 & 2 \\ 3 & 5 & 4 \end{bmatrix} $$$$$$

Given $$$n$$$ and $$$m$$$, can the above requirement be satisfied? If yes, output one valid construction.

$$$^{\dagger}$$$ A permutation of length $$$n$$$ is an array containing the integers from $$$1$$$ to $$$n$$$ exactly once in any order. For example, $$$[2,3,1,5,4]$$$ is a permutation, while $$$[1,2,2]$$$ is not (since $$$2$$$ appears twice), and $$$[1,3,4]$$$ is not (since $$$n=3$$$ but the array contains $$$4$$$).

Input

There is only one test case in each test file.

The first line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq n, m \leq 50$$$).

Output

For the test case, on the first line output whether a valid construction exists. If it exists, output "Yes"; otherwise, output "No".

If a valid construction exists, you should also output $$$n$$$ lines, each containing $$$m$$$ integers. The $$$j$$$-th number on the $$$i$$$-th line denotes the number at row $$$i$$$, column $$$j$$$ of the matrix. You must ensure that the multiset of all output numbers forms a permutation of length $$$n \times m$$$. If there are multiple valid constructions, you may output any of them.

Examples
Input
2 3
Output
Yes
1 3 2
6 5 4
Input
3 3
Output
Yes
5 4 3
6 9 8
7 2 1

F. Mod
time limit per test
4 seconds
memory limit per test
1024 megabytes
input
standard input
output
standard output

To train your ability to calculate huge numerical values, $$$Irelia$$$ requires you to perform $$$n$$$ operations. Let $$$a_i$$$ denote the result produced by the $$$i$$$-th operation.

Each operation is one of the following four forms:

  • = v: assign $$$a_i = v$$$.
  • + j k: assign $$$a_i = a_j + a_k$$$.
  • * j k: assign $$$a_i = a_j \times a_k$$$.
  • ^ j k: assign $$$a_i = a_j^{a_k}$$$.

After each operation, output the value of $$$a_i$$$ modulo $$$m$$$.

Please note that the modulo operation is only applied to the answer, and the actual value of $$$a_i$$$ may be huge.

Input

There is only one test case in each test file.

The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n \leq 201307$$$, $$$2 \leq m \leq 10^9$$$), representing the number of operations and the modulus.

In the next $$$n$$$ lines, each contains an operation. For the $$$i$$$-th line, it starts with a character $$$op$$$ ($$$op \in \{\text{=,+,*,^}\}$$$). If $$$op$$$ is "=", it is followed by an integer $$$v$$$ ($$$1 \leq v \leq 10^9$$$). Otherwise, $$$op$$$ is followed by two integers $$$j$$$ and $$$k$$$ ($$$1 \leq j, k \lt i$$$).

Output

After each operation, output the value of $$$a_i$$$ modulo $$$m$$$.

Examples
Input
4 201307
= 1
+ 1 1
* 2 2
^ 3 3
Output
1
2
4
256
Input
6 8
= 5
+ 1 1
^ 2 1
^ 2 2
= 8
= 9
Output
5
2
0
0
0
1

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

The English statement was translated by AI and may be slightly different from the original Chinese statement. Please refer to the Chinese statement if you have any questions.

Given a road of length $$$l$$$ kilometers. The road is divided into $$$n$$$ segments, each with its own speed limit. The $$$i$$$-th segment starts at distance $$$a_{i-1}$$$ kilometers from the road's start, ends at distance $$$a_i$$$ kilometers from the road's start, and has a speed limit of $$$v_i$$$ kilometers per hour.

Bob drove through the entire road. There are $$$m$$$ cameras on the road; the $$$i$$$-th camera is located at position $$$x_i$$$ kilometers from the road's start, and recorded Bob passing it at time $$$t_i$$$ hours after Bob departed from the start. Bob finally took $$$t_{end}$$$ hours to reach the end of the road.

Cameras themselves do not measure speed, but by combining Bob's passing times at multiple cameras, the traffic police may deduce that Bob must have sped on some segments. Suppose there are two cameras at positions $$$x_a$$$ and $$$x_b$$$. If driving from $$$x_a$$$ to $$$x_b$$$ while always traveling at the maximum allowed speed takes $$$t_0$$$ hours, and $$$t_b - t_a \lt t_0$$$, then Bob must have sped on some part of the road between them. (Ignore time spent accelerating or decelerating.)

Now Bob wants to know the minimum number of camera records that must be removed so that the police cannot deduce that Bob definitely sped on any segment. Note: the records at the road's start and end cannot be removed; the police always know when Bob entered and left the road.

Input

Each test file contains multiple test cases. The first line contains the number of test cases $$$T$$$ ($$$1 \leq T \leq 10^4$$$). The description of the test cases follows.

The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$l$$$ and $$$t_{end}$$$ ($$$1 \leq n, m \leq 2 \times 10^5$$$, $$$2 \leq l \leq 10^9$$$, $$$2 \leq t_{end} \leq 10^9$$$), denoting the number of speed-limit segments, the number of cameras, the total length of the road, and the total time Bob took to pass the road, respectively.

The second line contains $$$n-1$$$ integers $$$a_1, a_2, \dots, a_{n-1}$$$ ($$$0 \lt a_i \lt l$$$), representing the boundaries of the segments. In particular, $$$a_0=0$$$ and $$$a_n=l$$$ always hold. When $$$n=1$$$, this line is empty.

The third line contains $$$n$$$ integers $$$v_1, v_2, \dots, v_n$$$ ($$$v_i \in \{5, 10, 15, 20, 25, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120\}$$$), representing the speed limit of each segment.

The next $$$m$$$ lines each contain two integers $$$x_i$$$ and $$$t_i$$$ ($$$0 \lt x_i \lt l$$$, $$$0 \lt t_i \lt t_{end}$$$), representing the position of the $$$i$$$-th camera and the time Bob passed it. It is guaranteed that $$$x_1 \lt x_2 \lt \dots \lt x_m$$$ and $$$t_1 \lt t_2 \lt \dots \lt t_m$$$. For each test file, it is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \times 10^5$$$, and the sum of $$$m$$$ over all test cases does not exceed $$$2 \times 10^5$$$.

Output

For each test case, output a single integer on one line — the minimum number of camera records that must be removed so that the police cannot deduce that Bob definitely sped. If the police will definitely find that Bob sped regardless, output "-1".

Examples
Input
4
1 1 60 2

30
20 1
1 1 60 2

30
30 1
1 1 60 2

30
40 1
1 1 120 2

30
60 1
Output
1
0
1
-1
Input
3
5 5 949 953
359 506 602 861
110 110 10 40 20
38 80
327 245
790 333
861 455
921 915
5 5 615 8
113 193 206 350
60 100 80 25 60
3 3
285 4
318 5
348 6
402 7
5 5 824 179
51 354 656 674
60 15 90 20 5
479 4
482 21
573 22
679 78
730 97
Output
0
-1
3
Note

Speeding is illegal, dangerous, and subject to legal penalties. This problem is a contest programming exercise and describes a fictional scenario; do not attempt to evade traffic surveillance in real life. Also do not attempt to tamper with or delete camera records, as that may constitute serious criminal offenses such as damaging computer information systems. Please program responsibly and prioritize safety.

H. Prime Segments
time limit per test
3 seconds
memory limit per test
1024 megabytes
input
standard input
output
standard output

Link has a sequence of length $$$n$$$ consisting of positive integers $$$a$$$.

Link wants to know how many subarrays of this sequence have a sum that is a prime number. In other words, how many pairs $$$(l,r)$$$ ($$$1 \leq l \leq r \leq n$$$) satisfy $$$\sum_{i=l}^{r} a_i$$$ being a prime number.

Input

Each test file contains multiple test cases. The first line contains the number of test cases $$$T$$$ ($$$1 \leq T \leq 10^4$$$). The description of the test cases follows.

The first line contains an integer $$$n$$$ ($$$1 \leq n \leq 10^6$$$), the length of the sequence.

The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \leq a_i \leq 10^6$$$, $$$\sum a_i \leq 10^6$$$), representing the elements of the sequence.

It is guaranteed that the sum of all $$$n$$$ across the test cases in a single file does not exceed $$$10^6$$$, and the sum of all $$$\sum a_i$$$ across the test cases in a single file does not exceed $$$10^6$$$.

Output

For each test case, output a single line containing one integer — the number of subarrays whose sum is a prime number.

Example
Input
2
4
1 2 3 4
12
1 1 4 5 1 4 1 9 1 9 8 10
Output
5
16
Note

In the first sample test case, the valid $$$(l,r)$$$ pairs are:

  • $$$l=1, r=2$$$: the sum of this subarray is $$$3$$$.
  • $$$l=2, r=2$$$: the sum of this subarray is $$$2$$$.
  • $$$l=2, r=3$$$: the sum of this subarray is $$$5$$$.
  • $$$l=3, r=3$$$: the sum of this subarray is $$$3$$$.
  • $$$l=3, r=4$$$: the sum of this subarray is $$$7$$$.

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

The English statement was translated by AI and may be slightly different from the original Chinese statement. Please refer to the Chinese statement if you have any questions.

Alice has a sheet of paper; some region on the paper contains important information, but the paper was torn into two pieces by Bob. Assuming the tear is random, Alice wants to know the probability that the important information is completely preserved in one of the two halves.

Formally:

There is an $$$n\times m$$$ sheet of paper on the plane, and the coordinate system partitions it into unit square grid cells. There is a "critical region" on the paper — a closed rectangle with vertices $$$(a,b)$$$, $$$(a,d)$$$, $$$(c,d)$$$, $$$(c,b)$$$, where

$$$$$$ 0 \leq a \lt c \leq n,\quad 0 \leq b \lt d \leq m. $$$$$$

Now Bob will tear the paper along a "legal path" from $$$(0,0)$$$ to $$$(n,m)$$$, and this path is chosen uniformly at random among all legal paths. A legal path means a lattice path from $$$(0,0)$$$ to $$$(n,m)$$$ where each step is either to the right (increase $$$x$$$ by $$$1$$$) or up (increase $$$y$$$ by $$$1$$$). Such a path necessarily divides the whole sheet into a "upper-left" part and a "lower-right" part. For convenience, a path that leaves one part empty is also considered legal.

We care whether the critical region is cut by this tearing path. If the entire closed rectangle of the critical region lies exactly on one side of the path (either entirely in the "upper-left" part or entirely in the "lower-right" part), we say the "critical region remains intact"; otherwise, it is considered cut by the tearing path.

We ask: among all possible legal paths, what is the probability that the "critical region remains intact"? Give the result modulo $$$998244353$$$ $$$^{\dagger}$$$.

$$$^{\dagger}$$$ Formally, let $$$M = 998\,244\,353$$$. It can be shown the answer can be expressed as a reduced fraction $$$\frac{p}{q}$$$ with integers $$$p,q$$$ and $$$q \not\equiv 0\pmod{M}$$$. Output the integer $$$p\cdot q^{-1}\bmod M$$$. In other words, output an integer $$$x$$$ with $$$0\le x \lt M$$$ such that $$$x\cdot q \equiv p \pmod{M}$$$.

Input

Each test file contains multiple test cases. The first line contains the number of test cases $$$T$$$ ($$$1 \leq T \leq 10^4$$$). The description of the test cases follows.

The first line of each test case contains six integers $$$n, m, a, b, c, d$$$ ($$$1 \leq n \leq 10^3, 1 \leq m \leq 10^6$$$, $$$0 \leq a \lt c \leq n$$$, $$$0 \leq b \lt d \leq m$$$), with meanings as described above.

Output

For each test case, output one integer on a separate line — the value of the probability that the "critical region remains intact" modulo $$$998244353$$$.

Example
Input
3
3 3 1 1 2 2
3 3 0 0 3 3
3 3 0 1 3 2
Output
1
299473306
199648871
Note

In the first sample test case, the critical region has size $$$1 \times 1$$$. It can never be split by any tearing path, so the probability that it remains intact is $$$1$$$.

In the second sample test case, the critical region has size $$$3 \times 3$$$, i.e. it is the entire sheet. Since the tearing path always divides the sheet into two nonempty parts unless one part is empty, the probability that the critical region remains intact is $$$\frac{2}{20}$$$, i.e. $$$\frac{1}{10}$$$.

In the third sample test case, the probability that the critical region remains intact is $$$\frac{8}{20}$$$, i.e. $$$\frac{2}{5}$$$.

J. Triangles and Squares
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

The English statement was translated by AI and may be slightly different from the original Chinese statement. Please refer to the Chinese statement if you have any questions.

Little L broke into a mysterious underground palace.

On the tightly closed palace door, Little L saw an inscription telling him that to open the door he needs to construct several convex polyhedra whose surfaces are tiled only by regular triangles and squares of side length $$$1$$$, and that a total of $$$x$$$ regular triangles and $$$y$$$ squares must be used.

Over time the power of the door has weakened. Now you only need to answer whether there exists at least one construction scheme such that the constructed several convex polyhedra satisfy the above conditions.

Note:

  • A polyhedron is called convex if for any two points inside the polyhedron the segment joining them lies entirely inside the polyhedron.
  • Each face of a convex polyhedron may be composed of several squares and regular triangles.
Input

Each test file contains multiple test cases. The first line contains the number of test cases $$$T$$$ ($$$1 \leq T \leq 10^4$$$). The description of the test cases follows.

The first line of each test case contains two integers $$$x$$$ and $$$y$$$ ($$$0 \leq x, y \leq 10^{18}$$$), indicating the number of regular triangles and squares to be used.

Output

For each test case, output "Yes" if there exists at least one valid construction scheme for the given $$$x, y$$$, otherwise output "No".

Example
Input
5
4 6
4 10
2 3
0 0
0 5
Output
Yes
Yes
Yes
Yes
No

K. Character Walk
time limit per test
1 second
memory limit per test
512 megabytes
input
standard input
output
standard output

You are given a string $$$s = s_1 s_2 \dots s_n$$$ consisting of lowercase English letters.

For $$$1 \leq i \leq j \leq n$$$, let $$$s_{i,j}$$$ be the substring of $$$s$$$ from position $$$i$$$ to $$$j$$$ inclusive.

Consider the integer points on the number line: $$$0, 1, \dots, n$$$. From a position $$$x$$$, you may move to another position $$$y$$$ if and only if the substring $$$s_{\,\min(x,y)+1,\; \max(x,y)}$$$ is a palindrome.

You are given $$$q$$$ queries. In the $$$i$$$-th query, you start at position $$$a_i$$$. For each query, determine the minimum number of moves required to reach either position $$$0$$$ or position $$$n$$$.

Input

There is only one test case in each test file.

The first line contains the string $$$s$$$ ($$$1 \leq n \leq 10^6$$$, where $$$n$$$ is the length of $$$s$$$).

The second line contains an integer $$$q$$$ ($$$1 \leq q \leq 10^6$$$).

The third line contains $$$q$$$ integers $$$a_1, a_2, \dots, a_q$$$ ($$$0 \leq a_i \leq n$$$), the starting positions.

Output

Print $$$q$$$ integers in a single line, where the $$$i$$$-th integer is the answer to the $$$i$$$-th query.

Examples
Input
abcdce
2
2 3
Output
2 3
Input
daabaddabaaaxy
2
2 9
Output
2 3
Input
aabaaaqwertyuiopsdfghjklzxcvnm
2
4 6
Output
2 2
Note

In the third example, one possible way to move to position $$$0$$$ (for both queries) is as follows.

  • In the first move, we move to position $$$5$$$ from either position $$$4$$$ or $$$6$$$.
  • Then, we move to position $$$0$$$ from position $$$5$$$.