#include <bits/stdc++.h>
using namespace std;
const int nax = 5 * 1e5 + 10;
int n, k;
vector<pair<int, int>> tree[nax];
long long dp[nax][3];
long long dfs(int u, int f, int p) {
  long long& cur = dp[u][f];
  if (cur != -1) return cur;
  cur = 0ll;
  vector<long long> val;
  for (auto& i : tree[u]) {
    int v = i.first;
    int w = i.second;
    if (v == p) continue;
    long long take = dfs(v, 1, u);
    long long ntake = dfs(v, 0, u);
    cur += ntake;
    val.push_back(take + w - ntake);
  }
  sort(val.rbegin(), val.rend());
  for (int i = 0; i < min(k - f, (int)val.size()); i++) cur += max(val[i], 0ll);
  return cur;
}
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0);
  ;
  int t;
  cin >> t;
  while (t--) {
    cin >> n >> k;
    for (int i = 0; i <= n; i++) {
      dp[i][1] = dp[i][0] = -(1ll);
      tree[i].clear();
    }
    for (int i = 0; i < n - 1; i++) {
      int u, v, w;
      cin >> u >> v >> w;
      tree[u].push_back({v, w});
      tree[v].push_back({u, w});
    }
    cout << max(dfs(1, 0, -1), dfs(1, 1, -1)) << endl;
  }
  return 0;
}
