A - Majority

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 100

問題文

ある提案に対し、N 人の人が賛成か反対かを表明しています。なお、N は奇数です。

i \, (i = 1, 2, \dots, N) 番目の人の意見は文字列 S_i で表され、S_i = For のとき賛成しており、S_i = Against のとき反対しています。

過半数の人がこの提案に賛成しているかどうかを判定してください。

制約

  • N1 以上 99 以下の奇数
  • 全ての i = 1, 2, \dots, N に対し、S_i = For または S_i = Against

入力

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

N
S_1
S_2
\vdots
S_N

出力

N 人のうち過半数が提案に賛成しているならば Yes、そうでなければ No と出力せよ。


入力例 1

3
For
Against
For

出力例 1

Yes

提案に賛成している人数は 2 人であり、これは半数を超えているので Yes と出力します。


入力例 2

5
Against
Against
For
Against
For

出力例 2

No

提案に賛成している人数は 2 人であり、これは半数以下なので No と出力します。


入力例 3

1
For

出力例 3

Yes

Score : 100 points

Problem Statement

There are N people. Each of them agrees or disagrees with a proposal. Here, N is an odd number.

The i-th (i = 1, 2, \dots, N) person's opinion is represented by a string S_i: the person agrees if S_i = For and disagrees if S_i = Against.

Determine whether the majority agrees with the proposal.

Constraints

  • N is an odd number between 1 and 99, inclusive.
  • S_i = For or S_i = Against, for all i = 1, 2, \dots, N.

Input

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

N
S_1
S_2
\vdots
S_N

Output

Print Yes if the majority of the N people agree with the proposal; print No otherwise.


Sample Input 1

3
For
Against
For

Sample Output 1

Yes

The proposal is supported by two people, which is the majority, so Yes should be printed.


Sample Input 2

5
Against
Against
For
Against
For

Sample Output 2

No

The proposal is supported by two people, which is not the majority, so No should be printed.


Sample Input 3

1
For

Sample Output 3

Yes
B - Postal Card

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 200

問題文

数字のみからなる長さ 6 の文字列が N 個与えられます。i \, (i = 1, 2, \dots, N) 番目のものを S_i と表します。

さらに、数字のみからなる長さ 3 の文字列が M 個与えられます。j \, (j = 1, 2, \dots, M) 番目のものを T_j と表します。

S_1, S_2, \dots, S_N のうち、末尾 3 文字が T_1, T_2, \dots, T_M のいずれかに一致するものの個数を求めてください。

制約

  • 1 \leq N, M \leq 1000
  • N, M は整数
  • 全ての i = 1, 2, \dots, N に対し、S_i は数字のみからなる長さ 6 の文字列
  • 全ての j = 1, 2, \dots, M に対し、T_j は数字のみからなる長さ 3 の文字列

入力

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

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

出力

答えを出力せよ。


入力例 1

3 3
142857
004159
071028
159
287
857

出力例 1

2

S_1 の末尾 3 文字は 857 であり、これは T_3 に一致します。
S_2 の末尾 3 文字は 159 であり、これは T_1 に一致します。
S_3 の末尾 3 文字は 028 であり、これは T_1, T_2, T_3 のいずれにも一致しません。

以上から、答えは 2 です。


入力例 2

5 4
235983
109467
823476
592801
000333
333
108
467
983

出力例 2

3

入力例 3

4 4
000000
123456
987111
000000
000
111
999
111

出力例 3

3

Score : 200 points

Problem Statement

You are given N strings of length six each, consisting of digits. Let S_i be the i-th (i = 1, 2, \dots, N) of them.

You are also given M strings of length three each, consisting of digits. Let T_j be the j-th (j = 1, 2, \dots, M) of them.

Find the number of strings among S_1, S_2, \dots, S_N whose last three characters coincide with one or more of T_1, T_2, \dots, T_M.

Constraints

  • 1 \leq N, M \leq 1000
  • N and M are integers.
  • S_i is a string of length 6 consisting of digits, for all i = 1, 2, \dots, N.
  • T_j is a string of length 3 consisting of digits, for all j = 1, 2, \dots, M.

Input

The 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 the answer.


Sample Input 1

3 3
142857
004159
071028
159
287
857

