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

#FW0000086. Giao thông vương quốc AtCoder (AtCoder Kingdom’s Transport)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Giao thông vương quốc AtCoder (AtCoder Kingdom’s Transport)

AtCoder Kingdom’s Transport

Source: AtCoder

Version: Phuoc Hung OJ Extended

Problem Statement

A connected undirected positive-weight road network receives qq new roads, possibly parallel. After each road is built, output the sum of shortest distances over all unordered city pairs i<ji<j.

Input

First n,mn,m, mm undirected roads uvwu v w, then qq and qq new roads $u v w`, all 1-based.

Output

Print qq totals, one per line.

Subtasks

  • Subtask 1 (40 points): 2≤n≤7, 1≤q≤62\le n\le7,\ 1\le q\le6. n−1≤m≤n(n−1)/2n-1\le m\le n(n-1)/2; mạng ban đầu liên thông, trọng số dương ≤106\le10^6, không có self-loop.

  • Subtask 2 (60 points): 2≤n≤24, 1≤q≤102\le n\le24,\ 1\le q\le10. n−1≤m≤n(n−1)/2n-1\le m\le n(n-1)/2; mạng ban đầu liên thông, trọng số dương ≤106\le10^6, không có self-loop.

Examples

Input

3 2
1 2 3
2 3 4
2
1 3 1
1 2 10

Output

8
8

Explanation

Original pairwise distances sum to 14. Adding road 1–3 of length 1 lowers the sum to 8; the subsequent road of length 10 offers no improvement.