A - Attach 00

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 300

問題文

以下の 2 つの条件を満たす正の整数 n の最小値を求めてください。

  • nK の倍数である。
  • n の十進表記には、02 文字以上連続して現れる。

T 個のテストケースが与えられるので、それぞれについて答えを求めてください。

制約

  • 1 \leq T \leq 10^5
  • 1 \leq K \leq 10^9
  • 入力はすべて整数

入力

入力は以下の形式で標準入力から与えられる。ここで \mathrm{case}_ii 個目のテストケースを意味する。

T
\mathrm{case}_1
\vdots
\mathrm{case}_T

各テストケースは以下の形式で与えられる。

K

出力

T 行出力せよ。i 行目には i 個目のテストケースの答えを出力せよ。


入力例 1

3
17
6801
998244353

出力例 1

1003
34005
16970154001

この入力には 3 個のテストケースが含まれています。

1 個目のテストケースにおいて、条件を満たす最小値は 1003\ (= 17 \times 59) です。

2 個目のテストケースにおいて、条件を満たす最小値は 34005\ (= 6801 \times 5) です。なお、20403\ (= 6801 \times 3) は条件を満たしません。

3 個目のテストケースにおいて、条件を満たす最小値は 16970154001\ (= 998244353 \times 17) です。このように、答えが 32 bit 整数に収まらないこともあります。

Score : 300 points

Problem Statement

Find the minimum value of a positive integer n satisfying the following two conditions.

  • n is a multiple of K.
  • In the decimal representation of n, 0 appears two or more times consecutively.

T test cases are given; solve each of them.

Constraints

  • 1 \leq T \leq 10^5
  • 1 \leq K \leq 10^9
  • All input values are integers.

Input

The input is given from Standard Input in the following format, where \mathrm{case}_i denotes the i-th test case:

T
\mathrm{case}_1
\vdots
\mathrm{case}_T

Each test case is given in the following format:

K

Output

Output T lines. The i-th line should contain the answer for the i-th test case.


Sample Input 1

3
17
6801
998244353

Sample Output 1

1003
34005
16970154001

This input contains three test cases.

For the first test case, the minimum value satisfying the conditions is 1003\ (= 17 \times 59).

For the second test case, the minimum value satisfying the conditions is 34005\ (= 6801 \times 5). Note that 20403\ (= 6801 \times 3) does not satisfy the condition.

For the third test case, the minimum value satisfying the conditions is 16970154001\ (= 998244353 \times 17). As this shows, the answer may not fit in a 32-bit integer.

B - Adjacent Tiles

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 400

問題文

同じ大きさの正方形のタイル N 枚を、平面上に重ならないように配置します。
このとき、一辺を完全に共有している相異なるタイルのペアの個数は最大でいくつになりますか?

ただし、タイルのペア (a,b) が一辺を完全に共有するとは、 a のある辺と b のある辺が完全に一致することをいいます。

T 個のテストケースが与えられるので、それぞれについて答えを求めてください。

制約

  • 1 \le T \le 2 \times 10^5
  • 1 \le N \le 10^{18}
  • 入力はすべて整数

入力

入力は以下の形式で標準入力から与えられる。ここで \mathrm{case}_ii 個目のテストケースを意味する。

T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T

各テストケースは以下の形式で与えられる。

N

出力

T 行出力せよ。i 行目には i 個目のテストケースの答えを出力せよ。


入力例 1

6
1
2
3
4
100000000000000000
1000000000000000000

出力例 1

0
1
2
4
199999999367544467
1999999998000000000

この入力には 6 個のテストケースが含まれています。

下図は、 N=1,2,3,4 の最適なタイルの配置の一例を示します。

  • 1 個目のテストケースについて、 N=1 です。
    • 一辺を完全に共有しているタイルのペアは存在しません。
  • 2 個目のテストケースについて、 N=2 です。
    • タイルのペア ({\rm a,b}) が一辺を完全に共有しています。
  • 3 個目のテストケースについて、 N=3 です。
    • タイルのペア ({\rm a,b}),({\rm b,c}) が一辺を完全に共有しています。
  • 4 個目のテストケースについて、 N=4 です。
    • タイルのペア ({\rm a,b}),({\rm a,c}),({\rm b,d}),({\rm c,d}) が一辺を完全に共有しています。