Sample Output 1

2

The last three characters of S_1 are 857, which coincide with T_3.
The last three characters of S_2 are 159, which coincide with T_1.
The last three characters of S_3 are 028, which do not coincide with T_1, T_2, or T_3.

Thus, the answer is 2.


Sample Input 2

5 4
235983
109467
823476
592801
000333
333
108
467
983

Sample Output 2

3

Sample Input 3

4 4
000000
123456
987111
000000
000
111
999
111

Sample Output 3

3
C - Path Graph?

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 300

問題文

N 頂点 M 辺の単純無向グラフが与えられます。頂点には 1, 2, \dots, N の番号が、辺には 1, 2, \dots, M の番号が付けられています。
i \, (i = 1, 2, \dots, M) は頂点 u_i, v_i を結んでいます。

このグラフがパスグラフであるか判定してください。

単純無向グラフとは 単純無向グラフとは、自己ループや多重辺を含まず、辺に向きの無いグラフのことをいいます。

パスグラフとは 頂点に 1, 2, \dots, N の番号が付けられたN 頂点のグラフがパスグラフであるとは、(1, 2, \dots, N) を並べ変えて得られる数列 (v_1, v_2, \dots, v_N) であって、以下の条件を満たすものが存在することをいいます。
  • 全ての i = 1, 2, \dots, N-1 に対して、頂点 v_i, v_{i+1} を結ぶ辺が存在する
  • 整数 i, j1 \leq i, j \leq N, |i - j| \geq 2 を満たすならば、頂点 v_i, v_j を結ぶ辺は存在しない

制約

  • 2 \leq N \leq 2 \times 10^5
  • 0 \leq M \leq 2 \times 10^5
  • 1 \leq u_i, v_i \leq N \, (i = 1, 2, \dots, M)
  • 入力される値は全て整数
  • 入力で与えられるグラフは単純

入力

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

N M
u_1 v_1
u_2 v_2
\vdots
u_M v_M

出力

与えられたグラフがパスグラフなら Yes、そうでないなら No と出力せよ。


入力例 1

4 3
1 3
4 2
3 2

出力例 1

Yes

与えらえたグラフは下図のようであり、これはパスグラフです。

example_00


入力例 2

2 0

出力例 2

No

与えらえたグラフは下図のようであり、これはパスグラフではありません。

example_01


入力例 3

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

出力例 3

No

与えらえたグラフは下図のようであり、これはパスグラフではありません。

example_02

Score : 300 points

Problem Statement

You are given a simple undirected graph with N vertices and M edges. The vertices are numbered 1, 2, \dots, N, and the edges are numbered 1, 2, \dots, M.
Edge i \, (i = 1, 2, \dots, M) connects vertices u_i and v_i.

Determine if this graph is a path graph.

What is a simple undirected graph? A simple undirected graph is a graph without self-loops or multiple edges whose edges do not have a direction.

What is a path graph? A graph with N vertices numbered 1, 2, \dots, N is said to be a path graph if and only if there is a sequence (v_1, v_2, \dots, v_N) that is a permutation of (1, 2, \dots, N) and satisfies the following conditions:
  • For all i = 1, 2, \dots, N-1, there is an edge connecting vertices v_i and v_{i+1}.
  • If integers i and j satisfies 1 \leq i, j \leq N and |i - j| \geq 2, then there is no edge that connects vertices v_i and v_j.

Constraints

  • 2 \leq N \leq 2 \times 10^5
  • 0 \leq M \leq 2 \times 10^5
  • 1 \leq u_i, v_i \leq N \, (i = 1, 2, \dots, M)
  • All values in the input are integers.
  • The graph given in the input is simple.

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 Yes if the given graph is a path graph; print No otherwise.


Sample Input 1

4 3
1 3
4 2
3 2

Sample Output 1

Yes

Illustrated below is the given graph, which is a path graph.

example_00


Sample Input 2

2 0

Sample Output 2

No

Illustrated below is the given graph, which is not a path graph.

example_01


Sample Input 3

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

Sample Output 3

No

Illustrated below is the given graph, which is not a path graph.

example_02

D - Match or Not

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 400

問題文

