A - Status Code

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

配点 : 100

問題文

100 以上 999 以下の整数 S が与えられます。

S200 以上 299 以下のとき Success 、そうでないとき Failure と出力してください。

制約

  • 100\leq S\leq999
  • S は整数

入力

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

S

出力

答えを出力せよ。


入力例 1

200

出力例 1

Success

200200 以上 299 以下なので、Success と出力してください。


入力例 2

401

出力例 2

Failure

401200 以上 299 以下ではないので、Failure と出力してください。


入力例 3

999

出力例 3

Failure

Score : 100 points

Problem Statement

You are given an integer S between 100 and 999 (inclusive).

If S is between 200 and 299 (inclusive), print Success; otherwise, print Failure.

Constraints

  • 100 \le S \le 999
  • S is an integer.

Input

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

S

Output

Print the answer.


Sample Input 1

200

Sample Output 1

Success

200 is between 200 and 299, so print Success.


Sample Input 2

401

Sample Output 2

Failure

401 is not between 200 and 299, so print Failure.


Sample Input 3

999

Sample Output 3

Failure
B - Long Loong

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

配点 : 100

問題文

正の整数 X について、レベル X龍文字列 とは、1 個の L, X 個の o, 1 個の n, 1 個の g をこの順に並べた長さ (X+3) の文字列です。

正の整数 N が与えられるので、レベル N の龍文字列を出力してください。
大文字と小文字は区別されることに注意してください。

制約

  • 1\leq N\leq 2024
  • N は整数

入力

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

N

出力

レベル N の龍文字列を出力せよ。


入力例 1

3

出力例 1

Looong

1 個の L, 3 個の o, 1 個の n, 1 個の g をこの順に並べた文字列は Looong です。


入力例 2

1

出力例 2

Long

Score: 100 points

Problem Statement

For a positive integer X, the Dragon String of level X is a string of length (X+3) formed by one L, X occurrences of o, one n, and one g arranged in this order.

You are given a positive integer N. Print the Dragon String of level N.
Note that uppercase and lowercase letters are distinguished.

Constraints

  • 1 \leq N \leq 2024
  • N is an integer.

Input

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

N

Output

Print the Dragon String of level N.


Sample Input 1

3

Sample Output 1

Looong

Arranging one L, three os, one n, and one g in this order yields Looong.


Sample Input 2

1

Sample Output 2

Long
C - Slimes

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

配点 : 200

問題文

A 匹のスライムがいます。

すぬけくんが 1 回叫ぶたびに、スライムは K 倍に増殖します。

スライムが B 匹以上になるには、すぬけくんは最小で何回叫ぶ必要があるでしょうか?

制約

  • 1 \leq A \leq B \leq 10^9
  • 2 \leq K \leq 10^9
  • 入力は全て整数

入力

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

A B K

出力

答えを出力せよ。


入力例 1

1 4 2

出力例 1

2

はじめ、スライムが 1 匹います。すぬけくんが 1 回叫ぶとスライムは 2 匹になり、 2 回叫ぶとスライムは 4 匹になります。4 匹以上になるためには、最小で 2 回叫ぶ必要があります。


入力例 2

7 7 10

出力例 2

0

はじめからスライムは 7 匹います。


入力例 3

31 415926 5

出力例 3

6

Score : 200 points

Problem Statement

There are A slimes.

Each time Snuke shouts, the slimes multiply by K times.

In order to have B or more slimes, at least how many times does Snuke need to shout?

Constraints

  • 1 \leq A \leq B \leq 10^9
  • 2 \leq K \leq 10^9
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B K

Output

Print the answer.


Sample Input 1

1 4 2

Sample Output 1

2

We start with one slime. After Snuke's first shout, we have two slimes; after his second shout, we have four slimes. Thus, he needs to shout at least twice to have four or more slimes.


Sample Input 2

7 7 10

Sample Output 2

0

We have seven slimes already at the start.


Sample Input 3

31 415926 5

Sample Output 3

6
D - Precondition

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

配点 : 200

問題文

英小文字および英大文字のみからなる文字列 S, T が与えられます。

文字列 S が以下の条件を満たしているか判定してください。

  • S の先頭でない英大文字の直前の文字はすべて T に含まれる。より形式的には、2 \leq i \leq |S| なる整数 i について Si 番目の文字が英大文字ならば、Si-1 番目の文字は T に含まれる。

