A - 22222

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100

問題文

数字からなる文字列 S が与えられます。

S から 2 以外の文字を削除し、残った文字を順序を保って結合した文字列を求めてください。

制約

  • S は数字からなる長さ 1 以上 100 以下の文字列
  • S21 つ以上含む

入力

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

S

出力

答えを出力せよ。


入力例 1

20250222

出力例 1

22222

20250222 から 0, 5, 0 を削除し、残った文字を順序を保って結合することで文字列 22222 が得られます。


入力例 2

2

出力例 2

2

入力例 3

22222000111222222

出力例 3

22222222222

Score : 100 points

Problem Statement

You are given a string S consisting of digits.

Remove all characters from S except for 2, and then concatenate the remaining characters in their original order to form a new string.

Constraints

  • S is a string consisting of digits with length between 1 and 100, inclusive.
  • S contains at least one 2.

Input

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

S

Output

Print the answer.


Sample Input 1

20250222

Sample Output 1

22222

By removing 0, 5, and 0 from 20250222 and then concatenating the remaining characters in their original order, the string 22222 is obtained.


Sample Input 2

2

Sample Output 2

2

Sample Input 3

22222000111222222

Sample Output 3

22222222222
B - cat

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200

問題文

英小文字からなる N 個の文字列 S_1, S_2, \ldots, S_N が与えられます。ここで、文字列の長さはそれぞれ相異なります。

これらの文字列を長さの昇順に並べ替え、この順に結合して得られる文字列を求めてください。

制約

  • 2 \leq N \leq 50
  • N は整数
  • S_i は長さ 1 以上 50 以下の英小文字からなる文字列
  • i \neq j のとき S_iS_j の長さは異なる

入力

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

N
S_1
S_2
\vdots
S_N

出力

答えを出力せよ。


入力例 1

3
tc
oder
a

出力例 1

atcoder

( tc, oder, a ) を文字列の長さの昇順に並べ替えると ( a, tc, oder ) となります。これらの文字列を順に結合すると文字列 atcoder が得られます。


入力例 2

4
cat
enate
on
c

出力例 2

concatenate

Score : 200 points

Problem Statement

You are given N strings S_1, S_2, \ldots, S_N, each consisting of lowercase English letters. The lengths of these strings are all distinct.

Sort these strings in ascending order of length, and then concatenate them in that order to form a single string.

Constraints

  • 2 \leq N \leq 50
  • N is an integer.
  • Each S_i is a string consisting of lowercase English letters with length between 1 and 50, inclusive.
  • If i \neq j, the length of S_i is different from the length of S_j.

Input

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

N
S_1
S_2
\vdots
S_N

Output

Print the answer.


Sample Input 1

3
tc
oder
a

Sample Output 1

atcoder

When we sort (tc, oder, a) in ascending order of length, we get (a, tc, oder). Concatenating them in this order yields the string atcoder.


Sample Input 2

4
cat
enate
on
c

Sample Output 2

concatenate
C - Debug

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

英大文字のみからなる文字列 S が与えられます。
S に対して、次の手順を行った後に得られる文字列を出力してください。

文字列が WA を(連続する)部分文字列として含む限り、次の操作を繰り返す。

  • 文字列中に登場する WA のうち最も先頭のものを AC に置換する。

なお、本問題の制約下で、この操作は高々有限回しか繰り返されないことが証明できます。

制約

  • S は長さ 1 以上 3\times 10^5 以下の英大文字のみからなる文字列

入力

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

S

出力

S に対して、問題文中に記された手順を行った後に得られる文字列を出力せよ。


入力例 1

WACWA

出力例 1

ACCAC

