atcoder#AGC002D. [AGC002D] Stamp Rally

[AGC002D] Stamp Rally

Problem Statement

We have an undirected graph with NN vertices and MM edges. The vertices are numbered 11 through NN, and the edges are numbered 11 through MM. Edge ii connects vertices aia_i and bib_i. The graph is connected.

On this graph, QQ pairs of brothers are participating in an activity called Stamp Rally. The Stamp Rally for the ii-th pair will be as follows:

  • One brother starts from vertex xix_i, and the other starts from vertex yiy_i.
  • The two explore the graph along the edges to visit ziz_i vertices in total, including the starting vertices. Here, a vertex is counted only once, even if it is visited multiple times, or visited by both brothers.
  • The score is defined as the largest index of the edges traversed by either of them. Their objective is to minimize this value.

Find the minimum possible score for each pair.

Constraints

  • 3N1053 \leq N \leq 10^5
  • N1M105N-1 \leq M \leq 10^5
  • $1 \leq a_i
  • The given graph is connected.
  • 1Q1051 \leq Q \leq 10^5
  • $1 \leq x_j
  • 3zjN3 \leq z_j \leq N

Input

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

NN MM

a1a_1 b1b_1

a2a_2 b2b_2

::

aMa_M bMb_M

QQ

x1x_1 y1y_1 z1z_1

x2x_2 y2y_2 z2z_2

::

xQx_Q yQy_Q zQz_Q

Output

Print QQ lines. The ii-th line should contain the minimum possible score for the ii-th pair of brothers.

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