英小文字と ? からなる文字列 S,T が与えられます。ここで、|S| \gt |T| が成り立ちます(文字列 X に対し、 |X|X の長さを表します)。

また、|X|=|Y| を満たす文字列 X,Y は、次の条件を満たすとき及びそのときに限りマッチするといいます。

  • X,Y に含まれる ? をそれぞれ独立に好きな英小文字に置き換えることで XY を一致させることができる

x=0,1,\ldots,|T| に対して次の問題を解いてください。

  • S の先頭の x 文字と末尾の |T|-x 文字を順番を保ったまま連結することで得られる長さ |T| の文字列を S' とする。S'T がマッチするならば Yes と、そうでなければ No と出力せよ。

制約

  • S,T は英小文字と ? からなる文字列
  • 1 \leq |T| \lt |S| \leq 3 \times 10^5

入力

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

S
T

出力

|T|+1 行出力せよ。
i 行目には x=i-1 に対する出力をせよ。


入力例 1

a?c
b?

出力例 1

Yes
No
No

x=0 の場合、S'?c となります。ここで、S'1 文字目の ?b に、T2 文字目の ?c に置き換えることで S'T を一致させることができるため、S'T はマッチします。このため、1 行目の出力は Yes です。
x=1,2 の場合は S' はそれぞれ aca? であり、T とマッチしません。このため、2,3 行目の出力は No です。


入力例 2

atcoder
?????

出力例 2

Yes
Yes
Yes
Yes
Yes
Yes

入力例 3

beginner
contest

出力例 3

No
No
No
No
No
No
No
No

Score : 400 points

Problem Statement

You are given strings S and T consisting of lowercase English letters and ?. Here, |S| \gt |T| holds (for a string X, |X| denotes the length of X).

Two strings X and Y such that |X|=|Y| is said to match if and only if:

  • one can make X equal Y by replacing each ? in X and Y with any English letter independently.

Solve the following problem for each x=0,1,\ldots,|T|:

  • Let S' be the string of length |T| obtained by concatenating the first x characters and the last (|T|-x) characters of S without changing the order. Print Yes if S' and T match, and No otherwise.

Constraints

  • S and T are strings consisting of lowercase English letters and ?.
  • 1 \leq |T| \lt |S| \leq 3 \times 10^5

Input

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

S
T

Output

Print (|T|+1) lines.
The i-th line should contain the answer for x=i-1.


Sample Input 1

a?c
b?

Sample Output 1

Yes
No
No

When x=0, S' equals ?c. Here, we can replace the 1-st character of S', ?, with b and the 2-nd character of T, ?, with c to make S' equal T, so S' and T match. Thus, Yes should be printed in the first line.
When x=1 and 2, respectively, S' is ac and a?, neither of which matches with T. Thus, No should be printed in the second and third lines.


Sample Input 2

atcoder
?????

Sample Output 2

Yes
Yes
Yes
Yes
Yes
Yes

Sample Input 3

beginner
contest

Sample Output 3

No
No
No
No
No
No
No
No
E - Karuta

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 500

問題文

英小文字からなる文字列が N 個与えられます。i \, (i = 1, 2, \dots, N) 番目のものを S_i と表します。

二つの文字列 x, y に対し、以下の条件を全て満たす最大の整数 n\mathrm{LCP}(x, y) と表します。

  • x, y の長さはいずれも n 以上
  • 1 以上 n 以下の全ての整数 i に対し、xi 文字目と yi 文字目が等しい

全ての i = 1, 2, \dots, N に対し、以下の値を求めてください。

  • \displaystyle \max_{i \neq j} \mathrm{LCP}(S_i, S_j)

制約

  • 2 \leq N \leq 5 \times 10^5
  • N は整数
  • S_i は英小文字からなる長さ 1 以上の文字列 (i = 1, 2, \dots, N)
  • S_i の長さの総和は 5 \times 10^5 以下

入力

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

N
S_1
S_2
\vdots
S_N

出力

N 行出力せよ。i \, (i = 1, 2, \dots, N) 行目には、\displaystyle \max_{i \neq j} \mathrm{LCP}(S_i, S_j) を出力せよ。


入力例 1

3
abc
abb
aac

出力例 1

2
2
1