Score : 400 points

Problem Statement

Let us place N square tiles of the same size on a plane so that they do not overlap.
Then, what is the maximum possible number of pairs of different tiles that share one full side?

Here, a pair of different tiles (a,b) is said to share one full side if and only if some side of a and some side of b coincide completely.

T test cases are given; solve each of them.

Constraints

  • 1 \le T \le 2 \times 10^5
  • 1 \le N \le 10^{18}
  • All input values are integers.

Input

The input is given from Standard Input in the following format, where \mathrm{case}_i denotes the i-th test case:

T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T

Each test case is given in the following format:

N

Output

Output T lines. The i-th line should contain the answer for the i-th test case.


Sample Input 1

6
1
2
3
4
100000000000000000
1000000000000000000

Sample Output 1

0
1
2
4
199999999367544467
1999999998000000000

This input contains six test cases.

The figure below shows an example of an optimal arrangement of tiles for N=1,2,3,4.

  • For the first test case, N=1.
    • There is no pair of different tiles sharing one full side.
  • For the second test case, N=2.
    • The pair of tiles ({\rm a,b}) shares one full side.
  • For the third test case, N=3.
    • The pairs of tiles ({\rm a,b}),({\rm b,c}) share one full side.
  • For the fourth test case, N=4.
    • The pairs of tiles ({\rm a,b}),({\rm a,c}),({\rm b,d}),({\rm c,d}) share one full side.

C - Ascending Labels

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 500

問題文

N 頂点 M 辺の連結な単純無向グラフが与えられます。 頂点には 1,2,\ldots,N の番号が付けられており、i 個目の辺は頂点 U_i と頂点 V_i を結びます。

以下の条件をすべて満たす整数列 A=(A_1,A_2,\ldots,A_N)1 つ構築してください。

  • すべての頂点 v について、0 \le A_v \le N である。
  • A_1=0 である。
  • 頂点 1 以外のすべての頂点 v について、v に隣接する頂点 w のうち A_w=A_v-1 を満たすものがちょうど 1 つ存在する。

なお、本問題の制約下で、上記の条件をすべて満たす整数列が少なくとも 1 つ存在することが証明できます。 また、条件を満たす整数列が複数存在する場合、どれを出力しても正答とみなされます。

T 個のテストケースが与えられるので、それぞれについて答えを求めてください。

制約

  • 1 \le T \le 3 \times 10^4
  • 1 \le N \le 3 \times 10^5
  • N-1 \le M \le 3 \times 10^5
  • 1 \le U_i < V_i \le N
  • i \neq j ならば (U_i,V_i) \neq (U_j,V_j)
  • 与えられるグラフは連結
  • ひとつの入力における N の総和は 3 \times 10^5 以下
  • ひとつの入力における M の総和は 3 \times 10^5 以下
  • 入力はすべて整数

入力

入力は以下の形式で標準入力から与えられる。ここで \mathrm{case}_ii 個目のテストケースを意味する。

T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T

各テストケースは以下の形式で与えられる。

N M
U_1 V_1
U_2 V_2
\vdots
U_M V_M

出力

T 行出力せよ。i 行目には i 個目のテストケースの答えを出力せよ。

各テストケースについて、条件を満たす整数列 A を以下の形式で出力せよ。

A_1 A_2 \dots A_N

入力例 1

3
6 7
1 5
1 3
3 5
2 5
3 4
3 6
4 6
1 0
6 10
4 6
1 4
2 5
3 6
1 3
1 2
2 3
2 4
3 4
3 5

出力例 1

0 2 1 2 1 2
0
0 3 2 1 3 3

この入力には 3 個のテストケースが含まれています。

1 個目のテストケースについて、A=(0,2,1,2,1,2) とすることを考えます。

  • 頂点 1 について、A_1=0 です。
  • 頂点 2 について、A_w=A_2-1 を満たす隣接頂点 w は頂点 5 のただ 1 つです。
  • 頂点 3 について、A_w=A_3-1 を満たす隣接頂点 w は頂点 1 のただ 1 つです。
  • 頂点 4 について、A_w=A_4-1 を満たす隣接頂点 w は頂点 3 のただ 1 つです。
  • 頂点 5 について、A_w=A_5-1 を満たす隣接頂点 w は頂点 1 のただ 1 つです。
  • 頂点 6 について、A_w=A_6-1 を満たす隣接頂点 w は頂点 3 のただ 1 つです。