制約

  • S, T は長さ 1 以上 100 以下の英小文字および英大文字のみからなる文字列

入力

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

S
T

出力

S が問題文中の条件を満たしているとき Yes と出力せよ。そうでないとき、No と出力せよ。


入力例 1

AtCoder
Total

出力例 1

Yes

S の先頭でない英大文字は 3 番目の文字の C のみです。この直前の文字である tT に含まれているため、Yes と出力すればよいです。


入力例 2

aBCdE
abcdcba

出力例 2

No

S3 番目の文字は英大文字 C であり、その直前の文字は B ですが、BT に含まれていません。


入力例 3

abcde
XYZ

出力例 3

Yes

Score : 200 points

Problem Statement

You are given strings S and T consisting of lowercase and uppercase English letters.

Determine whether the string S satisfies the following condition:

  • Every uppercase letter in S that is not at the beginning is immediately preceded by a character contained in T. More formally, for all integers i such that 2 \leq i \leq |S|, if the i-th character of S is uppercase, then the (i-1)-th character of S is contained in T.

Constraints

  • Each of S and T is a string consisting of lowercase and uppercase English letters with length between 1 and 100, inclusive.

Input

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

S
T

Output

If S satisfies the condition in the problem statement, output Yes. Otherwise, output No.


Sample Input 1

AtCoder
Total

Sample Output 1

Yes

The only uppercase letter in S that is not at the beginning is the 3rd character C. The immediately preceding character t is contained in T, so output Yes.


Sample Input 2

aBCdE
abcdcba

Sample Output 2

No

The 3rd character of S is the uppercase letter C, and its immediately preceding character is B, but B is not contained in T.


Sample Input 3

abcde
XYZ

Sample Output 3

Yes
E - Product

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

配点 : 300

問題文

N 個の袋があります。
i には L_i 個のボールが入っていて、袋 ij(1\leq j\leq L_i) 番目のボールには正の整数 a_{i,j} が書かれています。

それぞれの袋から 1 つずつボールを取り出します。
取り出したボールに書かれた数の総積が X になるような取り出し方は何通りありますか?

ただし、書かれた数が同じであっても全てのボールは区別します。

制約

  • N \geq 2
  • L_i \geq 2
  • 袋に入っているボールの個数の総積は 10^5 を超えない。すなわち、\displaystyle\prod_{i=1}^{N}L_i \leq 10^5
  • 1 \leq a_{i,j} \leq 10^9
  • 1 \leq X \leq 10^{18}
  • 入力に含まれる値は全て整数である。

入力

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

N X
L_1 a_{1,1} a_{1,2} \ldots a_{1,L_1}
L_2 a_{2,1} a_{2,2} \ldots a_{2,L_2}
\vdots
L_N a_{N,1} a_{N,2} \ldots a_{N,L_N}

出力

答えを出力せよ。


入力例 1

2 40
3 1 8 4
2 10 5

出力例 1

2

13 番目のボールと袋 21 番目のボールを選ぶと、a_{1,3} \times a_{2,1} = 4 \times 10 = 40 となります。
12 番目のボールと袋 22 番目のボールを選ぶと、a_{1,2} \times a_{2,2} = 8 \times 5 = 40 となります。
これ以外に総積が 40 になる取り出し方は存在しないので、答えは 2 です。


入力例 2

3 200
3 10 10 10
3 10 10 10
5 2 2 2 2 2

出力例 2

45

書かれた数が同じであっても全てのボールは区別することに注意してください。


入力例 3

3 1000000000000000000
2 1000000000 1000000000
2 1000000000 1000000000
2 1000000000 1000000000

出力例 3

0

総積が X になる取り出し方が 1 つも存在しないこともあります。

Score : 300 points

Problem Statement

We have N bags.
Bag i contains L_i balls. The j-th ball (1\leq j\leq L_i) in Bag i has a positive integer a_{i,j} written on it.

We will pick out one ball from each bag.
How many ways are there to pick the balls so that the product of the numbers written on the picked balls is X?

Here, we distinguish all balls, even with the same numbers written on them.

