TheForces Round #22 (Interesting-Forces)
A. Interesting Subsequence
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

You're given an sequence $$$a$$$ of size $$$n$$$.

Determine if there is a sequence $$$b$$$ that satisfies all the following conditions:

  • $$$b$$$ is a subsequence of $$$a$$$;
  • There's an index $$$i(1\leq i \leq n)$$$,if we note $$$c=\underbrace{[[a_1,...a_{i-1},a_{i+1},...,a_n],[a_1,...a_{i-1},a_{i+1},...,a_n],...,[a_1,...a_{i-1},a_{i+1},...,a_n]]}_{n\cdot (n-1)elements}$$$,$$$b$$$ is not a subsequence of $$$c$$$.

A subsequence of a given sequence is a sequence that can be derived from the given sequence by deleting some or no elements without changing the order of the remaining elements. For example,$$$[1,2]$$$ and $$$[1,1]$$$ are subsequences of $$$[1,2,1]$$$ but $$$[2,2]$$$ and $$$[3]$$$ are not.

Input

Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^5$$$). The description of the test cases follows.

The first line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 2\cdot 10^5$$$).

The second line of each testcase contains $$$n$$$ integers $$$a_1,a_2,\ldots, a_n$$$ ($$$1 \le a_i \le 10^9$$$).

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

Output

If there is a sequence $$$b$$$ that satisfies all the following conditions output YES.Otherwise,output NO.

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

Test Case $$$1$$$:

$$$b=[1]$$$ is one of valid sequence.For index $$$i=1$$$,note $$$c=[a_2,a_2]=[2,2]$$$,then $$$b$$$ is not a subsequence of $$$c$$$.

Test Case $$$2$$$:

For index $$$i=1,2$$$,$$$c=[1,1]$$$ always holds.No matter which subsequence $$$b$$$ of $$$a$$$ you choose, $$$b$$$ must be a subsequence of $$$c$$$.

B. Interesting Connection
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

For two positive integers $$$x,y$$$,define $$$con(x,y)$$$ as the number obtained by connecting the decimal representation of $$$x$$$ with $$$y$$$, where $$$x$$$ is on the left and $$$y$$$ is on the right.For example $$$con(20,23)=2023,con(2,33)=233$$$.

You're given two integers $$$n,k$$$.You can choose an array $$$a$$$ of positive integers of size $$$n$$$,which satisfies $$$\Sigma_{i=1}^{n}a_i=k$$$.

Define the score as the number of prime numbers among all $$$con(a_i, a_j)(1\leq i,j\leq n)$$$.Find the minimum score you can get.

Input

Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^5$$$). The description of the test cases follows.

The only line of each test case contains two integers $$$n$$$, $$$k$$$ ($$$1 \le n \le k \le 10^9$$$).

Output

For each test case, output a single integer — the minimum score you can get.

Example
Input
6
2 3
2 5
3 3
3 7
3 17
1 1000000000
Output
1
1
9
2
0
0
Note

Test Case $$$1$$$:

One of the optimal array $$$a$$$ is $$$[1,2]$$$.

For this array,$$$con(a_1,a_1)=11,con(a_1,a_2)=12,con(a_2,a_1)=21,con(a_2,a_2)=22$$$.

There's only one prime.It can be proven that $$$1$$$ reaches the minimum.

Test Case $$$2$$$:

One of the optimal array $$$a$$$ is $$$[2,3]$$$.

For this array,$$$con(a_1,a_1)=22,con(a_1,a_2)=23,con(a_2,a_1)=32,con(a_2,a_2)=33$$$.

There's only one prime.It can be proven that $$$1$$$ reaches the minimum.

Test Case $$$3$$$:

The only array $$$a$$$ you can choose is $$$[1,1,1]$$$.

For this array,$$$con(a_i,a_j)=11$$$ for all $$$1 \le i,j \le n$$$.

There're $$$9$$$ primes.It can be proven that $$$9$$$ reaches the minimum.

C. Interesting Operation
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