最初、文字列は S=WACWA です。
この文字列には 1 文字目から 2 文字目、および、4 文字目から 5 文字目の 2 ヶ所に WA が部分文字列として含まれています。
1 回目の操作では、そのうち先頭のもの、すなわち 1 文字目から 2 文字目の部分を AC に置換し、文字列は ACCWA となります。
1 回目の操作後の文字列には 4 文字目から 5 文字目の 1 ヶ所にのみ WA が部分文字列として含まれているため、2 回目の操作ではこれを AC に置換し、文字列は ACCAC となります。
ACCACWA を部分文字列として含まないため、手順は終了します。よって、ACCAC を出力します。


入力例 2

WWA

出力例 2

ACC

最初、文字列は S=WWA です。
この文字列には 2 文字目から 3 文字目の 1 ヶ所にのみ WA が部分文字列として含まれているため、1 回目の操作ではこれを AC に置換し、文字列は WAC となります。
次に、1 回目の操作後の文字列は 1 文字目から 2 文字目の 1 ヶ所にのみ WA が部分文字列として含まれているため、2 回目の操作ではこれを AC に置換し、文字列は ACC となります。
ACCWA を部分文字列として含まないため、手順は終了します。よって、ACC を出力します。


入力例 3

WWWWW

出力例 3

WWWWW

S には最初から WA が部分文字列として含まれてないため、操作は 1 回も行われず手順は終了します。よって、WWWWW を出力します。

Score : 300 points

Problem Statement

You are given a string S consisting of uppercase English letters.
Apply the following procedure to S, and then output the resulting string:

As long as the string contains WA as a (contiguous) substring, repeat the following operation:

  • Among all occurrences of WA in the string, replace the leftmost one with AC.

It can be proved under the constraints of this problem that this operation is repeated at most a finite number of times.

Constraints

  • S is a string of uppercase English letters with length between 1 and 3\times 10^5, inclusive.

Input

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

S

Output

Print the resulting string after performing the procedure described in the problem statement on S.


Sample Input 1

WACWA

Sample Output 1

ACCAC

Initially, the string is S= WACWA.
This string contains WA as a substring in two places: from the 1st to the 2nd character, and from the 4th to the 5th character.
In the first operation, we replace the leftmost occurrence (the substring from the 1st to the 2nd character) with AC, resulting in ACCWA.
After the first operation, the string contains WA as a substring in exactly one place: from the 4th to the 5th character.
In the second operation, we replace it with AC, resulting in ACCAC.
Since ACCAC does not contain WA as a substring, the procedure ends. Therefore, we output ACCAC.


Sample Input 2

WWA

Sample Output 2

ACC

Initially, the string is S= WWA.
This string contains WA as a substring in exactly one place: from the 2nd to the 3rd character.
In the first operation, we replace it with AC, resulting in WAC.
Then, after the first operation, the string contains WA in exactly one place: from the 1st to the 2nd character.
In the second operation, we replace it with AC, resulting in ACC.
Since ACC does not contain WA as a substring, the procedure ends. Therefore, we output ACC.


Sample Input 3

WWWWW

Sample Output 3

WWWWW

Since S does not contain WA as a substring from the start, no operations are performed and the procedure ends immediately. Therefore, we output WWWWW.

D - Colorful Bracket Sequence

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 400

問題文

6 種類の文字、(, ), [, ], <, > からなる文字列 S が与えられます。

ここで、文字列 T は、以下の条件を満たすときカラフル括弧列と呼ばれます。

以下の操作を何回か(0 回でも良い)繰り返すことで、T を空文字列にできる。

  • T の(連続する)部分文字列であって、(), [], <> のいずれかであるようなものが存在するとき、そのうちの 1 つを選んで削除する。
  • 削除された部分文字列が T の先頭または末尾であるとき、残りの文字列を新たに T とする。
  • そうでないとき、削除された前後の文字列を 1 つに連結し、新たに T とする。

S がカラフル括弧列であるか判定してください。

制約

  • S は長さ 1 以上 2\times 10^5 以下の文字列
  • S(, ), [, ], <, > のみからなる。

入力

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

S

出力

S がカラフル括弧列ならば Yes を、そうでないならば No を出力せよ。


入力例 1

([])<>()