\mathrm{LCP}(S_1, S_2) = 2, \mathrm{LCP}(S_1, S_3) = 1, \mathrm{LCP}(S_2, S_3) = 1 です。


入力例 2

11
abracadabra
bracadabra
racadabra
acadabra
cadabra
adabra
dabra
abra
bra
ra
a

出力例 2

4
3
2
1
0
1
0
4
3
2
1

Score : 500 points

Problem Statement

You are given N strings consisting of lowercase English letters. Let S_i be the i-th (i = 1, 2, \dots, N) of them.

For two strings x and y, \mathrm{LCP}(x, y) is defined to be the maximum integer n that satisfies all of the following conditions:

  • The lengths of x and y are both at least n.
  • For all integers i between 1 and n, inclusive, the i-th character of x and that of y are equal.

Find the following value for all i = 1, 2, \dots, N:

  • \displaystyle \max_{i \neq j} \mathrm{LCP}(S_i, S_j)

Constraints

  • 2 \leq N \leq 5 \times 10^5
  • N is an integer.
  • S_i is a string of length at least 1 consisting of lowercase English letters (i = 1, 2, \dots, N).
  • The sum of lengths of S_i is at most 5 \times 10^5.

Input

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

N
S_1
S_2
\vdots
S_N

Output

Print N lines. The i-th (i = 1, 2, \dots, N) line should contain \displaystyle \max_{i \neq j} \mathrm{LCP}(S_i, S_j).


Sample Input 1

3
abc
abb
aac

Sample Output 1

2
2
1

\mathrm{LCP}(S_1, S_2) = 2, \mathrm{LCP}(S_1, S_3) = 1, and \mathrm{LCP}(S_2, S_3) = 1.


Sample Input 2

11
abracadabra
bracadabra
racadabra
acadabra
cadabra
adabra
dabra
abra
bra
ra
a

Sample Output 2

4
3
2
1
0
1
0
4
3
2
1
F - Components

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 500

問題文

N 頂点の木があります。頂点には 1 から N までの番号が付いており、i 番目の辺は頂点 a_i と頂点 b_i を結んでいます。

x=1,2,\ldots,N に対して次の問題を解いてください。

  • 木の頂点の部分集合 V であって空でないものは 2^N-1 通り存在するが、そのうち V による誘導部分グラフの連結成分数が x であるようなものは何通りあるかを 998244353 で割った余りを求めよ。
誘導部分グラフとは S をグラフ G の頂点の部分集合とします。このとき、GS による誘導部分グラフとは、頂点集合が S で、辺集合が「G の辺であって両端が S に含まれるものすべて」であるようなグラフです。

制約

  • 1 \leq N \leq 5000
  • 1 \leq a_i \lt b_i \leq N
  • 与えられるグラフは木

入力

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

N
a_1 b_1
\vdots
a_{N-1} b_{N-1}

出力

N 行出力せよ。
i 行目には x=i に対する出力をせよ。


入力例 1

4
1 2
2 3
3 4

出力例 1

10
5
0
0

以下の 5 通りでは誘導部分グラフの連結成分数が 2、これら以外では 1 になります。

  • V = \{1,2,4\}
  • V = \{1,3\}
  • V = \{1,3,4\}
  • V = \{1,4\}
  • V = \{2,4\}

入力例 2

2
1 2

出力例 2

3
0

入力例 3

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

出力例 3

140
281
352
195
52
3
0
0
0
0

Score : 500 points

Problem Statement

You are given a tree with N vertices. The vertices are numbered 1 through N, and the i-th edge connects vertex a_i and vertex b_i.

Solve the following problem for each x=1,2,\ldots,N:

  • There are (2^N-1) non-empty subsets V of the tree's vertices. Find the number, modulo 998244353, of such V that the subgraph induced by V has exactly x connected components.
What is an induced subgraph? Let S be the subset of the vertices of a graph G; then the subgraph of G induced by S is a graph whose vertex set is S and whose edge set consists of all edges of G whose both ends are contained in S.

Constraints

  • 1 \leq N \leq 5000
  • 1 \leq a_i \lt b_i \leq N
  • The given graph is a tree.

Input

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

N
a_1 b_1
\vdots
a_{N-1} b_{N-1}

