#include <bits/stdc++.h>
using namespace std;
template <class c>
struct rge {
  c b, e;
};
template <class c>
rge<c> range(c i, c j) {
  return rge<c>{i, j};
}
template <class c>
auto dud(c* x) -> decltype(cerr << *x, 0);
template <class c>
char dud(...);
struct debug {
  template <class c>
  debug& operator<<(const c&) {
    return *this;
  }
};
vector<char*> tokenizer(const char* args) {
  char* token = new char[111];
  strcpy(token, args);
  token = strtok(token, ", ");
  vector<char*> v({token});
  while (token = strtok(NULL, ", ")) v.push_back(token);
  return reverse(v.begin(), v.end()), v;
}
void debugg(vector<char*> args) { cerr << "\b\b "; }
template <typename Head, typename... Tail>
void debugg(vector<char*> args, Head H, Tail... T) {
  debug() << " [" << args.back() << ": " << H << "] ";
  args.pop_back();
  debugg(args, T...);
}
int64_t powm(int64_t a, int64_t b, int64_t mod) {
  int64_t res = 1;
  while (b) {
    if (b & 1) res = (res * a) % mod;
    a = (a * a) % mod;
    b >>= 1;
  }
  return res;
}
const long double pi = 3.14159265358979323846;
const int64_t mod = 1e9 + 7;
const int64_t N = 1000005;
const int64_t inf = 1e9;
const int64_t INF = 1e18;
vector<vector<pair<int64_t, int64_t>>> adj;
int64_t n, k;
pair<int64_t, int64_t> dfs(int64_t node, int64_t parent) {
  vector<int64_t> v;
  int64_t ans = 0;
  for (auto it : adj[node]) {
    if (it.first != parent) {
      pair<int64_t, int64_t> cur = dfs(it.first, node);
      int64_t dp[2];
      dp[0] = cur.first;
      dp[1] = cur.second;
      ans += dp[0];
      if (dp[1] + it.second - dp[0] > 0) v.push_back(dp[1] + it.second - dp[0]);
    }
  }
  sort(v.rbegin(), v.rend());
  for (int64_t i = 0; i < min(k, int64_t(v.size())); i++) ans += v[i];
  if (v.size() >= k) return make_pair(ans, ans - v[k - 1]);
  return make_pair(ans, ans);
}
void solve() {
  cin >> n >> k;
  adj.clear();
  adj.resize(n, vector<pair<int64_t, int64_t>>({}));
  for (int64_t i = 1; i < n; i++) {
    int64_t x, y, wt;
    cin >> x >> y >> wt;
    x--, y--;
    adj[x].push_back(make_pair(y, wt));
    adj[y].push_back(make_pair(x, wt));
  }
  cout << dfs(0, -1).first << '\n';
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0), cout.tie(0);
  cout << fixed << setprecision(15);
  int64_t tc = 1;
  cin >> tc;
  for (int64_t t = 1; t <= tc; t++) {
    solve();
  }
  return 0;
}