出力例 1

Yes

S=([])<>() に対して、次のように操作を繰り返すことで空文字列にすることができます。

  • ([])<>()2 文字目から 3 文字目までをとった部分文字列 [] を削除し、前後を連結する。文字列は新たに ()<>() となる。
  • ()<>()1 文字目から 2 文字目までをとった部分文字列 () を削除する。文字列は新たに <>() となる。
  • <>()1 文字目から 2 文字目までをとった部分文字列 <> を削除する。文字列は新たに () となる。
  • ()1 文字目から 2 文字目までをとった部分文字列 () を削除する。文字列は空文字列となる。

よって、S=([])<>() はカラフル括弧列であるため、Yes を出力します。


入力例 2

([<)]>

出力例 2

No

S=([<)]>(), [], <> を部分列として含まないため、1 回目の操作を行うことができず、特に S はカラフル括弧列ではありません。よって、No を出力します。


入力例 3

())

出力例 3

No

S に対して操作を繰り返し、空文字列とすることはできません。
よって、S はカラフル括弧列ではないため、No を出力します。

Score : 400 points

Problem Statement

You are given a string S consisting of six types of characters: (, ), [, ], <, >.

A string T is called a colorful bracket sequence if it satisfies the following condition:

It is possible to turn T into an empty string by repeating the following operation any number of times (possibly zero):

  • If there exists a contiguous substring of T that is one of (), [], or <>, choose one such substring and delete it.
  • If the deleted substring was at the beginning or end of T, the remainder becomes the new T.
  • Otherwise, concatenate the part before the deleted substring and the part after the deleted substring, and that becomes the new T.

Determine whether S is a colorful bracket sequence.

Constraints

  • S is a string of length between 1 and 2\times 10^5, inclusive.
  • S consists of (, ), [, ], <, >.

Input

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

S

Output

If S is a colorful bracket sequence, print Yes; otherwise, print No.


Sample Input 1

([])<>()

Sample Output 1

Yes

For S=([])<>(), it is possible to turn it into an empty string by repeating the operation as follows:

  • Delete the substring [] from the 2nd to the 3rd character in ([])<>(), then concatenate the parts before and after it. The string becomes ()<>().
  • Delete the substring () from the 1st to the 2nd character in ()<>(). The string becomes <>().
  • Delete the substring <> from the 1st to the 2nd character in <>(). The string becomes ().
  • Delete the substring () from the 1st to the 2nd character in (). The string becomes empty.

Thus, S=([])<>() is a colorful bracket sequence, so print Yes.


Sample Input 2

([<)]>

Sample Output 2

No

Since S=([<)]> does not contain (), [], or <> as a contiguous substring, we cannot perform the 1st operation, and in particular S is not a colorful bracket sequence. Therefore, print No.


Sample Input 3

())

Sample Output 3

No

It is impossible to turn S into an empty string by repeating the operations.
Therefore, S is not a colorful bracket sequence, so print No.

E - Palindromic Shortest Path

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 450

問題文

N 頂点の有向グラフがあり、頂点には 1, 2, \ldots, N の番号が付いています。

辺の情報は N^2 個の文字 C_{1, 1}, C_{1, 2}, \ldots, C_{1, N}, C_{2, 1}, \ldots, C_{N, N} によって与えられます。ここで、C_{i, j} は英小文字あるいは - です。

C_{i, j} が英小文字のとき頂点 i から頂点 j へ向かう辺がちょうど 1 つ存在してラベル C_{i, j} が付いており、C_{i, j}- のとき頂点 i から頂点 j へ向かう辺は存在しません。

1 \leq i, j \leq N を満たす各整数組 (i, j) について、以下の問題に対する答えを求めてください。

  • 頂点 i から頂点 j に向かう(単純とは限らない)パスのうち、辺に付けられたラベルの文字を順に結合した文字列が回文となるようなものの中で最も短いものの長さを求めよ。ただし、そのようなパスがない場合は答えは -1 とせよ。

