#include <bits/stdc++.h>
using namespace std;
int n, k, ans;
vector<set<int>> g;
vector<bool> willBeLeaf;
vector<int> dp, leavesAdj;
void initDP(int par, int node) {
  bool isLeaf = true;
  int willBeLeaves = 0;
  int totalAdj = 0;
  dp[node] = 0;
  for (int adj : g[node]) {
    if (adj == par) continue;
    initDP(node, adj);
    dp[node] += dp[adj];
    willBeLeaves += willBeLeaf[adj];
    isLeaf = false;
    totalAdj++;
  }
  if (isLeaf) {
    dp[node] = 0;
    willBeLeaf[node] = true;
    leavesAdj[node] = 0;
  } else {
    dp[node] += willBeLeaves / k;
    willBeLeaf[node] = (totalAdj - willBeLeaves) == 0 && willBeLeaves % k == 0;
    leavesAdj[node] = willBeLeaves;
  }
}
void reroot(int par, int node) {
  ans = max(ans, dp[node]);
  for (int adj : g[node]) {
    if (adj == par) continue;
    dp[node] -= dp[adj] + leavesAdj[node] / k;
    leavesAdj[node] -= willBeLeaf[adj];
    dp[node] += leavesAdj[node] / k;
    willBeLeaf[node] =
        (g[node].size() - 1 - leavesAdj[node]) == 0 && leavesAdj[node] % k == 0;
    dp[adj] -= leavesAdj[adj] / k;
    leavesAdj[adj] += willBeLeaf[node];
    dp[adj] += dp[node] + leavesAdj[adj] / k;
    willBeLeaf[adj] =
        (g[adj].size() - leavesAdj[adj]) == 0 && leavesAdj[adj] % k == 0;
    reroot(node, adj);
    dp[adj] -= dp[node] + leavesAdj[adj] / k;
    leavesAdj[adj] -= willBeLeaf[node];
    dp[adj] += leavesAdj[adj] / k;
    willBeLeaf[adj] =
        (g[adj].size() - 1 - leavesAdj[adj]) == 0 && leavesAdj[adj] % k == 0;
    dp[node] -= leavesAdj[node] / k;
    leavesAdj[node] += willBeLeaf[adj];
    dp[node] += dp[adj] + leavesAdj[node] / k;
    willBeLeaf[node] =
        (g[node].size() - leavesAdj[node]) == 0 && leavesAdj[node] % k == 0;
  }
}
void solve(int test_index) {
  cin >> n >> k;
  g.clear();
  g.resize(n);
  willBeLeaf.clear();
  willBeLeaf.resize(n);
  dp.clear();
  dp.resize(n);
  leavesAdj.clear();
  leavesAdj.resize(n);
  for (int i = 0; i < n - 1; i++) {
    int u, v;
    cin >> u >> v;
    u--, v--;
    g[u].insert(v);
    g[v].insert(u);
  }
  if (k == 1) {
    cout << n - 1 << "\n";
    return;
  }
  initDP(-1, 0);
  ans = dp[0];
  reroot(-1, 0);
  cout << ans << "\n";
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  int num_tests = 1;
  cin >> num_tests;
  for (int test_index = 1; test_index <= num_tests; test_index++) {
    solve(test_index);
  }
  return 0;
}
