E - Giant Domino

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

1 から N までの番号がついた N 個のドミノがあります。ドミノ i の大きさは S_i です。
いくつかのドミノを左右一列に並べたあとにドミノを倒すことを考えます。ドミノ i が右に向けて倒れる時、ドミノ i のすぐ右に置かれているドミノの大きさが 2 S_i 以下ならばそのドミノも右に向けて倒れます。

あなたは 2 個以上のドミノを選んで左右一列に並べることにしました。ただし、ドミノの並べ方は次の条件を満たす必要があります。 

  • 一番左のドミノはドミノ 1 である。
  • 一番右のドミノはドミノ N である。
  • ドミノ 1 のみを右に向けて倒した時に、最終的にドミノ N も右に向けて倒れる。

条件を満たすドミノの並べ方は存在しますか?また、存在する場合は最小で何個のドミノを並べる必要がありますか?

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

制約

  • 1 \leq T \leq 10^5
  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq S_i \leq 10^9
  • 全てのテストケースに対する N の総和は 2 \times 10^5 以下
  • 入力される値は全て整数

入力

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

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

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

N
S_1 S_2 \dots S_N

出力

T 行出力せよ。i 行目には i 番目のテストケースの答えを出力せよ。
各テストケースでは、条件を満たすドミノの並べ方が存在しない場合は -1 を、存在する場合は並べるドミノの最小個数を出力せよ。


入力例 1

3
4
1 3 2 5
2
1 100
10
298077099 766294630 440423914 59187620 725560241 585990757 965580536 623321126 550925214 917827435

出力例 1

4
-1
3

1 番目のテストケースについて、ドミノを左から順にドミノ 1, ドミノ 3, ドミノ 2, ドミノ 4 の順に並べることで問題文の条件を満たすことができます。特に 3 番目の条件については、ドミノ 1 のみを右に向けて倒した時に以下の順にドミノが倒れます。

  • ドミノ 1 の右にはドミノ 3 が置かれている。ドミノ 3 の大きさ S_3 = 2S_1 \times 2 = 1 \times 2 = 2 以下であるから、ドミノ 3 も右に向けて倒れる。
  • ドミノ 3 の右にはドミノ 2 が置かれている。ドミノ 2 の大きさ S_2 = 3S_3 \times 2 = 2 \times 2 = 4 以下であるから、ドミノ 2 も右に向けて倒れる。
  • ドミノ 2 の右にはドミノ 4 が置かれている。ドミノ 4 の大きさ S_4 = 5S_2 \times 2 = 3 \times 2 = 6 以下であるから、ドミノ 4 も右に向けて倒れる。

3 個以下のドミノを並べて問題文の条件を達成することはできないので、答えは 4 個です。

Score : 300 points

Problem Statement

There are N dominoes numbered from 1 to N. The size of domino i is S_i.
Consider arranging some dominoes in a line from left to right and then toppling them. When domino i falls to the right, if the size of the domino placed immediately to the right of domino i is at most 2 S_i, then that domino also falls to the right.

You decided to choose two or more dominoes and arrange them in a line from left to right. The arrangement of dominoes must satisfy the following conditions:

  • The leftmost domino is domino 1.
  • The rightmost domino is domino N.
  • When only domino 1 is toppled to the right, domino N eventually falls to the right as well.

Does an arrangement of dominoes satisfying the conditions exist? If it exists, what is the minimum number of dominoes that need to be arranged?

You are given T test cases, solve the problem for each of them.

Constraints

  • 1 \leq T \leq 10^5
  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq S_i \leq 10^9
  • The sum of N over all test cases 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 means 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
S_1 S_2 \dots S_N

Output

Output T lines. The i-th line should contain the answer for the i-th test case.
For each test case, if there is no arrangement of dominoes satisfying the conditions, output -1; otherwise, output the minimum number of dominoes to arrange.


Sample Input 1

3
4
1 3 2 5
2
1 100
10
298077099 766294630 440423914 59187620 725560241 585990757 965580536 623321126 550925214 917827435