制約

  • 1 \leq N \leq 100
  • N は整数
  • C_{i, j} は英小文字あるいは -

入力

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

N
C_{1, 1}C_{1, 2}\ldotsC_{1, N}
C_{2, 1}C_{2, 2}\ldotsC_{2, N}
\vdots
C_{N, 1}C_{N, 2}\ldotsC_{N, N}

出力

整数組 (i, j) に対する答えを A_{i, j} として以下の形式で出力せよ。

A_{1, 1} A_{1, 2} \ldots A_{1, N}
A_{2, 1} A_{2, 2} \ldots A_{2, N}
\vdots
A_{N, 1} A_{N, 2} \ldots A_{N, N}

入力例 1

4
ab--
--b-
---a
c---

出力例 1

0 1 2 4
-1 0 1 -1
3 -1 0 1
1 -1 -1 0

例として (i, j) = (1, 4) の場合について説明します。

頂点 1 \to 1 \to 2 \to 3 \to 4 というパスにおける辺に付けられた文字のラベルを順に結合すると文字列 abba が得られます。これは回文であり、頂点 1 から頂点 4 へ向かう長さ 3 以下のパスであって辺に付けられたラベルの文字を順に結合した文字列が回文となるものは存在しないため (i, j) = (1, 4) に対する答えは 4 となります。

空文字列も回文であることに注意してください。


入力例 2

5
us---
-st--
--s--
u--s-
---ts

出力例 2

0 1 3 -1 -1
-1 0 1 -1 -1
-1 -1 0 -1 -1
1 3 -1 0 -1
-1 -1 5 1 0

Score : 450 points

Problem Statement

We have a directed graph with N vertices, numbered 1, 2, \ldots, N.

Information about the edges is given by N^2 characters C_{1, 1}, C_{1, 2}, \ldots, C_{1, N}, C_{2, 1}, \ldots, C_{N, N}. Here, each C_{i, j} is either a lowercase English letter or -.

If C_{i, j} is a lowercase English letter, then there is exactly one directed edge from vertex i to vertex j labeled C_{i, j}. If C_{i, j} is -, there is no edge from vertex i to vertex j.

For each integer pair (i, j) with 1 \leq i, j \leq N, answer the following question:

  • Among all (not necessarily simple) paths from vertex i to vertex j whose concatenation of labels on the edges forms a palindrome, what is the length of the shortest such path? If there is no such path, the answer is -1.

Constraints

  • 1 \leq N \leq 100
  • N is an integer.
  • Each C_{i, j} is either a lowercase English letter or -.

Input

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

N
C_{1, 1}C_{1, 2}\ldotsC_{1, N}
C_{2, 1}C_{2, 2}\ldotsC_{2, N}
\vdots
C_{N, 1}C_{N, 2}\ldotsC_{N, N}

Output

Let A_{i, j} be the answer to the question for the pair (i, j). Print them in the following format:

A_{1, 1} A_{1, 2} \ldots A_{1, N}
A_{2, 1} A_{2, 2} \ldots A_{2, N}
\vdots
A_{N, 1} A_{N, 2} \ldots A_{N, N}

Sample Input 1

4
ab--
--b-
---a
c---

Sample Output 1

0 1 2 4
-1 0 1 -1
3 -1 0 1
1 -1 -1 0

For example, consider the case (i, j) = (1, 4).
By taking the path 1 \to 1 \to 2 \to 3 \to 4, and concatenating the labels on its edges in order, we get the string abba, which is a palindrome.
There is no path of length at most 3 from vertex 1 to vertex 4 whose concatenation of labels is a palindrome. Thus, the answer for (1, 4) is 4.

Note that the empty string is also a palindrome.


Sample Input 2

5
us---
-st--
--s--
u--s-
---ts

Sample Output 2

0 1 3 -1 -1
-1 0 1 -1 -1
-1 -1 0 -1 -1
1 3 -1 0 -1
-1 -1 5 1 0
F - Alkane

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 500