Constraints

  • N \geq 2
  • L_i \geq 2
  • The product of the numbers of balls in the bags is at most 10^5: \displaystyle\prod_{i=1}^{N}L_i \leq 10^5.
  • 1 \leq a_{i,j} \leq 10^9
  • 1 \leq X \leq 10^{18}
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N X
L_1 a_{1,1} a_{1,2} \ldots a_{1,L_1}
L_2 a_{2,1} a_{2,2} \ldots a_{2,L_2}
\vdots
L_N a_{N,1} a_{N,2} \ldots a_{N,L_N}

Output

Print the answer.


Sample Input 1

2 40
3 1 8 4
2 10 5

Sample Output 1

2

When choosing the 3-rd ball in Bag 1 and 1-st ball in Bag 2, we have a_{1,3} \times a_{2,1} = 4 \times 10 = 40.
When choosing the 2-nd ball in Bag 1 and 2-nd ball in Bag 2, we have a_{1,2} \times a_{2,2} = 8 \times 5 = 40.
There are no other ways to make the product 40, so the answer is 2.


Sample Input 2

3 200
3 10 10 10
3 10 10 10
5 2 2 2 2 2

Sample Output 2

45

Note that we distinguish all balls, even with the same numbers written on them.


Sample Input 3

3 1000000000000000000
2 1000000000 1000000000
2 1000000000 1000000000
2 1000000000 1000000000

Sample Output 3

0

There may be no way to make the product X.

F - Make it Forest

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

配点 : 350

問題文

頂点に 1 から N の番号がついた N 頂点 M 辺の単純無向グラフが与えられます。i 番目の辺は頂点 u_i と頂点 v_i を結ぶ辺です。
グラフを森にするためには辺を最低何本削除する必要がありますか?

森とは 単純無向グラフ F が森であるとは、F が閉路を含まないことを言います。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 0 \leq M \leq \min \left( \frac{N(N-1)}{2}, 2 \times 10^5\right)
  • 1 \leq u_i \lt v_i \leq N
  • 入力で与えられるグラフは単純
  • 入力される値は全て整数

入力

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

N M
u_1 v_1
u_2 v_2
\vdots
u_M v_M

出力

答えを出力せよ。


入力例 1

4 4
1 2
1 3
2 4
3 4

出力例 1

1

例えば 1 番目の辺を削除すると、グラフは森になります。


入力例 2

5 0

出力例 2

0

入力例 3

10 10
7 9
4 6
6 10
2 5
5 6
5 9
6 8
4 8
1 5
1 4

出力例 3

2

Score : 350 points

Problem Statement

You are given a simple undirected graph with N vertices and M edges, where the vertices are labeled 1 to N. The i-th edge connects vertices u_i and v_i.
What is the minimum number of edges that need to be deleted from this graph so that the graph becomes a forest?

What is a forest? A simple undirected graph F is called a forest if and only if F does not contain any cycle.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 0 \leq M \leq \min \left( \frac{N(N-1)}{2}, 2 \times 10^5\right)
  • 1 \leq u_i < v_i \leq N
  • The given graph is simple.
  • All input values are integers.

Input

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

N M
u_1 v_1
u_2 v_2
\vdots
u_M v_M

Output

Print the answer.


Sample Input 1

4 4
1 2
1 3
2 4
3 4

Sample Output 1

1

For example, if you delete the first edge, the graph becomes a forest.


Sample Input 2

5 0

Sample Output 2

0

Sample Input 3

10 10
7 9
4 6
6 10
2 5
5 6
5 9
6 8
4 8
1 5
1 4

Sample Output 3

2
G - Transmission Mission

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

配点 : 400

問題文

数直線上に N 棟の家があり、1 から N までの番号が付けられています。 家 i は座標 X_i に位置しています。同じ座標に複数の家が位置していることもあります。

あなたは M 個の基地局を数直線上の任意の実数座標に配置します。そして、それぞれの基地局に対して非負整数の値の電波強度を設定します。

ある基地局の電波強度を x にしたとき、その基地局からの電波が家に届く条件は、その基地局とその家の距離が \displaystyle\frac{x}{2} 以下であることです。 特に x=0 の場合、その基地局と同じ座標に位置する家にのみ電波が届きます。