以上より、この整数列 A は問題文中の条件を満たします。

他にも、A=(0,3,1,2,2,2) としても条件を満たすため、0 3 1 2 2 2 という出力も正答とみなされます。

下図は、出力例の各テストケースについて、各頂点の外側に頂点番号 v、内側に A_v を示したものです。

Score : 500 points

Problem Statement

You are given a connected simple undirected graph with N vertices and M edges. The vertices are numbered 1,2,\ldots,N, and the i-th edge connects vertices U_i and V_i.

Construct one integer sequence A=(A_1,A_2,\ldots,A_N) satisfying all of the following conditions.

  • 0 \le A_v \le N for every vertex v.
  • A_1=0.
  • For every vertex v other than vertex 1, there is exactly one vertex w adjacent to v satisfying A_w=A_v-1.

It can be proved that under the constraints of this problem, there exists at least one integer sequence satisfying all of the above conditions. If there are multiple integer sequences satisfying the conditions, any of them will be accepted.

T test cases are given; solve each of them.

Constraints

  • 1 \le T \le 3 \times 10^4
  • 1 \le N \le 3 \times 10^5
  • N-1 \le M \le 3 \times 10^5
  • 1 \le U_i < V_i \le N
  • (U_i,V_i) \neq (U_j,V_j) if i \neq j.
  • The given graph is connected.
  • The sum of N in each input is at most 3 \times 10^5.
  • The sum of M in each input is at most 3 \times 10^5.
  • All input values are integers.

Input

The input is given from Standard Input in the following format, where \mathrm{case}_i denotes the i-th test case:

T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T

Each test case is given in the following format:

N M
U_1 V_1
U_2 V_2
\vdots
U_M V_M

Output

Output T lines. The i-th line should contain the answer for the i-th test case.

For each test case, output an integer sequence A satisfying the conditions in the following format:

A_1 A_2 \dots A_N

Sample Input 1

3
6 7
1 5
1 3
3 5
2 5
3 4
3 6
4 6
1 0
6 10
4 6
1 4
2 5
3 6
1 3
1 2
2 3
2 4
3 4
3 5

Sample Output 1

0 2 1 2 1 2
0
0 3 2 1 3 3

This input contains three test cases.

For the first test case, consider setting A=(0,2,1,2,1,2).

  • For vertex 1, A_1=0.
  • For vertex 2, there is only one adjacent vertex w satisfying A_w=A_2-1, which is vertex 5.
  • For vertex 3, there is only one adjacent vertex w satisfying A_w=A_3-1, which is vertex 1.
  • For vertex 4, there is only one adjacent vertex w satisfying A_w=A_4-1, which is vertex 3.
  • For vertex 5, there is only one adjacent vertex w satisfying A_w=A_5-1, which is vertex 1.
  • For vertex 6, there is only one adjacent vertex w satisfying A_w=A_6-1, which is vertex 3.

Thus, this integer sequence A satisfies the conditions in the problem statement.

Additionally, setting A=(0,3,1,2,2,2) also satisfies the conditions, so the output 0 3 1 2 2 2 is also accepted.

The figure below shows, for each test case in the sample output, the vertex number v outside each vertex and A_v inside each vertex.

D - Angst for All Pairs

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 500

問題文

N 枚のカードがあり、1 から N までの番号が付けられています。はじめはどのカードも何も書かれていません。

あなたはそれぞれのカードに 0 個以上の正整数を書き込むことができます。

正整数 k1 回書き込むときにかかるコストは、k の十進表記での桁数に等しいです。

以下の条件を満たすことが可能か判定し、可能であれば必要な総コストの最小値を、不可能であれば -1 を出力してください。

  • 1 \leq x < y \leq K を満たすどの正整数組 (x,y) についても、そのうち片方だけを含むカードが存在する。

T 個のテストケースが与えられるので、それぞれについて答えを求めてください。