問題文

N 頂点の無向木 T が与えられます。頂点には 1, 2, \ldots, N の番号が付いており、i 番目の辺は頂点 A_i と頂点 B_i を結ぶ無向辺です。

グラフがアルカンであるとは以下の条件をともに満たしていることであると定義します。

  • グラフは無向木である
  • すべての頂点の次数が 1 または 4 であり、次数 4 の頂点が 1 つ以上存在する

T の部分グラフであってアルカンであるものが存在するか判定し、存在する場合はそのようなものの頂点数の最大値を求めてください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq A_i, B_i \leq N
  • 与えられるグラフは無向木
  • 入力される値はすべて整数

入力

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

N
A_1 B_1
A_2 B_2
\vdots
A_{N - 1} B_{N - 1}

出力

T の部分グラフであってアルカンであるものが存在する場合はそのようなものの頂点数の最大値を出力せよ。そうでない場合は -1 と出力せよ。


入力例 1

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

出力例 1

8

頂点 u と頂点 v を結ぶ無向辺を辺 (u, v) と表記します。

頂点 1,2,3,4,6,7,8,9、辺 (1,2),(2,3),(3,4),(2,6),(2,7),(3,8),(3,9) からなる部分グラフはアルカンです。


入力例 2

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

出力例 2

-1

入力例 3

15
8 5
2 9
1 12
6 11
9 3
15 1
7 12
7 13
10 5
6 9
5 1
1 9
4 5
6 14

出力例 3

11

Score : 500 points

Problem Statement

You are given an undirected tree T with N vertices, numbered 1, 2, \ldots, N. The i-th edge is an undirected edge connecting vertices A_i and B_i.

A graph is defined to be an alkane if and only if it satisfies the following conditions:

  • The graph is an undirected tree.
  • Every vertex has degree 1 or 4, and there is at least one vertex of degree 4.

Determine whether there exists a subgraph of T that is an alkane, and if so, find the maximum number of vertices in such a subgraph.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq A_i, B_i \leq N
  • The given graph is an undirected tree.
  • All input values are integers.

Input

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

N
A_1 B_1
A_2 B_2
\vdots
A_{N - 1} B_{N - 1}

Output

If there exists a subgraph of T that is an alkane, print the maximum number of vertices in such a subgraph. Otherwise, print -1.


Sample Input 1

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

Sample Output 1

8

Let (u, v) denote an undirected edge between vertices u and v.

A subgraph consisting of vertices 1,2,3,4,6,7,8,9 and edges (1,2),(2,3),(3,4),(2,6),(2,7),(3,8),(3,9) is an alkane.


Sample Input 2

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

Sample Output 2

-1

Sample Input 3

15
8 5
2 9
1 12
6 11
9 3
15 1
7 12
7 13
10 5
6 9
5 1
1 9
4 5
6 14

Sample Output 3

11
G - Dense Buildings

Time Limit: 5 sec / Memory Limit: 1024 MiB

配点 : 600

問題文

東西南北方向に H\times W 個の区画に区切られた街があり、各区画にはちょうど 1 つのビルが立っています。
具体的には北から i 番目 (1\leq i\leq H) かつ西から j 番目 (1\leq j\leq W) の区画(以下、区画 (i,j) とよぶ)には F_{i,j} 階建てのビルがあります。

高橋君には 2 種類の移動方法があり、区画 (i,j) のビルの X(1\leq X\leq F_{i,j}) にいるとき、そこから次のような移動が可能です。

  • 同じビルの中の 1 つ上の階または 1 つ下の階へ 階段 を使って移動する。ただし、1 階から 1 つ下の階への移動および、F_{i,j} 階から 1 つ上の階への移動はできない。
  • 東西南北に隣接する区画にあるビルのうち、 X 階建て以上であるものを 1 つを選び、そのビルの X 階へ (上空)通路 を使って移動する。

