#include <bits/stdc++.h>
using namespace std;
const int N = 5e5 + 7;
const long long INF = 1e18 + 7;
int n, k, q;
long long s;
vector<pair<int, int> > g[N];
int dfs(int v = 1, int p = 0) {
  int last = 0;
  int c = k;
  vector<int> dif;
  for (pair<int, int> to : g[v]) {
    if (to.second != p) {
      dif.push_back(to.first - dfs(to.second, v));
    }
  }
  sort(dif.begin(), dif.end(), greater<int>());
  for (int ver : dif) {
    if (ver > 0 && c) {
      c--;
      s += ver;
      last = ver;
    }
  }
  if (c)
    return 0;
  else
    return last;
}
int main() {
  cin >> q;
  while (q--) {
    cin >> n >> k;
    s = 0;
    for (int i = 1; i < n; i++) {
      int a, b, c;
      scanf("%d%d%d", &a, &b, &c);
      g[a].push_back(make_pair(c, b));
      g[b].push_back(make_pair(c, a));
    }
    for (int i = 1; i <= n; i++)
      sort(g[i].begin(), g[i].end(), greater<pair<int, int> >());
    dfs();
    for (int i = 1; i <= n; i++) g[i].clear();
    cout << s << endl;
  }
}