どの家にも少なくとも 1 つの基地局から電波が届くように基地局の位置と電波強度を設定するとき、電波強度の総和がとりうる最小値を求めてください。

なお、制約を満たす任意の入力に対して答えが整数であることが証明できます。

制約

  • 1 \leq M \leq N \leq 5 \times 10^5
  • 1 \leq X_i \leq 10^{12} (1 \leq i \leq N)
  • 入力される数値は全て整数である。

入力

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

N M
X_1 \dots X_N

出力

答えを整数として 1 行で出力せよ。


入力例 1

7 3
5 10 15 20 8 14 15

出力例 1

6

以下のように 3 つの基地局を置くことで、すべての家に電波が届きます。

  • 座標 7.5 に電波強度 5 の基地局を置く。この基地局から電波が届くのは家 1,2,5 である。
  • 座標 14.5 に電波強度 1 の基地局を置く。この基地局から電波が届くのは家 3,6,7 である。
  • 座標 20 に電波強度 0 の基地局を置く。この基地局から電波が届くのは家 4 である。

このときの電波強度の総和は 6 です。

電波強度の総和が 6 より小さい配置で条件を達成することはできないので、6 を出力します。


入力例 2

7 7
5 10 15 20 8 14 15

出力例 2

0

入力例 3

7 1
5 10 15 20 8 14 15

出力例 3

15

Score : 400 points

Problem Statement

There are N houses numbered from 1 to N on a number line. House i is located at coordinate X_i. Multiple houses may be located at the same coordinate.

You place M base stations at arbitrary real coordinates on the number line. Then, you set a non-negative integer signal strength for each base station.

When the signal strength of a base station is set to x, The signal from that base station reaches a house if and only if the distance between the base station and the house is at most \displaystyle\frac{x}{2}. Particularly, when x=0, the signal reaches only houses located at the same coordinate as the base station.

Find the minimum possible sum of signal strengths when the positions and signal strengths of the base stations are set such that at least one base station's signal reaches every house.

It can be proved that the answer is an integer for any input satisfying the constraints.

Constraints

  • 1 \leq M \leq N \leq 5 \times 10^5
  • 1 \leq X_i \leq 10^{12} (1 \leq i \leq N)
  • All input values are integers.

Input

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

N M
X_1 \dots X_N

Output

Output the answer as an integer in one line.


Sample Input 1

7 3
5 10 15 20 8 14 15

Sample Output 1

6

By placing three base stations as follows, signals reach all houses.

  • Place a base station with signal strength 5 at coordinate 7.5. This base station reaches houses 1,2,5.
  • Place a base station with signal strength 1 at coordinate 14.5. This base station reaches houses 3,6,7.
  • Place a base station with signal strength 0 at coordinate 20. This base station reaches house 4.

The sum of signal strengths in this case is 6.

It is impossible to satisfy the condition with an arrangement where the sum of signal strengths is smaller than 6, so output 6.


Sample Input 2

7 7
5 10 15 20 8 14 15

Sample Output 2

0

Sample Input 3

7 1
5 10 15 20 8 14 15

Sample Output 3

15
H - Subsequence Path

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

配点 : 500

問題文

1, \dots, N と番号づけられた N 個の都市と、1, \dots, M と番号づけられた M 本の道があります。
全ての道は一方通行であり、道 i \, (1 \leq i \leq M) を通ると、都市 A_i から都市 B_i へ移動することができます。また、道 i の長さは C_i です。

1 以上 M 以下の整数からなる長さ K の数列 E = (E_1, \dots, E_K) が与えられます。都市 1 から都市 N までいくつかの道を使って移動する方法であって、以下の条件を満たすものを良い経路と呼びます。

  • 通る道の番号を通った順番に並べた列は、E の部分列である。

なお、部分列とは、数列から 0 個以上の要素を削除し、残った要素を元の順序で並べて得られる数列のことを指します。

全ての良い経路における、通る道の長さの合計の最小値を求めてください。
ただし、良い経路が存在しない場合は、そのことを報告してください。

制約

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq M, K \leq 2 \times 10^5
  • 1 \leq A_i, B_i \leq N, A_i \neq B_i \, (1 \leq i \leq M)
  • 1 \leq C_i \leq 10^9 \, (1 \leq i \leq M)
  • 1 \leq E_i \leq M \, (1 \leq i \leq K)
  • 入力は全て整数

