#include <bits/stdc++.h>
using namespace std;
mt19937 gen(chrono::system_clock::now().time_since_epoch().count());
const int N = 5e5 + 10;
const long long MOD = 1e9 + 7;
const int rx[4] = {-1, 0, 1, 0};
const int ry[4] = {0, 1, 0, -1};
const int dx[8] = {-1, -1, -1, 0, 1, 1, 1, 0};
const int dy[8] = {-1, 0, 1, 1, 1, 0, -1, -1};
const int hx[8] = {-2, -2, -1, 1, 2, 2, 1, -1};
const int hy[8] = {-1, 1, 2, 2, 1, -1, -2, -2};
const long double eps = 1e-7;
const double pi = acos(-1.0);
const int INF = 2e9;
vector<pair<int, int> > g[N];
long long dp[N][2];
int k;
bool used[N];
void dfs(int v, int p = -1) {
  dp[v][0] = 0;
  dp[v][1] = 0;
  vector<pair<int, int> > vct;
  for (auto to : g[v]) {
    if (to.first == p) continue;
    dfs(to.first, v);
    vct.push_back({dp[to.first][0] + to.second - dp[to.first][1], to.first});
  }
  sort(vct.rbegin(), vct.rend());
  int kol = k;
  for (auto it : vct) {
    if (kol && it.first > 0) {
      kol--;
      if (kol == 0) {
        dp[v][1] += dp[it.second][1] + it.first;
        dp[v][0] += max(dp[it.second][1], dp[it.second][0]);
      } else {
        dp[v][1] += dp[it.second][1] + it.first;
        dp[v][0] += dp[it.second][1] + it.first;
      }
      used[it.second] = true;
    } else
      break;
  }
  for (auto to : g[v]) {
    if (to.first == p) continue;
    if (used[to.first]) continue;
    dp[v][0] += max(dp[to.first][1], dp[to.first][0]);
    dp[v][1] += max(dp[to.first][1], dp[to.first][0]);
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int T;
  cin >> T;
  while (T--) {
    int n;
    cin >> n >> k;
    for (int i = 0; i < n; i++) {
      g[i].clear();
      g[i].shrink_to_fit();
      used[i] = false;
    }
    for (int i = 0; i < n - 1; i++) {
      int x, y, w;
      cin >> x >> y >> w;
      x--;
      y--;
      g[x].push_back({y, w});
      g[y].push_back({x, w});
    }
    dfs(0);
    cout << max(dp[0][0], dp[0][1]) << endl;
  }
  return 0;
}