Output

Print N lines.
The i-th line should contain the answer for x=i.


Sample Input 1

4
1 2
2 3
3 4

Sample Output 1

10
5
0
0

The induced subgraph will have two connected components in the following five cases and one in the others.

  • V = \{1,2,4\}
  • V = \{1,3\}
  • V = \{1,3,4\}
  • V = \{1,4\}
  • V = \{2,4\}

Sample Input 2

2
1 2

Sample Output 2

3
0

Sample Input 3

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

Sample Output 3

140
281
352
195
52
3
0
0
0
0
G - Balance Update Query

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 600

問題文

高橋君は N 種類のカードを 10^{100} 枚ずつ持っています。はじめ、i 種類目のカードの得点は a_i で、使用可能枚数は b_i です。

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

  • 1 x y : x 種類目のカードの得点を y に設定
  • 2 x y : x 種類目のカードの使用可能枚数を y に設定
  • 3 x : 次の条件を満たすように x 枚のカードを選ぶことができるならば選ばれたカードの得点の総和の最大値を、そうでなければ -1 を出力
    • どの種類のカードもその使用可能枚数を超えて選ばれない

制約

  • 1 \leq N,Q \leq 2 \times 10^5
  • 0 \leq a_i \leq 10^9
  • 0 \leq b_i \leq 10^4
  • 1 種類目のクエリにおいて 1 \leq x \leq N, 0 \leq y \leq 10^9
  • 2 種類目のクエリにおいて 1 \leq x \leq N, 0 \leq y \leq 10^4
  • 3 種類目のクエリにおいて 1 \leq x \leq 10^9
  • 3 種類目のクエリが 1 個以上含まれる
  • 入力はすべて整数

入力

入力は以下の形式で標準入力から与えられる。ただし、\mathrm{query}_ii 番目のクエリを表す。

N
a_1 b_1
\vdots
a_N b_N
Q
\mathrm{query}_1
\vdots
\mathrm{query}_Q

出力

3 種類目のクエリの個数を M とする。M 行出力をせよ。
i 行目には i 番目の 3 種類目のクエリに対する出力をせよ。


入力例 1

3
1 1
2 2
3 3
7
3 4
1 1 10
3 4
2 1 0
2 3 0
3 4
3 2

出力例 1

11
19
-1
4

1 番目の 3 種類目のクエリでは、2 種類目のカードを 1 枚、3 種類目のカードを 3 枚選ぶことで得点の総和が 11 となり、これが最大です。
2 番目の 3 種類目のクエリでは、1 種類目のカードを 1 枚、3 種類目のカードを 3 枚選ぶことで得点の総和が 19 となり、これが最大です。
3 番目の 3 種類目のクエリでは、4 枚のカードを選ぶことができないため出力は -1 となります。
4 番目の 3 種類目のクエリでは、2 種類目のカードを 2 枚選ぶことで得点の総和が 4 となり、これが最大です。

Score : 600 points

Problem Statement

Takahashi has 10^{100} cards of each of N kinds. Initially, the score and quota of the i-th kind of card are set to a_i and b_i, respectively.

Given Q queries in the following formats, process them in order.

  • 1 x y: set the score of the x-th kind of card to y.
  • 2 x y: set the quota of the x-th kind of card to y.
  • 3 x: if one can choose x cards subject to the following condition, print the maximum possible sum of the scores of the chosen cards; print -1 otherwise.
    • The number of chosen cards of each kind does not exceed its quota.

Constraints

  • 1 \leq N,Q \leq 2 \times 10^5
  • 0 \leq a_i \leq 10^9
  • 0 \leq b_i \leq 10^4
  • For each query of the 1-st kind, 1 \leq x \leq N and 0 \leq y \leq 10^9.
  • For each query of the 2-nd kind, 1 \leq x \leq N and 0 \leq y \leq 10^4.
  • For each query of the 3-rd kind, 1 \leq x \leq 10^9.
  • There is at least one query of the 3-rd kind.
  • All values in the input are integers.

Input

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

N
a_1 b_1
\vdots
a_N b_N
Q
\mathrm{query}_1
\vdots
\mathrm{query}_Q

Output