入力

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

N M K
A_1 B_1 C_1
\vdots
A_M B_M C_M
E_1 \ldots E_K

出力

全ての良い経路における、通る道の長さの合計の最小値を出力せよ。ただし、良い経路が存在しないならば、-1 を出力せよ。


入力例 1

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

出力例 1

4

良い経路として考えられるのは次の二つです。

  • 4 を使う。通る道の長さの合計は 5 である。
  • 1, 2 をこの順で使う。通る道の長さの合計は 2 + 2 = 4 である。

よって、求める最小値は 4 です。


入力例 2

3 2 3
1 2 1
2 3 1
2 1 1

出力例 2

-1

良い経路は存在しません。


入力例 3

4 4 5
3 2 2
1 3 5
2 4 7
3 4 10
2 4 1 4 3

出力例 3

14

Score : 500 points

Problem Statement

There are N towns numbered 1, \dots, N, and M roads numbered 1, \dots, M.
Every road is directed; road i (1 \leq i \leq M) leads you from Town A_i to Town B_i. The length of road i is C_i.

You are given a sequence E = (E_1, \dots, E_K) of length K consisting of integers between 1 and M. A way of traveling from town 1 to town N using roads is called a good path if:

  • the sequence of the roads' numbers arranged in the order used in the path is a subsequence of E.

Note that a subsequence of a sequence is a sequence obtained by removing 0 or more elements from the original sequence and concatenating the remaining elements without changing the order.

Find the minimum sum of the lengths of the roads used in a good path.
If there is no good path, report that fact.

Constraints

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq M, K \leq 2 \times 10^5
  • 1 \leq A_i, B_i \leq N, A_i \neq B_i \, (1 \leq i \leq M)
  • 1 \leq C_i \leq 10^9 \, (1 \leq i \leq M)
  • 1 \leq E_i \leq M \, (1 \leq i \leq K)
  • All values in the input are integers.

Input

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

N M K
A_1 B_1 C_1
\vdots
A_M B_M C_M
E_1 \ldots E_K

Output

Find the minimum sum of the lengths of the roads used in a good path.
If there is no good path, print -1.


Sample Input 1

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

Sample Output 1

4

There are two possible good paths as follows:

  • Using road 4. In this case, the sum of the length of the used road is 5.
  • Using road 1 and 2 in this order. In this case, the sum of the lengths of the used roads is 2 + 2 = 4.

Therefore, the desired minimum value is 4.


Sample Input 2

3 2 3
1 2 1
2 3 1
2 1 1

Sample Output 2

-1

There is no good path.


Sample Input 3

4 4 5
3 2 2
1 3 5
2 4 7
3 4 10
2 4 1 4 3

Sample Output 3

14
I - Shortest Path Query

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

配点 : 525

問題文

3N 列のグリッドが与えられます。上から i 行目、左から j 列目のマスをマス (i,j) と表します。マス (i,j) には S_{i,j}# ならば壁マスで、 . ならば空きマスであり通行可能です。

Q 個のクエリが与えられるので、順に処理してください。

各クエリでは整数 r,c が与えられるので、マス (r,c) の状態を反転させてください。つまり、マス (r,c) が壁マスならば空きマスにし、空きマスならば壁マスにしてください。その後、以下の問題の答えを出力してください。

マス (1,1) から上下左右に隣接する空きマスに移動する操作を繰り返してマス (3,N) に移動することを考えます。このとき、マス (3,N) に到達できるか判定し、到達できる場合は操作回数の最小値を求めてください。

制約

  • 2\le N\le 2\times 10^5
  • S_{i,j}# または .
  • S_{1,1}=S_{3,N}= .
  • 1\le Q\le 2\times 10^5
  • 1\le r\le 3
  • 1\le c\le N
  • (r,c) \neq (1,1),(3,N)
  • N,Q,r,c は整数

入力

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

N
S_{1,1}S_{1,2}\ldots S_{1,N}
S_{2,1}S_{2,2}\ldots S_{2,N}
S_{3,1}S_{3,2}\ldots S_{3,N}
Q
\text{query}_1
\text{query}_2
\vdots
\text{query}_Q

