#include <bits/stdc++.h>
using namespace std;
vector<pair<long long, long long> > ed[500001];
int n, k;
pair<long long, long long> dfs(int u, int p) {
  vector<pair<long long, long long> > var;
  long long tot = 0, cnt = 0;
  for (auto v : ed[u]) {
    if (v.first == p) continue;
    pair<long long, long long> temp = dfs(v.first, u);
    var.push_back({temp.first + v.second, temp.second});
    tot += temp.second;
  }
  sort(var.begin(), var.end(),
       [](pair<long long, long long> l, pair<long long, long long> r) {
         return l.first - l.second > r.first - r.second;
       });
  pair<long long, long long> ret = {tot, tot};
  for (auto x : var) {
    if (x.first > x.second) tot += x.first - x.second, cnt++;
    if (cnt == k - 1) ret.first = tot;
    if (cnt == k) {
      ret.second = tot;
      break;
    }
  }
  if (cnt < k) ret.first = ret.second = tot;
  return ret;
}
int main(int argc, char **argv) {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t;
  cin >> t;
  while (t--) {
    cin >> n >> k;
    for (int i = 0; i < n; i++) ed[i + 1].clear();
    for (int i = 0; i < n - 1; i++) {
      int a, b, c;
      cin >> a >> b >> c;
      ed[a].push_back({b, c}), ed[b].push_back({a, c});
    }
    cout << dfs(1, -1).second << endl;
  }
}
