QOJ.ac

QOJ

时间限制: 1 s 内存限制: 512 MB 总分: 100

#907. Kaleidoscope

统计

There was originally an undirected weighted graph $G$. Xiao Ai placed it into a kaleidoscope to observe it, which caused $G$ to appear as the union of $n$ copies generated by its rotation. Specifically, for every edge $(u, v, w) \in G$, all edges $((u+i) \bmod n + 1, (v+i) \bmod n + 1, w)$ are generated in $H$. The final graph $H$ is the result seen in the kaleidoscope.

Xiao Ai wants to know the sum of the weights of the minimum spanning tree of this graph. Please help her.

Input

The input contains multiple test cases. The first line contains a positive integer $T$, representing the number of test cases.

For each test case:

  • The next line contains two positive integers $n, m$.
  • The next $m$ lines each contain three positive integers $u, v, w$, representing an edge.

Output

Output $T$ lines, where the $i$-th line represents the answer to the $i$-th query.

Examples

Input 1

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

Output 1

4
3

Subtasks

For $100\%$ of the data, it is guaranteed that $1 \le T \le 100$; $1 \le n, w \le 10^9$; $\sum m \le 10^5$. It is guaranteed that a spanning tree exists.

Subtask 1 (30pts): $n, m \le 10$.

Subtask 2 (20pts): $\sum n \cdot m \le 10^5$.

Subtask 3 (20pts): $w \le 2$.

Subtask 4 (30pts): No special restrictions.

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.