Under construction

Still being built. Things may change

Cyclic Distance

Time limit3sMemory limit512 MB

Summary
Choose k distinct vertices of a weighted tree to maximize the total tree distance along the cycle p1 to pk and back to p1, and output that maximum.
Level

Hard9 of 10

Topics
Tree, Dynamic programming, Binary search
Solved
No attempts yet

Problem

The technique from the problem 'Alien' turns a naive O(nk) dynamic programming solution into O(n log n). More problems that use this technique would make contests better, like the two unsolved problems in 300iq Contest 2.

You are given a weighted tree with nn vertices and n−1n-1 edges. The ii-th edge connects vertices uiu_i and viv_i and has length lil_i. Let dis(u,v)dis(u, v) be the distance between vertices uu and vv, which is the sum of the weights on the simple path between them.

Choose kk distinct vertices p1,p2,…,pkp_1, p_2, \ldots, p_k that maximize ∑dis(pi,p(i mod k)+1)\sum dis(p_i, p_{(i \bmod k)+1}). Output the maximum sum.

Input

The first line contains an integer TT (1≤T≤1051 \le T \le 10^5), the number of test cases. Each test case is given as follows.

The first line contains two integers nn and kk (2≤n≤2⋅1052 \le n \le 2 \cdot 10^5, 2≤k≤n2 \le k \le n).

Each of the following n−1n-1 lines contains three integers uiu_i, viv_i, and lil_i (1≤ui,vi≤n1 \le u_i, v_i \le n, 1≤li≤1061 \le l_i \le 10^6).

It is guaranteed that ∑n≤2⋅105\sum n \le 2 \cdot 10^5.

Output

For each test case, output one line containing one integer: the answer.

Examples1

  1. Example 1

    Input
    1
    5 3
    1 2 4
    1 3 1
    1 4 8
    4 5 9
    
    Expected output
    44