You're given an string $$$s$$$ of size $$$n$$$ composed of lowercase letters.

Define $$$f(C)$$$ as the character obtained by shifting the character $$$C$$$ to the left.Formally,$$$f(a)=z,f(b)=a,...,f(z)=y$$$.

You can do the following operation any number of times:

  • choose two different indexes $$$i,j(1 \leq i,j \leq n)$$$,then make $$$s_i:=f(s_i)$$$ and $$$s_j:=f(s_j)$$$;

Find the minimum number of operations to make all characters in $$$s$$$ to $$$a$$$.If you can not make all characters in $$$s$$$ to $$$a$$$,output $$$-1$$$ instead.

Input

Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^5$$$). The description of the test cases follows.

The first line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 2\cdot 10^5$$$).

The second line of each testcase contains a string $$$s$$$ of size $$$n$$$ composed of lowercase letters.

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

Output

For each test case, print a single integer — the minimum number of operations to make all characters in $$$s$$$ to $$$a$$$.If you can not make all characters in $$$s$$$ to $$$a$$$,output $$$-1$$$ instead.

Example
Input
5
2
aa
2
ab
2
cc
3
amo
4
egzx
Output
0
-1
2
26
29
Note

Test Case $$$1$$$:

All characters in $$$s$$$ are already $$$a$$$ so you don't need to do any operations.

Test Case $$$2$$$:

You can not make all characters in $$$s$$$ to $$$a$$$.

Test Case $$$3$$$:

We use the following scheme: $$$cc\xrightarrow{i=1,j=2} bb\xrightarrow{i=1,j=2} aa$$$.

Test Case $$$4$$$:

We use the following scheme: $$$amo\xrightarrow{i=1,j=2} zlo\xrightarrow{i=1,j=2} yko \xrightarrow{i=1,j=2} ... \xrightarrow{i=1,j=2} oao \xrightarrow{i=1,j=3} nan \xrightarrow{i=1,j=3} mam \xrightarrow{i=1,j=3} ... \xrightarrow{i=1,j=3} aaa$$$.

D. Interesting Snake Queue
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

A snake queue can be formed as an $$$n \cdot n$$$ grid graph.

When someone enter this queue, he is in position $$$(1,1)$$$.

Assume someone is in $$$(i,j)$$$ currently(and $$$(i,j)$$$ is not the endpoint of the queue),one second later:

  • If $$$i$$$ is odd and $$$j \neq n$$$,he'll move to $$$(i,j+1)$$$;
  • If $$$i$$$ is odd and $$$j = n$$$,he'll move to $$$(i+1,j)$$$;
  • If $$$i$$$ is even and $$$j \neq 1$$$,he'll move to $$$(i,j-1)$$$;
  • If $$$i$$$ is even and $$$j = 1$$$,he'll move to $$$(i+1,j)$$$.

There're $$$n \cdot n$$$ people.The $$$i_{th}$$$ person has a number $$$a_i$$$ in hi hand.In the $$$j_{th}(1\leq j \leq n^2)$$$ second,the $$$j_{th}$$$ person will enter this queue.

For example,$$$n=3$$$ and $$$a=[11,22,33,44,55,66,77,88,99]$$$,the following images show the status of the snake queue at certain times:

The status in the $$$2_{nd},5_{th}$$$ and $$$8_{th}$$$ second,respectively.

You need to answer the $$$q$$$ queries.The $$$i_{th}$$$ query gives two integers $$$t_i,x_i$$$,which means you need to answer the sum of the numbers of all the people in the $$$x_{th}$$$ column in the $$$t_{th}$$$ second.

Input

The first line of contains two integers $$$n,q$$$ ($$$2 \le n \le 2000$$$,$$$1 \le q \le 10^6$$$).

The second line of contains $$$n \cdot n$$$ integers $$$a_1,a_2,...,a_{n^2}(1 \leq a_i \leq 2000)$$$.