制約

  • 1 \leq T \leq 10^5
  • 1 \leq N \leq 10^6
  • 2 \leq K \leq 10^6
  • ひとつの入力における N の総和は 10^6 以下
  • ひとつの入力における K の総和は 10^6 以下
  • 入力はすべて整数

入力

入力は以下の形式で標準入力から与えられる。ここで \mathrm{case}_ii 個目のテストケースを意味する。

T
\mathrm{case}_1
\vdots
\mathrm{case}_T

各テストケースは以下の形式で与えられる。

N K

出力

T 行出力せよ。i 行目には i 個目のテストケースの答えを出力せよ。


入力例 1

4
3 5
100 25
5 1225
180 998244

出力例 1

5
39
-1
17655598

この入力には 4 個のテストケースが含まれています。

1 個目のテストケースにおいて、以下のように書き込めば総コスト 5 で条件を満たします。

  • カード 1 には 1, 5 を書き込む。
  • カード 2 には 2, 5 を書き込む。
  • カード 3 には 3 を書き込む。

2 個目のテストケースにおいて、条件を満たすのに必要な総コストの最小値は 39 です。

3 個目のテストケースにおいて、条件を満たすことは不可能なので -1 を出力します。

4 個目のテストケースにおいて、条件を満たすのに必要な総コストの最小値は 17655598 です。

Score : 500 points

Problem Statement

There are N cards, numbered 1 through N. Initially, nothing is written on any of the cards.

You can write zero or more positive integers on each card.

The cost of writing a positive integer k once is equal to the number of digits in the decimal representation of k.

Determine whether it is possible to satisfy the following condition. If it is possible, output the minimum total cost required; if it is not possible, output -1.

  • For every pair of positive integers (x,y) satisfying 1 \leq x < y \leq K, there exists a card containing exactly one of x and y.

T test cases are given; solve each of them.

Constraints

  • 1 \leq T \leq 10^5
  • 1 \leq N \leq 10^6
  • 2 \leq K \leq 10^6
  • The sum of N in each input is at most 10^6.
  • The sum of K in each input is at most 10^6.
  • All input values are integers.

Input

The input is given from Standard Input in the following format, where \mathrm{case}_i denotes the i-th test case:

T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T

Each test case is given in the following format:

N K

Output

Output T lines. The i-th line should contain the answer for the i-th test case.


Sample Input 1

4
3 5
100 25
5 1225
180 998244

Sample Output 1

5
39
-1
17655598

This input contains four test cases.

For the first test case, writing as follows satisfies the condition with a total cost of 5.

  • Write 1, 5 on card 1.
  • Write 2, 5 on card 2.
  • Write 3 on card 3.

For the second test case, the minimum total cost required to satisfy the condition is 39.

For the third test case, it is impossible to satisfy the condition, so output -1.

For the fourth test case, the minimum total cost required to satisfy the condition is 17655598.

E - ABC|AB|A

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 500

問題文

A, B, C からなる文字列 S が与えられます。

以下の操作を 0 回以上何回でも行えます。

  • S から A または AB または ABC である部分文字列 (連続部分列) を 1 つ選択して削除する。残った文字は順序を保って連結される。

最終的な S の長さとして達成可能な最小値を求めてください。

T 個のテストケースが与えられるので、それぞれについて答えを求めてください。

制約

  • T1 以上 10^5 以下の整数
  • SA, B, C からなる長さ 1 以上 10^6 以下の文字列
  • ひとつの入力における |S| の総和は 10^6 以下

入力

入力は以下の形式で標準入力から与えられる。ここで \mathrm{case}_ii 個目のテストケースを意味する。

T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T

各テストケースは以下の形式で与えられる。

S

出力

T 行出力せよ。i 行目には i 個目のテストケースの答えを出力せよ。


入力例 1

5
CAABCB
B
AAABBC
AABBCC
CABCCBCBAABCBBBAABAABACABBC

出力例 1

1
1
0
1
7

この入力には 5 個のテストケースが含まれています。

1 個目のテストケースについて、例えば以下のように操作を行うことができます。

  • S= CAABCB3 文字目から 5 文字目を選択して削除する。操作後に SCAB となる。
  • S= CAB2 文字目から 3 文字目を選択して削除する。操作後に SC となる。
  • これ以上操作を行うことはできない。 S= C の長さは 1 であり、これが達成可能な最小です。