Print M lines, where M is the number of queries of the 3-rd kind.
The i-th line should contain the response to the i-th such query.


Sample Input 1

3
1 1
2 2
3 3
7
3 4
1 1 10
3 4
2 1 0
2 3 0
3 4
3 2

Sample Output 1

11
19
-1
4

For the first query of the 3-rd kind, you can choose one card of the 2-nd kind and three cards of the 3-rd kind for a total score of 11, which is the maximum.
For the second such query, you can choose one card of the 1-st kind and three cards of the 3-rd kind for a total score of 19, which is the maximum.
For the third such query, you cannot choose four cards, so -1 should be printed.
For the fourth such query, you can choose two cards of the 2-nd kind for a total score of 4, which is the maximum.

Ex - Directed Graph and Query

Time Limit: 4.5 sec / Memory Limit: 1024 MB

配点 : 600

問題文

N 頂点 M 辺の有向グラフがあります。頂点には 1 から N までの番号が付いており、i 番目の有向辺は頂点 a_i から頂点 b_i へと結ばれています。

また、このグラフ上の経路について、コストを次のように定めます。

  • 経路上の頂点(始点・終点を含む)の番号の最大値

x=1,2,\ldots,Q に対して次の問題を解いてください。

  • 頂点 s_x から頂点 t_x への経路のコストの最小値を求めよ。ただし、そのような経路が一つも存在しない場合は代わりに -1 と出力せよ。

なお、入力の量が多くなる場合があるので、高速な方法で入出力を行うことを推奨します。

制約

  • 2 \leq N \leq 2000
  • 0 \leq M \leq N(N-1)
  • 1 \leq a_i,b_i \leq N
  • a_i \neq b_i
  • i \neq j ならば (a_i,b_i) \neq (a_j,b_j)
  • 1 \leq Q \leq 10^4
  • 1 \leq s_i,t_i \leq N
  • s_i \neq t_i
  • 入力はすべて整数

入力

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

N M
a_1 b_1
\vdots
a_M b_M
Q
s_1 t_1
\vdots
s_Q t_Q

出力

Q 行出力せよ。
i 行目には x=i に対する出力をせよ。


入力例 1

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

出力例 1

2
3
-1

x=1 に対しては、1 番目の辺を通って頂点 1 から頂点 2 へ行く経路のコストが 2 であり、これが最小です。
x=2 に対しては、2 番目の辺を通って頂点 2 から頂点 3 へ、そして 3 番目の辺を通って頂点 3 から頂点 1 へ行く経路のコストが 3 であり、これが最小です。
x=3 に対しては、頂点 1 から頂点 4 への経路が存在しないため -1 と出力します。

Score : 600 points

Problem Statement

There is a directed graph with N vertices and M edges. The vertices are numbered 1 through N, and the i-th directed edge goes from vertex a_i to vertex b_i.

The cost of a path on this graph is defined as:

  • the maximum index of a vertex on the path (including the initial and final vertices).

Solve the following problem for each x=1,2,\ldots,Q.

  • Find the minimum cost of a path from vertex s_x to vertex t_x. If there is no such path, print -1 instead.

The use of fast input and output methods is recommended because of potentially large input and output.

Constraints

  • 2 \leq N \leq 2000
  • 0 \leq M \leq N(N-1)
  • 1 \leq a_i,b_i \leq N
  • a_i \neq b_i
  • If i \neq j, then (a_i,b_i) \neq (a_j,b_j).
  • 1 \leq Q \leq 10^4
  • 1 \leq s_i,t_i \leq N
  • s_i \neq t_i
  • All values in the input are integers.

Input

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

N M
a_1 b_1
\vdots
a_M b_M
Q
s_1 t_1
\vdots
s_Q t_Q

Output

Print Q lines.
The i-th line should contain the answer for x=i.


Sample Input 1

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

Sample Output 1

2
3
-1

For x=1, the path via the 1-st edge from vertex 1 to vertex 2 has a cost of 2, which is the minimum.
For x=2, the path via the 2-nd edge from vertex 2 to vertex 3 and then via the 3-rd edge from vertex 3 to vertex 1 has a cost of 3, which is the minimum.
For x=3, there is no path from vertex 1 to vertex 4, so -1 should be printed.