This page is still under construction.

Parts of this page are still being built. What you see may change.

The festival must go on

Time limit1sMemory limit256 MB

Summary
Pick M roads of a weighted tree so the longest path using only picked roads is as short as possible.
Level

Medium7 of 10

Topics
Binary search, Tree, Greedy
Solved
No attempts yet

Problem

Cube World has NN cities and N−1N-1 bidirectional roads. Every pair of distinct cities is joined by a route, so the road network is a tree. Cities are numbered from 11 to NN, roads are numbered from 11 to N−1N-1, and road ii has length lil_i.

The ruler of Cube World is holding a festival. He picks MM of the N−1N-1 roads and runs one event on each picked road, such as a car parade or a sports match. The transport ministry complained that traffic would be paralysed, so the ruler decided to keep the disruption small. Among all ways to pick MM roads, he takes one that minimizes the maximum length of a simple path that uses only picked roads. The length of a path is the sum of the lengths of the roads on it, and a path that stays in one city has length 00.

For each test case, find that minimized maximum.

Input

The first line contains an integer TT (1≤T≤1001 \le T \le 100), the number of test cases.

The first line of each test case contains two space separated integers NN and MM (2≤N≤20002 \le N \le 2000, 1≤M≤N−11 \le M \le N-1), where NN is the number of cities and MM is the number of roads to pick. The next N−1N-1 lines describe the roads. The ii-th of them contains three space separated integers aia_i, bib_i, lil_i (1≤ai,bi≤N1 \le a_i, b_i \le N, ai≠bia_i \ne b_i, 1≤li≤1061 \le l_i \le 10^6), meaning that road ii connects city aia_i and city bib_i and its length is lil_i.

The sum of NN over all test cases does not exceed 20002000.

Output

For each test case, print one line with the maximum length of a simple path over the picked roads, for a choice of MM roads that makes this value as small as possible.

Note

A simple path is a path that never repeats a city. It is a sequence of distinct cities c1,c2,…,clc_1, c_2, \dots, c_l such that for every ii with 1≤i≤l−11 \le i \le l-1 a road connects city cic_i and city ci+1c_{i+1}. In this problem every such road has to be a picked road.

Examples2

  1. Example 1

    Input
    2
    5 3
    1 2 3
    1 3 6
    2 4 7
    2 5 4
    5 3
    1 2 3
    2 3 6
    2 4 7
    1 5 4
    
    Expected output
    11
    13
    
  2. Example 2

    Input
    4
    2 1
    1 2 5
    5 2
    1 2 10
    1 3 9
    1 4 8
    1 5 7
    6 3
    1 2 2
    2 3 9
    3 4 4
    4 5 1
    5 6 6
    4 3
    1 2 1000000
    2 3 1000000
    3 4 1000000
    
    Expected output
    5
    15
    5
    3000000