Sample Output 1

4
-1
3

For the 1st test case, arranging the dominoes from left to right in the order domino 1, domino 3, domino 2, domino 4 satisfies the conditions in the problem statement. Specifically, for the 3rd condition, when only domino 1 is toppled to the right, the dominoes fall in the following order:

  • Domino 3 is placed to the right of domino 1. Since the size of domino 3, S_3 = 2, is not greater than S_1 \times 2 = 1 \times 2 = 2, domino 3 also falls to the right.
  • Domino 2 is placed to the right of domino 3. Since the size of domino 2, S_2 = 3, is not greater than S_3 \times 2 = 2 \times 2 = 4, domino 2 also falls to the right.
  • Domino 4 is placed to the right of domino 2. Since the size of domino 4, S_4 = 5, is not greater than S_2 \times 2 = 3 \times 2 = 6, domino 4 also falls to the right.

It is impossible to achieve the conditions in the problem statement by arranging 3 or fewer dominoes, so the answer is 4.

F - Flush

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 350

問題文

ポーカーテーブルの上に N 種類のフレーバーのティーバッグが置かれています。フレーバーには 1 から N までの番号が付けられており、フレーバー i (1 \leq i \leq N) のティーバッグは A_i 個あります。

あなたはこれらのティーバッグを使ったゲームに参加します。ゲームには 1 以上 A_1 + \dots + A_N 以下の難易度が設定されており,難易度 b のゲームは以下の流れで行われます。

  1. あなたは整数 x を宣言する。このとき、b \leq x \leq A_1 + \dots + A_N を満たす必要がある。
  2. ディーラーはポーカーテーブルの上にあるティーバッグの中からちょうど x 個を選び、あなたに渡す。
  3. あなたは渡された x 個のティーバッグのフレーバーを確認し、その中から b 個のティーバッグを選ぶ。
  4. あなたが選んだ b 個のティーバッグがすべて同じフレーバーであれば、あなたは勝利する。そうでなければ、あなたは敗北する。

ディーラーはあなたが敗北するように最善を尽くすものとします。

Q 個の質問が与えられるので、それぞれに答えてください。j 番目の質問は以下の通りです。

  • 難易度 B_j のゲームに勝利するためにはじめに宣言する必要がある整数 x の最小値を答えよ。勝利が不可能であれば、代わりに -1 と答えよ。

制約

  • 1 \leq N \leq 3 \times 10^5
  • 1 \leq Q \leq 3 \times 10^5
  • 1 \leq A_i \leq 10^6 (1 \leq i \leq N)
  • 1 \leq B_j \leq \min(10^6, A_1 + \dots + A_N) (1 \leq j \leq Q)
  • 入力される値は全て整数である。

入力

入力は以下の形式で標準入力から与えられる。

N Q
A_1 \cdots A_N
B_1
\vdots
B_Q

出力

Q 行出力せよ。

j 行目 (1 \leq j \leq Q) には、j 番目のクエリに対する答えを出力せよ。


入力例 1

4 5
4 1 8 4
1
8
5
2
10

出力例 1

1
17
14
5
-1

1 番目のクエリにおいて、x=1 を宣言すれば、ディーラーがどの 1 個のティーバッグを選んで渡しても、その中から適切に 1 個を選ぶことで勝利条件を満たすことが可能です。 1 より小さい整数を x として宣言することはできないので、答えは 1 です。

2 番目のクエリにおいて、x=17 を宣言すれば、ディーラーがどの 17 個のティーバッグを選んで渡しても、その中から適切に 8 個を選ぶことで勝利条件を満たすことが可能です。 逆に x < 17 のとき、ディーラーはあなたを敗北させるようにティーバッグを選んで渡すことが可能です。したがって、答えは 17 です。

3 番目のクエリにおいて、x=14 を宣言すれば、ディーラーがどの 14 個のティーバッグを選んで渡しても、その中から適切に 5 個を選ぶことで勝利条件を満たすことが可能です。 逆に x < 14 のとき、ディーラーはあなたを敗北させるようにティーバッグを選んで渡すことが可能です。したがって、答えは 14 です。

