PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#BS0000477. Hành trình sưu tập dấu (Stamp Rally)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Hành trình sưu tập dấu (Stamp Rally)

Stamp Rally

Source: AtCoder

Version: Phuoc Hung OJ Extended

Problem Statement

Consider a connected undirected graph with NN vertices numbered from 11 to NN. Its MM edges are numbered in input order; edge ii joins aia_i and bib_i.

There are QQ pairs of brothers collecting stamps. In pair jj, the brothers begin at vertices xjx_j and yjy_j, respectively. Each may walk along graph edges. Together they must visit exactly zjz_j distinct vertices, including their starting vertices. Repeated visits to a vertex, even by both brothers, count only once.

The score of a journey is the highest number of any edge used by either brother. Determine the smallest achievable score for each pair.

Input

The first line contains integers N,MN,M, the numbers of vertices and edges.

The next MM lines give the edges: line ii contains integers ai,bia_i,b_i, the endpoints of edge ii.

The following line contains the integer QQ, the number of pairs.

Each of the final QQ lines contains integers xj,yj,zjx_j,y_j,z_j, the starting vertices and required number of distinct visited vertices for pair jj.

Output

Print QQ lines. Line jj must contain the minimum integer score for pair jj.

Subtasks

  • Subtask 1 (20 points): 3≤N≤303\le N\le 30; N−1≤M≤30N-1\le M\le 30; 1≤Q≤301\le Q\le 30; 1≤ai<bi≤N1\le a_i<b_i\le N for every 1≤i≤M1\le i\le M; 1≤xj<yj≤N1\le x_j<y_j\le N, 3≤zj≤N3\le z_j\le N for every 1≤j≤Q1\le j\le Q. The graph is connected; repeated edges are allowed.
  • Subtask 2 (30 points): 3≤N≤20003\le N\le 2000; N−1≤M≤2000N-1\le M\le 2000; 1≤Q≤20001\le Q\le 2000; 1≤ai<bi≤N1\le a_i<b_i\le N for every 1≤i≤M1\le i\le M; 1≤xj<yj≤N1\le x_j<y_j\le N, 3≤zj≤N3\le z_j\le N for every 1≤j≤Q1\le j\le Q. The graph is connected; repeated edges are allowed.
  • Subtask 3 (50 points): 3≤N≤1000003\le N\le 100000; N−1≤M≤100000N-1\le M\le 100000; 1≤Q≤1000001\le Q\le 100000; 1≤ai<bi≤N1\le a_i<b_i\le N for every 1≤i≤M1\le i\le M; 1≤xj<yj≤N1\le x_j<y_j\le N, 3≤zj≤N3\le z_j\le N for every 1≤j≤Q1\le j\le Q. The graph is connected; repeated edges are allowed.

Examples

Input

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

Output

1
2
4
3

Explanation

The graph contains five vertices and edges 1−21-2, 3−43-4, 2−32-3, 4−54-5 in that order.

  • For (1,3,3)(1,3,3), edge 11 allows the brother at vertex 11 to reach 22, while the other remains at 33. With no edge only two vertices are visited; the minimum score is 11.
  • For (1,3,4)(1,3,4), edge 22 also permits a visit to vertex 44. The first edge alone allows only three vertices, so the answer is 22.
  • For (1,3,5)(1,3,5), vertex 55 stays isolated until edge 44 is available. With that edge all five vertices can be visited, so the answer is 44.
  • For (1,2,3)(1,2,3), neither of the first two edges provides access to a third vertex from the starting vertices. Edge 33 gives access to vertex 33, so the answer is 33.