#include <bits/stdc++.h>
using namespace std;
long long n, k;
vector<vector<pair<long long, long long>>> graph;
bool comp(pair<long long, long long> a, pair<long long, long long> b) {
  return (a.first - a.second) > (b.first - b.second);
}
pair<long long, long long> recur(long long index, long long parent) {
  long long i;
  pair<long long, long long> ans;
  ans.first = 0;
  ans.second = 0;
  vector<pair<long long, long long>> children;
  for (auto it : graph[index]) {
    if (it.first == parent) continue;
    pair<long long, long long> rca = recur(it.first, index);
    children.push_back(make_pair(rca.first + it.second, rca.second));
  }
  sort((children).begin(), (children).end(), comp);
  for (i = (0); i < (min(k - 1, (long long)(int)((children).size()))); i++) {
    if (children[i].first >= children[i].second) {
      ans.first += children[i].first;
      ans.second += children[i].first;
    } else {
      ans.first += children[i].second;
      ans.second += children[i].second;
    }
  }
  if ((k - 1) < (int)((children).size())) {
    ans.first += children[k - 1].second;
    if (children[i].first >= children[i].second) {
      ans.second += children[k - 1].first;
    } else {
      ans.second += children[k - 1].second;
    }
  }
  for (i = (k); i < ((int)((children).size())); i++) {
    ans.first += children[i].second;
    ans.second += children[i].second;
  }
  return ans;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  long long i, j;
  long long q;
  cin >> q;
  while (q--) {
    cin >> n >> k;
    graph.clear();
    graph.resize(n);
    for (i = (0); i < (n - 1); i++) {
      long long a, b, c;
      cin >> a >> b >> c;
      graph[a - 1].push_back(make_pair(b - 1, c));
      graph[b - 1].push_back(make_pair(a - 1, c));
    }
    pair<long long, long long> rcans = recur(0, -1);
    cout << max(rcans.second, rcans.first) << '\n';
  }
  return 0;
}
