#d1904. 【模板】最小瓶颈生成树(数据加强版)

【模板】最小瓶颈生成树(数据加强版)

[Template] Minimum Bottleneck Spanning Tree (Hard Version)

Problem Description

You are given an undirected graph with nn vertices and mm edges, each edge having a weight. Define the bottleneck of a path from vertex ii to vertex jj as the maximum weight on that path. There are qq queries; each query gives two vertices (s,t)(s, t). For each query, find the minimum bottleneck over all paths from ss to tt.

It is guaranteed that the graph is connected.

Input Format

The first line contains two positive integers nn (1≤n≤1051 \le n \le 10^5) and mm (1≤m≤min⁡(106,n×(n−1)2)1 \le m \le \min(10^6, \frac{n \times (n-1)}{2})), the numbers of vertices and edges.

Lines 22 to m+1m+1 each contain three integers x,y,dx, y, d (0≤d≤1090 \le d \le 10^9), denoting an undirected edge of weight dd between xx and yy.

Line m+2m+2 contains a positive integer qq (1≤q≤1061 \le q \le 10^6), the number of queries. The following qq lines each contain two positive integers s,ts, t (1≤s,t≤n1 \le s, t \le n), the start and end vertices.

Output Format

For each query, output one integer on its own line: the minimum bottleneck.

Note: Because the input and output sizes of this problem are very large, please use fast I/O.

Sample Input

2 1
1 2 10
1
1 2

Sample Output

10