#include <bits/stdc++.h>
using namespace std;
const int INF = 1000000001;
const long long int INFLL = 1000000000000000001;
const long double EPS = 1e-6;
const int MAXN = 500005;
bool visited[MAXN];
int n, k;
long long int DP[MAXN][2];
vector<pair<int, long long int>> adj[MAXN];
void Dfs(int u) {
  visited[u] = true;
  long long int result = 0;
  vector<pair<long long int, int>> changes;
  for (const pair<int, long long int>& x : adj[u]) {
    int v = x.first;
    long long int c = x.second;
    if (!visited[v]) {
      Dfs(v);
      result += DP[v][0];
      changes.push_back(make_pair(DP[v][1] + c - DP[v][0], 1));
      changes.push_back(make_pair(DP[v][1] - DP[v][0], 0));
    }
  }
  sort(changes.begin(), changes.end());
  reverse(changes.begin(), changes.end());
  DP[u][0] = result;
  DP[u][1] = result;
  int psum = 0;
  for (int i = 0; i < (int)changes.size(); i++) {
    psum += changes[i].second;
    if (psum <= k - 1) DP[u][1] += max(0LL, changes[i].first);
    if (psum <= k) DP[u][0] += max(0LL, changes[i].first);
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int q;
  cin >> q;
  while (q--) {
    cin >> n >> k;
    for (int i = 0; i < n - 1; i++) {
      int u, v;
      long long int c;
      cin >> u >> v >> c;
      u--;
      v--;
      adj[u].push_back(make_pair(v, c));
      adj[v].push_back(make_pair(u, c));
    }
    Dfs(0);
    cout << DP[0][0] << "\n";
    for (int i = 0; i <= n; i++) {
      adj[i].clear();
      adj[i].shrink_to_fit();
      DP[i][0] = 0;
      DP[i][1] = 0;
      visited[i] = false;
    }
  }
  return 0;
}
