#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
bool leaf[N];
vector<int> adj[N];
int tc, n, k, ans, dp[N], leaves_num[N];
void dfs(int u, int p) {
  for (auto &c : adj[u]) {
    if (c != p) {
      dfs(c, u);
      dp[u] += dp[c];
      leaves_num[u] += leaf[c];
    }
  }
  leaf[u] = (leaves_num[u] % k == 0 &&
             leaves_num[u] == ((int)adj[u].size() - (p ? 1 : 0)));
  dp[u] += leaves_num[u] / k;
}
void solve(int u, int p) {
  ans = max(ans, dp[u]);
  for (auto &c : adj[u]) {
    if (c != p) {
      bool u_leaf = leaf[u], c_leaf = leaf[c];
      int u_dp = dp[u], u_leaves_num = leaves_num[u];
      int c_dp = dp[c], c_leaves_num = leaves_num[c];
      dp[u] -= (leaves_num[u] / k + dp[c]);
      leaves_num[u] -= leaf[c];
      dp[u] += leaves_num[u] / k;
      leaf[u] = (leaves_num[u] % k == 0 && leaves_num[u] == adj[u].size() - 1);
      dp[c] -= leaves_num[c] / k;
      leaves_num[c] += leaf[u];
      dp[c] += leaves_num[c] / k + dp[u];
      leaf[c] = (leaves_num[c] % k == 0 && leaves_num[c] == adj[c].size());
      solve(c, u);
      leaf[u] = u_leaf, leaf[c] = c_leaf;
      dp[u] = u_dp, leaves_num[u] = u_leaves_num;
      dp[c] = c_dp, leaves_num[c] = c_leaves_num;
    }
  }
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> tc;
  while (tc--) {
    cin >> n >> k;
    ans = 0;
    for (int i = 0; i <= n; ++i) {
      dp[i] = leaves_num[i] = leaf[i] = 0;
      adj[i].clear();
    }
    for (int i = 1; i < n; ++i) {
      int u, v;
      cin >> u >> v;
      adj[u].push_back(v);
      adj[v].push_back(u);
    }
    dfs(1, 0);
    solve(1, 0);
    cout << ans << '\n';
  }
  return 0;
}
