atcoder#ASAPOROC. グラフ

グラフ

Score : 700700 points

Problem Statement

Takahashi found an undirected connected graph with NN vertices and MM edges. The vertices are numbered 11 through NN. The ii-th edge connects vertices aia_i and bib_i, and has a weight of cic_i.

He will play QQ rounds of a game using this graph. In the ii-th round, two vertices SiS_i and TiT_i are specified, and he will choose a subset of the edges such that any vertex can be reached from at least one of the vertices SiS_i or TiT_i by traversing chosen edges.

For each round, find the minimum possible total weight of the edges chosen by Takahashi.

Constraints

  • 1N4,0001 \leq N \leq 4,000
  • 1M400,0001 \leq M \leq 400,000
  • 1Q100,0001 \leq Q \leq 100,000
  • 1ai,bi,Si,TiN1 \leq a_i,b_i,S_i,T_i \leq N
  • 1ci1091 \leq c_i \leq 10^{9}
  • aibia_i \neq b_i
  • SiTiS_i \neq T_i
  • The given graph is connected.

Partial Scores

  • In the test set worth 200200 points, Q=1Q = 1.
  • In the test set worth another 300300 points, Q3000Q \leq 3000.

Input

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

NN MM

a1a_1 b1b_1 c1c_1

a2a_2 b2b_2 c2c_2

:

aMa_M bMb_M cMc_M

QQ

S1S_1 T1T_1

S2S_2 T2T_2

:

SQS_Q TQT_Q

Output

Print QQ lines. The ii-th line should contain the minimum possible total weight of the edges chosen by Takahashi.

4 3
1 2 3
2 3 4
3 4 5
2
2 3
1 4
8
7

We will examine each round:

  • In the 11-st round, choosing edges 11 and 33 achieves the minimum total weight of 88.
  • In the 22-nd round, choosing edges 11 and 22 achieves the minimum total weight of 77.
4 6
1 3 5
4 1 10
2 4 6
3 2 2
3 4 5
2 1 3
1
2 3
8

This input satisfies the additional constraints for both partial scores.