4 番目のクエリにおいて、x=5 を宣言すれば、ディーラーがどの 5 個のティーバッグを選んで渡しても、その中から適切に 2 個を選ぶことで勝利条件を満たすことが可能です。 逆に x < 5 のとき、ディーラーはあなたを敗北させるようにティーバッグを選んで渡すことが可能です。したがって、答えは 5 です。

5 番目のクエリにおいて、どのように x を宣言しても、ディーラーはあなたを敗北させるようにティーバッグを選んで渡すことが可能です。したがって、-1 を出力します。


入力例 2

5 3
13 13 13 13 2
5
12
13

出力例 2

19
47
51

Score : 350 points

Problem Statement

On the poker table, there are tea bags of N different flavors. The flavors are numbered from 1 through N, and there are A_i tea bags of flavor i (1 \leq i \leq N).

You will play a game using these tea bags. The game has a parameter called difficulty between 1 and A_1 + \dots + A_N, inclusive. A game of difficulty b proceeds as follows:

  1. You declare an integer x. Here, it must satisfy b \leq x \leq A_1 + \dots + A_N.
  2. The dealer chooses exactly x tea bags from among those on the table and gives them to you.
  3. You check the flavors of the x tea bags you received, and choose b tea bags from them.
  4. If all b tea bags you chose are of the same flavor, you win. Otherwise, you lose.

The dealer will do their best to make you lose.

You are given Q queries, so answer each of them. The j-th query is as follows:

  • For a game of difficulty B_j, report the minimum integer x you must declare at the start to guarantee a win. If it is impossible to win, report -1 instead.

Constraints

  • 1 \leq N \leq 3 \times 10^5
  • 1 \leq Q \leq 3 \times 10^5
  • 1 \leq A_i \leq 10^6 (1 \leq i \leq N)
  • 1 \leq B_j \leq \min(10^6, A_1 + \dots + A_N) (1 \leq j \leq Q)
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

N Q
A_1 \cdots A_N
B_1
\vdots
B_Q

Output

Print Q lines.

The j-th line (1 \leq j \leq Q) should contain the answer to the j-th query.


Sample Input 1

4 5
4 1 8 4
1
8
5
2
10

Sample Output 1

1
17
14
5
-1

For the 1-st query, if you declare x=1, then no matter which 1 bag the dealer chooses, you can satisfy the winning condition by choosing appropriate 1 bag among them. Since you cannot choose an integer x less than 1, the answer is 1.

For the 2-nd query, if you declare x=17, then no matter which 17 bags the dealer chooses, you can satisfy the winning condition by choosing appropriate 8 bags among them. Conversely, if x < 17, the dealer can choose bags to prevent your victory. Thus, the answer is 17.

For the 3-rd query, if you declare x=14, then no matter which 14 bags the dealer chooses, you can satisfy the winning condition by choosing appropriate 5 bags among them. Conversely, if x < 14, the dealer can choose bags to prevent your victory. Thus, the answer is 14.

For the 4-th query, if you declare x=5, then no matter which 5 bags the dealer chooses, you can satisfy the winning condition by choosing appropriate 2 bags among them. Conversely, if x < 5, the dealer can choose bags to prevent your victory. Thus, the answer is 5.

For the 5-th query, no matter what x you declare, the dealer can choose bags to prevent your victory. Thus, print -1.


Sample Input 2

5 3
13 13 13 13 2
5
12
13

Sample Output 2

19
47
51
G - Unique Username

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 400

問題文

高橋君はあるサービスで使うユーザー名を決めるのに困っています。彼を助けるプログラムを書いてください。

以下の条件をすべて満たす文字列 X1 つ求めてください。

  • X は次の手順で得られる文字列である。
    • N 個の文字列 S_1,S_2,\ldots,S_N を好きな順番で並べたものを S_1', S_2', \ldots,S_N' とする。そして、S_1'、( 1 個以上の _ )、S_2'、( 1 個以上の _ )、\ldots、( 1 個以上の _ )、S_N' をこの順番で連結したものを X とする。
  • X の文字数は 3 以上 16 以下である。
  • XM 個の文字列 T_1,T_2,\ldots,T_M のいずれとも一致しない。