The next $$$q$$$ lines,the $$$i_{th}$$$ line contains two integers $$$t_i,x_i(1 \leq t_i \leq n^2,1 \leq x_i \leq n)$$$,which means you need to answer the sum of the numbers of all the people in the $$$x_{th}$$$ column in the $$$t_{th}$$$ second.

Output

For each query, print a single integer in a new line — the sum of the numbers of all the people in the $$$x_{th}$$$ column in the $$$t_{th}$$$ second.

Example
Input
3 7
11 22 33 44 55 66 77 88 99
2 1
2 3
5 2
5 3
8 1
8 2
8 3
Output
22
0
55
55
143
132
121

E. Interesting Alternating Sum
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You're given a permutation $$$p$$$ of size $$$n$$$ and a piece of pseudocode:

    sum:=0
for i:=1 to n
sort(1,i) #sort p[1],p[2],...p[i] in ascending order
for j:=1 to n
if(j is odd) sum:=sum+p[j]
else sum:=sum-p[j]

Output the final value of the variable $$$sum$$$.

Input

The first line of input will contain a single integer $$$t(1 \leq t \leq 10^5)$$$, denoting the number of test cases.

Each test case consists of one line of input.

The first line of each test case contains an integer $$$n(1 \leq n \leq 4 \cdot 10^5)$$$.

The second line contains $$$n$$$ integers $$$p_1,p_2,...,p_n(1 \le p_i \le n,p$$$ is a permutation $$$)$$$.

The sum of $$$n$$$ over all will not exceed $$$4 \cdot 10^5$$$.

Output

For each test case,output on a new line — the final value of the variable $$$sum$$$.

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

Test Case $$$1$$$:

When $$$i=1,p=[2,1]$$$.The current value of $$$sum=0+2-1=1$$$;

When $$$i=2,p=[1,2]$$$.The current value of $$$sum=1+1-2=0$$$.

Test Case $$$2$$$:

When $$$i=1,p=[3,1,2]$$$.The current value of $$$sum=0+3-1+2=4$$$;

When $$$i=2,p=[1,3,2]$$$.The current value of $$$sum=4+1-3+2=4$$$;

When $$$i=3,p=[1,2,3]$$$.The current value of $$$sum=4+1-2+3=6$$$.

F. Interesting String Problem
time limit per test
1.5 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

Since you are a good friend of Jaber and Eyad, they are asking for your help to solve this problem.

You are given a graph consisting of $$$n$$$ nodes, which initially has no edges. For each node $$$i$$$, there's a string $$$s_i$$$ of lowercase Latin letters written on it.

You have to process $$$q$$$ queries of two types:

  • 1 u v : it means add an edge between node $$$u$$$ and node $$$v$$$.
  • 2 u t : it means for node $$$u$$$ and string $$$t$$$, output the sum of $$$cnt_v$$$ over all nodes $$$v$$$ which belong to the same component as $$$u$$$,where $$$cnt_v$$$ is the number of times $$$s_v$$$ occurs in $$$t$$$ as a substring.
Input

The first line contains one integer number $$$n$$$ ($$$1 \le n \le 2 * 10^5$$$), the number of nodes in the given graph.

The following $$$n$$$ lines, the $$$i_{th}$$$ of them has the string $$$a_i$$$, the string written on the $$$i_{th}$$$ node.

The following line has one integer number $$$q$$$ ($$$1 \le q \le 2 * 10^5$$$), which is the number of queries.

The following $$$q$$$ lines have the queries in the following form:

  • 1 u v : for a query of the first type.
  • 2 u t : for a query of the second type.

It is guaranteed that the sum of lengths of $$$s_v$$$ doesn't exceed $$$5 * 10^5$$$, and sum of lengths of the query strings doesn't exceed $$$5 * 10^5$$$.

Output

For each query of the second type, print in a single line the answer to the query.

Example
Input
4
a
ab
ba
ca
7
2 2 abab
1 2 3
2 2 abab
1 1 3
2 2 acac
1 3 4
2 2 acac
Output
2
3
2
3