Score : 500 points

Problem Statement

You are given a string S consisting of A, B, and C.

You can perform the following operation zero or more times.

  • Choose a substring (contiguous subsequence) of S that is A, AB, or ABC, and delete it. The remaining characters are concatenated, preserving their order.

Find the minimum possible length of S in the end.

T test cases are given; solve each of them.

Constraints

  • T is an integer between 1 and 10^5, inclusive.
  • S is a string consisting of A, B, and C with length between 1 and 10^6, inclusive.
  • The sum of |S| in each input is at most 10^6.

Input

The input is given from Standard Input in the following format, where \mathrm{case}_i denotes the i-th test case:

T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T

Each test case is given in the following format:

S

Output

Output T lines. The i-th line should contain the answer for the i-th test case.


Sample Input 1

5
CAABCB
B
AAABBC
AABBCC
CABCCBCBAABCBBBAABAABACABBC

Sample Output 1

1
1
0
1
7

This input contains five test cases.

For the first test case, we can perform the operations as follows, for example.

  • Choose and delete the third through fifth characters of S= CAABCB. After the operation, S becomes CAB.
  • Choose and delete the second through third characters of S= CAB. After the operation, S becomes C.
  • No further operations can be performed. S= C has length 1, which is the minimum achievable.
F - AND/OR

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 700

問題文

長さ N の非負整数列 A=(A_1,A_2,\dots,A_N) と整数 K が与えられます。

はじめ、x=0 です。 i=1,2,\dots,N の順に、以下の 2 つの操作のうち一方を選んで行います。

  • 操作 1 : xx {\rm AND} A_i に置き換える。
  • 操作 2 : xx {\rm OR} A_i に置き換える。

ただし、操作 2 を選ぶ回数は全体で K 回以下でなければなりません。 ここで、{\rm AND} はビット単位 {\rm AND} 演算を、{\rm OR} はビット単位 {\rm OR} 演算を表します。

全ての操作が終了した時点の x として達成可能な最大値を M とします。 最終的な xM となる操作列の個数を 998244353 で割った余りを求めてください。

ただし、2 つの操作列は、ある整数 j (1 \le j \le N) について j 回目に選んだ操作が異なるとき、またそのときに限り区別されます。

T 個のテストケースが与えられるので、それぞれについて答えを求めてください。

ビット単位 \mathrm{AND} 演算とは

非負整数 A, B のビット単位 \mathrm{AND}A\ \mathrm{AND}\ B は以下のように定義されます。

  • A\ \mathrm{AND}\ B を二進表記した際の 2^k (k \geq 0) の位の数は、A, B を二進表記した際の 2^k の位の数のうち両方が 1 であれば 1、そうでなければ 0 である。
例えば、3\ \mathrm{AND}\ 5 = 1 となります (二進表記すると: 011\ \mathrm{AND}\ 101 = 001)。

ビット単位 \mathrm{OR} 演算とは

非負整数 A, B のビット単位 \mathrm{OR}A\ \mathrm{OR}\ B は以下のように定義されます。

  • A\ \mathrm{OR}\ B を二進表記した際の 2^k (k \geq 0) の位の数は、A, B を二進表記した際の 2^k の位の数のうち少なくとも片方が 1 であれば 1、そうでなければ 0 である。
例えば、3\ \mathrm{OR}\ 5 = 7 となります (二進表記すると: 011\ \mathrm{OR}\ 101 = 111)。

制約

  • 1 \le T \le 10^4
  • 1 \le K \le N \le 2 \times 10^5
  • 0 \le A_i < 2^{60}
  • ひとつの入力における N の総和は 2 \times 10^5 以下
  • 入力はすべて整数

入力

入力は以下の形式で標準入力から与えられる。ここで \mathrm{case}_ii 個目のテストケースを意味する。

T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T

各テストケースは以下の形式で与えられる。

N K
A_1 A_2 \dots A_N

出力

T 行出力せよ。i 行目には i 個目のテストケースの答えを出力せよ。


入力例 1

5
5 3
3 7 1 2 6
1 1
1
10 8
3 1 4 1 5 9 2 6 5 3
10 5
227966327241983404 430356836747706085 918791034668488208 753980266897555955 1090352658151595591 1077218377572152409 641276315925917062 859382256805178767 103741332934325112 1090530740094421349
33 33
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

