This page is still under construction.

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

Bones's Battery

Time limit5sMemory limit128 MB

Summary
Find the smallest battery range so every pair of schools connects with at most K charges over roads within range.
Level

Medium5 of 10

Topics
Binary search, Graph, Shortest path
Solved
No attempts yet

Problem

Bones is shopping for an electric shuttle for the school district where his mother works. Every school has a charging station. Call the range of the shuttle the greatest distance it can drive on a full charge.

A trip from any school to any other school has to finish with at most KK rechargings. The shuttle's battery starts out empty, so it must be charged before it sets off, and that charge counts toward the KK. It may be charged again at any school it stops at along the way.

At most one road runs between any pair of schools, and every pair of schools is joined by some sequence of roads. Given the road network and KK, find the smallest range the electric shuttle needs.

Input

The first line has one integer TT (1≤T≤501 \le T \le 50), the number of test cases.

Each test case begins with a line of three integers NN, KK, and MM (2≤N≤1002 \le N \le 100, 1≤K≤1001 \le K \le 100), where NN is the number of schools, KK is the largest number of rechargings allowed on one trip, and MM is the number of roads.

Each of the next MM lines has three integers uiu_i, viv_i, and did_i (0≤ui,vi<N0 \le u_i, v_i < N, ui≠viu_i \ne v_i, 1≤di≤1091 \le d_i \le 10^9). Road ii joins school uiu_i and school viv_i in both directions and has length did_i. Schools are numbered from 0.

Output

For each test case, print the smallest required range on one line.

Examples2

  1. Example 1

    Input
    2
    4 2 4
    0 1 100
    1 2 200
    2 3 300
    3 0 400
    10 2 15
    0 1 113
    1 2 314
    2 3 271
    3 4 141
    4 0 173
    5 7 235
    7 9 979
    9 6 402
    6 8 431
    8 5 462
    0 5 411
    1 6 855
    2 7 921
    3 8 355
    4 9 113
    
    Expected output
    300
    688
    
  2. Example 2

    Input
    3
    5 1 4
    0 1 10
    1 2 10
    2 3 10
    3 4 10
    5 4 4
    0 1 10
    1 2 10
    2 3 10
    3 4 10
    5 2 4
    0 1 10
    1 2 10
    2 3 10
    3 4 10
    
    Expected output
    40
    10
    20