ただし、条件をすべて満たす文字列 X が存在しない場合は代わりに -1 と出力してください。

制約

  • 1 \leq N \leq 8
  • 0 \leq M \leq 10^5
  • N,M は整数
  • 1 \leq |S_i| \leq 16
  • N-1+\sum{|S_i|} \leq 16
  • i \neq j ならば S_i \neq S_j
  • S_i は英小文字のみからなる文字列
  • 3 \leq |T_i| \leq 16
  • i \neq j ならば T_i \neq T_j
  • T_i は英小文字と _ のみからなる文字列

入力

入力は以下の形式で標準入力から与えられる。

N M
S_1
S_2
\vdots
S_N
T_1
T_2
\vdots
T_M

出力

条件をすべて満たす文字列 X1 つ出力せよ。ただし、条件をすべて満たす文字列 X が存在しない場合は代わりに -1 を出力せよ。
答えが複数存在する場合、どれを出力しても良い。


入力例 1

1 1
chokudai
chokudai

出力例 1

-1

条件のうち 1 番目と 2 番目を満たす文字列は X= chokudai しかありませんが、これは T_1 と一致します。
このため、条件をすべて満たす文字列 X が存在せず、-1 が正しい出力となります。


入力例 2

2 2
choku
dai
chokudai
choku_dai

出力例 2

dai_choku

この他に、choku__dai (chokudai の間の _2 つ) 等も条件をすべて満たします。


入力例 3

2 2
chokudai
atcoder
chokudai_atcoder
atcoder_chokudai

出力例 3

-1

chokudai__atcoderatcoder__chokudai (chokudaiatcoder の間の _2 つ)は文字数が 17 なので 2 番目の条件を満たしません。


入力例 4

4 4
ab
cd
ef
gh
hoge
fuga
____
_ab_cd_ef_gh_

出力例 4

ab__ef___cd_gh

1 番目の条件で記述されている操作で得られないような文字列が T_i として与えられる場合があります。

Score : 400 points

Problem Statement

Takahashi is having trouble with deciding a username for a service. Write a code to help him.

Find a string X that satisfies all of the following conditions:

  • X is obtained by the following procedure:
    • Let S_1', S_2', \ldots,S_N' be a permutation of S_1, S_2, \ldots,S_N. Let X be the concatenation of S_1', (1 or more copies of _), S_2', (1 or more copies of _), \ldots, (1 or more copies of _), and S_N', in this order.
  • The length of X is between 3 and 16, inclusive.
  • X does not coincide with any of M strings T_1,T_2,\ldots,T_M.

If there is no X that satisfies all of the conditions, print -1 instead.

Constraints

  • 1 \leq N \leq 8
  • 0 \leq M \leq 10^5
  • N and M are integers.
  • 1 \leq |S_i| \leq 16
  • N-1+\sum{|S_i|} \leq 16
  • S_i \neq S_j if i \neq j.
  • S_i is a string consisting of lowercase English letters.
  • 3 \leq |T_i| \leq 16
  • T_i \neq T_j if i \neq j.
  • T_i is a string consisting of lowercase English letters and _.

Input

Input is given from Standard Input in the following format:

N M
S_1
S_2
\vdots
S_N
T_1
T_2
\vdots
T_M

Output

Print a string X that satisfies all of the conditions. If there is no X that satisfies all of the conditions, print -1 instead.
If there are multiple solutions, print any of them.


Sample Input 1

1 1
chokudai
chokudai

Sample Output 1

-1

The only string that satisfies the first and second conditions is X= chokudai, but it coincides with T_1.
Thus, there is no X that satisfies all of the conditions, so -1 should be printed.


Sample Input 2

2 2
choku
dai
chokudai
choku_dai

Sample Output 2

dai_choku