出力例 1

3
1
26
1
603979768

この入力には 5 個のテストケースが含まれています。

1 個目のテストケースについて M=7 であり、以下の 3 通りの操作列が最終的に x=M を達成します。

  • 操作を 1,1,2,2,2 の順に行う。 x0 \to 0 \to 0 \to 1 \to 3 \to 7 と変化する。
  • 操作を 1,2,1,2,2 の順に行う。 x0 \to 0 \to 7 \to 1 \to 3 \to 7 と変化する。
  • 操作を 2,1,1,2,2 の順に行う。 x0 \to 3 \to 3 \to 1 \to 3 \to 7 と変化する。

5 個目のテストケースについて、答えを 998244353 で割った余りを出力することに注意してください。

Score : 700 points

Problem Statement

You are given a length-N sequence of non-negative integers A=(A_1,A_2,\dots,A_N) and an integer K.

Initially, x=0. For i=1,2,\dots,N in this order, let us choose one of the following two operations to perform.

  • Operation 1 : Replace x with x {\rm AND} A_i.
  • Operation 2 : Replace x with x {\rm OR} A_i.

Here, operation 2 must be chosen at most K times in total. {\rm AND} denotes the bitwise {\rm AND} operation, and {\rm OR} denotes the bitwise {\rm OR} operation.

Let M be the maximum possible value of x after all operations are finished. Find the number, modulo 998244353, of operation sequences such that the final x equals M.

Here, two operation sequences are distinguished if and only if there exists an integer j (1 \le j \le N) such that the operation chosen at the j-th step differs between them.

T test cases are given; solve each of them.

What is the bitwise \mathrm{AND} operation?

The bitwise \mathrm{AND} of non-negative integers A, B, denoted A\ \mathrm{AND}\ B, is defined as follows.

  • The digit in the 2^k (k \geq 0) place of the binary representation of A\ \mathrm{AND}\ B is 1 if the corresponding digits of A and B in binary are both 1, and 0 otherwise.
For example, 3\ \mathrm{AND}\ 5 = 1 (in binary: 011\ \mathrm{AND}\ 101 = 001).

What is the bitwise \mathrm{OR} operation?

The bitwise \mathrm{OR} of non-negative integers A, B, denoted A\ \mathrm{OR}\ B, is defined as follows.

  • The digit in the 2^k (k \geq 0) place of the binary representation of A\ \mathrm{OR}\ B is 1 if at least one of the corresponding digits of A and B in binary is 1, and 0 otherwise.
For example, 3\ \mathrm{OR}\ 5 = 7 (in binary: 011\ \mathrm{OR}\ 101 = 111).

Constraints

  • 1 \le T \le 10^4
  • 1 \le K \le N \le 2 \times 10^5
  • 0 \le A_i < 2^{60}
  • The sum of N in each input is at most 2 \times 10^5.
  • All input values are integers.

Input

The input is given from Standard Input in the following format, where \mathrm{case}_i denotes the i-th test case:

T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T

Each test case is given in the following format:

N K
A_1 A_2 \dots A_N

Output

Output T lines. The i-th line should contain the answer for the i-th test case.


Sample Input 1

5
5 3
3 7 1 2 6
1 1
1
10 8
3 1 4 1 5 9 2 6 5 3
10 5
227966327241983404 430356836747706085 918791034668488208 753980266897555955 1090352658151595591 1077218377572152409 641276315925917062 859382256805178767 103741332934325112 1090530740094421349
33 33
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

Sample Output 1

3
1
26
1
603979768

This input contains five test cases.

For the first test case, M=7, and the following three operation sequences achieve the final value x=M.

  • Perform the operations in the order 1,1,2,2,2. x changes as 0 \to 0 \to 0 \to 1 \to 3 \to 7.
  • Perform the operations in the order 1,2,1,2,2. x changes as 0 \to 0 \to 7 \to 1 \to 3 \to 7.
  • Perform the operations in the order 2,1,1,2,2. x changes as 0 \to 3 \to 3 \to 1 \to 3 \to 7.

For the fifth test case, note that the answer should be output modulo 998244353.