#asaporoc. [asaporo_c]Graph

[asaporo_c]Graph

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 ≦ N ≦ 4,000
  • 1M400,0001 ≦ M ≦ 400,000
  • 1Q100,0001 ≦ Q ≦ 100,000
  • 1ai,bi,Si,TiN1 ≦ a_i,b_i,S_i,T_i ≦ N
  • 1ci1091 ≦ c_i ≦ 10^{9}
  • aineqbia_i \\neq b_i
  • SineqTiS_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 ≦ 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.


Sample Input 1

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

Sample Output 1

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.

Sample Input 2

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

Sample Output 2

8

This input satisfies the additional constraints for both partial scores.