ただし、区画 (i,j) と区画 (i',j') が東西南北に隣接するとは、\lvert i-i'\rvert+\lvert j-j'\rvert=1 であることをさします。

Q 個の質問が与えられるので、それぞれの質問に答えてください。i 個目 (1\leq i\leq Q) の質問は次のようなものです。

区画 (A_i,B_i) にあるビルの Y_i 階から、区画 (C_i,D_i) にあるビルの Z_i 階へ高橋君が移動する時に、 階段 を使う回数としてあり得る最小の値を求めよ。
ここで、階段を使う回数は 1 つ上の階または 1 つ下の階へ移動するたびにカウントされ、同じビルの中でも重複してカウントされるものとする。 (例えば、あるビルを 1 階から 6 階まで移動したとき、5 回階段を使用したと数える。)
また、通路の使用回数を最小とする必要はないことに注意せよ。

制約

  • 1\leq H \leq 500
  • 1\leq W \leq 500
  • 1\leq F_{i,j} \leq 10^6
  • 1\leq Q\leq 2\times 10^5
  • 1\leq A_i,C_i\leq H
  • 1\leq B_i,D_i\leq W
  • 1\leq Y_i\leq F_{A_i,B_i}
  • 1\leq Z_i\leq F_{C_i,D_i}
  • (A_i,B_i,Y_i)\neq (C_i,D_i,Z_i)
  • 入力はすべて整数

入力

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

H W
F_{1,1} F_{1,2} \ldots F_{1,W}
F_{2,1} F_{2,2} \ldots F_{2,W}
\vdots
F_{H,1} F_{H,2} \ldots F_{H,W}
Q
A_1 B_1 Y_1 C_1 D_1 Z_1
A_2 B_2 Y_2 C_2 D_2 Z_2
\vdots
A_Q B_Q Y_Q C_Q D_Q Z_Q

出力

Q 行出力せよ。i 行目には i 番目の質問に対する答えを整数で出力せよ。


入力例 1

3 3
12 10 6
1 1 3
8 6 7
2
1 1 10 3 1 6
1 1 6 1 2 4

出力例 1

10
2

1 番目の質問について、例えば以下のように移動すると階段を合計で 10 回使用して、 区画 (1,1) のビルの 10 階から区画 (3,1) のビルの 6 階へ移動することができます。

  • 通路を使って、区画 (1,1) のビルの 10 階から区画 (1,2) のビルの 10 階へ移動する。
  • 階段を 4 回使って、区画 (1,2) のビルの 10 階から 6 階へ移動する。
  • 通路を使って、区画 (1,2) のビルの 6 階から区画 (1,3) のビルの 6 階へ移動する。
  • 階段を 3 回使って、区画 (1,3) のビルの 6 階から 3 階へ移動する。
  • 通路を使って、区画 (1,3) のビルの 3 階から区画 (2,3) のビルの 3 階へ移動する。
  • 通路を使って、区画 (2,3) のビルの 3 階から区画 (3,3) のビルの 3 階へ移動する。
  • 階段を 3 回使って、区画 (3,3) のビルの 3 階から 6 階へ移動する。
  • 通路を使って、区画 (3,3) のビルの 6 階から区画 (3,2) のビルの 6 階へ移動する。
  • 通路を使って、区画 (3,2) のビルの 6 階から区画 (3,1) のビルの 6 階へ移動する。

階段の使用回数が 9 回以下となるように、区画 (1,1) のビルの 10 階から区画 (3,1) のビルの 6 階へ移動することはできないため、10 を出力します。

2 番目の質問については、区画 (1,2) のビルへ通路を用いて移動した後に、階段を 2 回用いて 6 階から 4 階へ下りると、階段を 2 回使用することで、区画 (1,1) のビルの 6 階から区画 (1,2) のビルの 4 階へ移動することができます。

Score : 600 points

Problem Statement