各クエリは以下の形式で与えられる。

r c

出力

Q 行出力せよ。

i 行目 (1\le i\le Q) には、 i 番目のクエリにおいてマス (1,1) からマス (3,N) に到達不可能ならば -1 を、到達可能ならば操作回数の最小値を出力せよ。


入力例 1

5
.#...
.#.#.
...#.
3
1 2
1 2
2 3

出力例 1

6
10
-1

1 つ目のクエリではマス (1,2) の状態を反転させます。その結果、各マスの状態は以下のようになります。

.....
.#.#.
...#.

このとき、マス (1,1) から順にマス (1,2),(1,3),(1,4),(1,5),(2,5),(3,5) と移動することで 6 回の操作でマス (3,5) に到達することができます。

2 つ目のクエリではマス (1,2) の状態を反転させます。その結果、各マスの状態は以下のようになります。

.#...
.#.#.
...#.

このとき、マス (1,1) から順にマス (2,1),(3,1),(3,2),(3,3),(2,3),(1,3),(1,4),(1,5),(2,5),(3,5) と移動することで 10 回の操作でマス (3,5) に到達することができます。

3 つ目のクエリではマス (2,3) の状態を反転させます。その結果、各マスの状態は以下のようになります。

.#...
.###.
...#.

このとき、どのように操作してもマス (1,1) からマス (3,5) に到達することはできません。


入力例 2

7
.#.....
.#..#..
...#...
6
2 5
3 4
3 5
2 5
1 4
1 4

出力例 2

10
8
10
12
-1
12

Score : 525 points

Problem Statement

You are given a grid with three rows and N columns. Denote the cell at the i-th row from the top and j-th column from the left as cell (i,j). Cell (i,j) is a wall cell if S_{i,j} is #, and an empty cell and passable if it is ..

You are given Q queries, which you should process in order.

Each query gives integers r and c, and you should flip the state of cell (r,c). That is, if cell (r,c) is a wall cell, make it an empty cell, and if it is an empty cell, make it a wall cell. Then, output the answer to the following problem:

Consider moving from cell (1,1) to cell (3,N) by repeatedly moving to an empty cell adjacent up, down, left, or right. Determine whether cell (3,N) is reachable, and if reachable, find the minimum number of moves.

Constraints

  • 2\le N\le 2\times 10^5
  • S_{i,j} is # or ..
  • S_{1,1}=S_{3,N}= .
  • 1\le Q\le 2\times 10^5
  • 1\le r\le 3
  • 1\le c\le N
  • (r,c) \neq (1,1),(3,N)
  • N,Q,r,c are integers.

Input

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

N
S_{1,1}S_{1,2}\ldots S_{1,N}
S_{2,1}S_{2,2}\ldots S_{2,N}
S_{3,1}S_{3,2}\ldots S_{3,N}
Q
\text{query}_1
\text{query}_2
\vdots
\text{query}_Q

Each query is given in the following format:

r c

Output

Print Q lines.

On the i-th line (1\le i\le Q), if cell (3,N) is unreachable from cell (1,1) in the i-th query, print -1; if reachable, print the minimum number of moves.


Sample Input 1

5
.#...
.#.#.
...#.
3
1 2
1 2
2 3

Sample Output 1

6
10
-1

In the first query, flip the state of cell (1,2). As a result, the state of each cell becomes:

.....
.#.#.
...#.

At this time, by moving from cell (1,1) through cells (1,2),(1,3),(1,4),(1,5),(2,5),(3,5) in order, you can reach cell (3,5) in six moves.

In the second query, flip the state of cell (1,2). As a result, the state of each cell becomes:

.#...
.#.#.
...#.

At this time, by moving from cell (1,1) through cells (2,1),(3,1),(3,2),(3,3),(2,3),(1,3),(1,4),(1,5),(2,5),(3,5) in order, you can reach cell (3,5) in ten moves.

In the third query, flip the state of cell (2,3). As a result, the state of each cell becomes:

.#...
.###.
...#.

At this time, no matter how you move, you cannot reach cell (3,5) from cell (1,1).


Sample Input 2

7
.#.....
.#..#..
...#...
6
2 5
3 4
3 5
2 5
1 4
1 4

Sample Output 2

10
8
10
12
-1
12