Strings like choku__dai (which has two _'s between choku and dai) also satisfy all of the conditions.


Sample Input 3

2 2
chokudai
atcoder
chokudai_atcoder
atcoder_chokudai

Sample Output 3

-1

chokudai__atcoder and atcoder__chokudai (which have two _'s between chokudai and atcoder) have a length of 17, which violates the second condition.


Sample Input 4

4 4
ab
cd
ef
gh
hoge
fuga
____
_ab_cd_ef_gh_

Sample Output 4

ab__ef___cd_gh

The given T_i may contain a string that cannot be obtained by the procedure described in the first condition.

H - E [max]

Time Limit: 3 sec / Memory Limit: 1024 MiB

配点 : 450

問題文

N 個の六面サイコロがあります。 サイコロには 1 から N までの番号が付けられており、サイコロ i の各面に書かれた数はそれぞれ A_{i,1},A_{i,2},\dots,A_{i,6} です。

今からこれら N 個のサイコロを全て同時に振ります。 各サイコロの上を向いた面に書かれた数の最大値の期待値を \bmod\ 998244353 で求めてください。

なお、いずれのサイコロについても、そのサイコロを振った際に上を向く面は独立かつ一様ランダムに選ばれるものとします。

期待値を \bmod\ 998244353 で求めるとは

求める期待値は必ず有理数になることが証明できます。 また、この問題の制約のもとでは、その値を既約分数 \frac{P}{Q} で表した時、Q \not\equiv 0 \pmod{998244353} となることも証明できます。 よって、R \times Q \equiv P \pmod{998244353}, 0 \leq R < 998244353 を満たす整数 R が一意に定まります。 この R を求めてください。

制約

  • 1\leq N \leq 10^5
  • 1\leq A_{i,j} \leq 10^9
  • 入力は全て整数

入力

入力は以下の形式で標準入力から与えられる。

N
A_{1,1} A_{1,2} \dots A_{1,6}
A_{2,1} A_{2,2} \dots A_{2,6}
\vdots
A_{N,1} A_{N,2} \dots A_{N,6}

出力

答えを出力せよ。


入力例 1

2
1 1 4 4 4 4
1 1 1 3 3 3

出力例 1

332748121

サイコロ i の上を向いた面に書かれた数を x_i とします。

  • x_1=1,x_2=1 のケース:\frac{2}{6}\times\frac{3}{6}=\frac{1}{6} の確率で発生し、上を向いた面に書かれた数の最大値は 1 です。
  • x_1=1,x_2=3 のケース:\frac{2}{6}\times\frac{3}{6}=\frac{1}{6} の確率で発生し、上を向いた面に書かれた数の最大値は 3 です。
  • x_1=4,x_2=1 のケース:\frac{4}{6}\times\frac{3}{6}=\frac{1}{3} の確率で発生し、上を向いた面に書かれた数の最大値は 4 です。
  • x_1=4,x_2=3 のケース:\frac{4}{6}\times\frac{3}{6}=\frac{1}{3} の確率で発生し、上を向いた面に書かれた数の最大値は 4 です。

よって、求める期待値は \frac{1}{6}\times 1 + \frac{1}{6}\times 3 + \frac{1}{3}\times 4 + \frac{1}{3}\times 4 = \frac{10}{3} \equiv 332748121 \pmod{998244353} です。


入力例 2

2
1 1 1 1 1 1
2 2 2 2 2 2

出力例 2

2

サイコロ 1,2 の上を向いた面に書かれた数はそれぞれ常に 1,2 であり、その最大値は常に 2 です。


入力例 3

8
55 76 80 21 34 28
82 84 2 32 56 17
11 57 37 28 39 18
47 2 97 25 75 29
72 45 22 75 26 81
6 79 16 68 68 40
31 80 68 57 18 55
49 10 63 91 93 40

出力例 3

213725517

Score : 450 points

Problem Statement

There are N six-sided dice. The dice are numbered from 1 to N, and the numbers written on the faces of die i are A_{i,1},A_{i,2},\dots,A_{i,6}.

Now, all N dice will be rolled simultaneously. Find the expected value, modulo 998244353, of the maximum value among the numbers written on the face that comes up on each die.

For any die, the face that comes up when the die is rolled is chosen independently and uniformly at random.

Finding the expected value modulo 998244353

It can be proved that the expected value to be found is always a rational number. Also, under the constraints of this problem, it can be proved that when the value is expressed as an irreducible fraction \frac{P}{Q}, we have Q \not\equiv 0 \pmod{998244353}. Therefore, there exists a unique integer R such that R \times Q \equiv P \pmod{998244353}, 0 \leq R < 998244353. Find this R.

Constraints

  • 1\leq N \leq 10^5
  • 1\leq A_{i,j} \leq 10^9
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

N
A_{1,1} A_{1,2} \dots A_{1,6}
A_{2,1} A_{2,2} \dots A_{2,6}
\vdots
A_{N,1} A_{N,2} \dots A_{N,6}

Output

Output the answer.


Sample Input 1

2
1 1 4 4 4 4
1 1 1 3 3 3

Sample Output 1

332748121

Let x_i be the number written on the face that comes up on die i.

  • Case x_1=1,x_2=1: Occurs with probability \frac{2}{6}\times\frac{3}{6}=\frac{1}{6}, and the maximum value among the numbers written on the faces that come up is 1.
  • Case x_1=1,x_2=3: Occurs with probability \frac{2}{6}\times\frac{3}{6}=\frac{1}{6}, and the maximum value among the numbers written on the faces that come up is 3.
  • Case x_1=4,x_2=1: Occurs with probability \frac{4}{6}\times\frac{3}{6}=\frac{1}{3}, and the maximum value among the numbers written on the faces that come up is 4.
  • Case x_1=4,x_2=3: Occurs with probability \frac{4}{6}\times\frac{3}{6}=\frac{1}{3}, and the maximum value among the numbers written on the faces that come up is 4.

Thus, the expected value to be found is \frac{1}{6}\times 1 + \frac{1}{6}\times 3 + \frac{1}{3}\times 4 + \frac{1}{3}\times 4 = \frac{10}{3} \equiv 332748121 \pmod{998244353}.


Sample Input 2

2
1 1 1 1 1 1
2 2 2 2 2 2

Sample Output 2

2

The numbers written on the faces that come up on dice 1,2 are always 1,2, respectively, and their maximum value is always 2.


Sample Input 3

8
55 76 80 21 34 28
82 84 2 32 56 17
11 57 37 28 39 18
47 2 97 25 75 29
72 45 22 75 26 81
6 79 16 68 68 40
31 80 68 57 18 55
49 10 63 91 93 40

Sample Output 3

213725517
I - Rated Range

Time Limit: 2.5 sec / Memory Limit: 1024 MiB

配点 : 525

問題文

高橋君はAtCoderのコンテストに N 回参加しようとしています。

i 回目 (1 \leq i \leq N) のコンテストでは、レーティングが L_i 以上 R_i 以下である場合、レーティングが 1 増加します。

以下の形式で与えられる Q 個のクエリに答えてください。

  • 整数 X が与えられる。高橋君の最初のレーティングが X であった場合、N 回のコンテストを終えた後のレーティングを求めてください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq L_i \leq R_i \leq 5 \times 10^5 (1 \leq i \leq N)
  • 1 \leq Q \leq 3 \times 10^5
  • 各クエリについて 1 \leq X \leq 5 \times 10^5
  • 入力は全て整数

入力

入力は以下の形式で標準入力から与えられる。

N
L_1 R_1
L_2 R_2
\vdots
L_N R_N
Q
\text{query}_1
\text{query}_2
\vdots
\text{query}_Q

ただし、\text{query}_ii 個目のクエリを表し、以下の形式である。

X

出力

Q 行出力せよ。i 行目には、i 個目のクエリに対する答えを出力せよ。


入力例 1

5
1 5
1 3
3 6
2 4
4 7
3
3
2
5

出力例 1

6
6
8

1 個目のクエリでは以下のようにコンテストごとにレーティングが変化します。

  • 1 回目のコンテストではレーティングが 1 以上 5 以下のため、レーティングが 1 増加し 4 になります。
  • 2 回目のコンテストではレーティングが 1 以上 3 以下でないため、レーティングが増加せず 4 のままです。
  • 3 回目のコンテストではレーティングが 3 以上 6 以下のため、レーティングが 1 増加し 5 になります。
  • 4 回目のコンテストではレーティングが 2 以上 4 以下でないため、レーティングが増加せず 5 のままです。
  • 5 回目のコンテストではレーティングが 4 以上 7 以下のため、レーティングが 1 増加し 6 になります。

2 個目のクエリでは 1,2,3,5 回目のコンテストでレーティングが 1 増加するため、レーティングは 6 になります。

3 個目のクエリでは 1,3,5 回目のコンテストでレーティングが 1 増加するため、レーティングは 8 になります。


入力例 2

10
1 1999
1 1999
1200 2399
1 1999
1 1999
1 1999
2000 500000
1 1999
1 1999
1600 2799
7
1
1995
2000
2399
500000
2799
1000

出力例 2

8
2002
2003
2402
500001
2800
1007

入力例 3

15
260522 414575
436426 479445
148772 190081
190629 433447
47202 203497
394325 407775
304784 463982
302156 468417
131932 235902
78537 395728
223857 330739
286918 329211
39679 238506
63340 186568
160016 361868
10
287940
296263
224593
101449
336991
390310
323355
177068
11431
8580

出力例 3

287946
296269
224599
101453
336997
390315
323363
177075
11431
8580

Score : 525 points

Problem Statement

Takahashi plans to participate in N AtCoder contests.

In the i-th contest (1 \leq i \leq N), if his rating is between L_i and R_i (inclusive), his rating increases by 1.

You are given Q queries in the following format:

  • An integer X is given. Assuming that Takahashi's initial rating is X, determine his rating after participating in all N contests.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq L_i \leq R_i \leq 5 \times 10^5 (1 \leq i \leq N)
  • 1 \leq Q \leq 3 \times 10^5
  • For each query, 1 \leq X \leq 5 \times 10^5.
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

N
L_1 R_1
L_2 R_2
\vdots
L_N R_N
Q
\text{query}_1
\text{query}_2
\vdots
\text{query}_Q

Here, \text{query}_i is the i-th query in the form:

X

Output

Print Q lines. The i-th line should contain the answer to the i-th query.


Sample Input 1

5
1 5
1 3
3 6
2 4
4 7
3
3
2
5

Sample Output 1

6
6
8

For the 1st query, the rating changes as follows:

  • In the 1st contest, the rating is between 1 and 5, so it increases by 1, becoming 4.
  • In the 2nd contest, the rating is not between 1 and 3, so it remains 4.
  • In the 3rd contest, the rating is between 3 and 6, so it increases by 1, becoming 5.
  • In the 4th contest, the rating is not between 2 and 4, so it remains 5.
  • In the 5th contest, the rating is between 4 and 7, so it increases by 1, becoming 6.

For the 2nd query, the rating increases in the 1st, 2nd, 3rd, and 5th contests, ending at 6.

For the 3rd query, the rating increases in the 1st, 3rd, and 5th contests, ending at 8.


Sample Input 2

10
1 1999
1 1999
1200 2399
1 1999
1 1999
1 1999
2000 500000
1 1999
1 1999
1600 2799
7
1
1995
2000
2399
500000
2799
1000

Sample Output 2

8
2002
2003
2402
500001
2800
1007

Sample Input 3

15
260522 414575
436426 479445
148772 190081
190629 433447
47202 203497
394325 407775
304784 463982
302156 468417
131932 235902
78537 395728
223857 330739
286918 329211
39679 238506
63340 186568
160016 361868
10
287940
296263
224593
101449
336991
390310
323355
177068
11431
8580

Sample Output 3

287946
296269
224599
101453
336997
390315
323363
177075
11431
8580