There is a city divided into H \times W blocks in the north-south-east-west directions, and there is exactly one building in each block.
Specifically, in the block at the i-th row from the north (1\leq i\leq H) and the j-th column from the west (1\leq j\leq W) (hereafter referred to as block (i,j)), there is a building of F_{i,j} floors.

Takahashi has two ways of moving. If he is on the X-th floor (1\leq X\leq F_{i,j}) of the building in block (i,j), he can:

  • Move up or down one floor within the same building using stairs. If X=1, he cannot move down; if X=F_{i,j}, he cannot move up.
  • Choose a building with at least X floors in a cardinally adjacent block, and move to the X-th floor of that building using a (sky) walkway.

Here, two blocks (i,j) and (i',j') are cardinally adjacent if and only if \lvert i - i'\rvert + \lvert j - j'\rvert = 1.

You are given Q queries to be answered. The i-th query (1\leq i\leq Q) is the following.

Find the minimum possible number of times that Takahashi uses stairs to move from the Y_i-th floor of the building in block (A_i,B_i) to the Z_i-th floor of the building in block (C_i,D_i).
The count of times using stairs is incremented each time he moves up or down one floor, possibly multiple times within the same building. (For example, moving from the 1st floor to the 6th floor of a building counts as 5 uses of stairs.)
Note that he does not have to minimize the number of times he uses walkways.

Constraints

  • 1\leq H \leq 500
  • 1\leq W \leq 500
  • 1\leq F_{i,j} \leq 10^6
  • 1\leq Q\leq 2\times 10^5
  • 1\leq A_i,C_i\leq H
  • 1\leq B_i,D_i\leq W
  • 1\leq Y_i\leq F_{A_i,B_i}
  • 1\leq Z_i\leq F_{C_i,D_i}
  • (A_i,B_i,Y_i)\neq (C_i,D_i,Z_i)
  • All input values are integers.

Input

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

H W
F_{1,1} F_{1,2} \ldots F_{1,W}
F_{2,1} F_{2,2} \ldots F_{2,W}
\vdots
F_{H,1} F_{H,2} \ldots F_{H,W}
Q
A_1 B_1 Y_1 C_1 D_1 Z_1
A_2 B_2 Y_2 C_2 D_2 Z_2
\vdots
A_Q B_Q Y_Q C_Q D_Q Z_Q

Output

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


Sample Input 1

3 3
12 10 6
1 1 3
8 6 7
2
1 1 10 3 1 6
1 1 6 1 2 4

Sample Output 1

10
2

For the first query, for example, it is possible to move from the 10th floor of the building in block (1,1) to the 6th floor of the building in block (3,1) by using stairs a total of 10 times, in the following manner:

  • Move from the 10th floor of the building in block (1,1) to the 10th floor of the building in block (1,2) via a walkway.
  • Use stairs 4 times to go from the 10th floor down to the 6th floor of the building in block (1,2).
  • Move from the 6th floor of the building in block (1,2) to the 6th floor of the building in block (1,3) via a walkway.
  • Use stairs 3 times to go from the 6th floor down to the 3rd floor of the building in block (1,3).
  • Move from the 3rd floor of the building in block (1,3) to the 3rd floor of the building in block (2,3) via a walkway.
  • Move from the 3rd floor of the building in block (2,3) to the 3rd floor of the building in block (3,3) via a walkway.
  • Use stairs 3 times to go from the 3rd floor up to the 6th floor of the building in block (3,3).
  • Move from the 6th floor of the building in block (3,3) to the 6th floor of the building in block (3,2) via a walkway.
  • Move from the 6th floor of the building in block (3,2) to the 6th floor of the building in block (3,1) via a walkway.

It is impossible to make this journey using at most 9 uses of stairs, so we output 10.

For the second query, if you first use a walkway to go to the building in block (1,2), and then use the stairs twice to go from the 6th floor down to the 4th floor, it is possible to move from the 6th floor of the building in block (1,1) to the 4th floor of the building in block (1,2) by using the stairs twice.