#include <bits/stdc++.h>
using namespace std;
const int mxn = 2e5;
vector<int> adj[mxn];
unordered_set<int> frd[mxn];
bool is_leaf[mxn];
int cnt_leaf[mxn];
void dfs(int u, int p) {
  int ch = -1 * (p == -1);
  for (int v : adj[u]) {
    if (v != p) {
      dfs(v, u);
      ch++;
    }
  }
  if (ch == 0) {
    is_leaf[u] = 1;
    if (p >= 0)
      cnt_leaf[p]++;
    else
      cnt_leaf[*adj[u].begin()]++;
  }
}
void solve() {
  int n, k;
  cin >> n >> k;
  for (int i = 1; i < n; i++) {
    int u, v;
    cin >> u >> v, --u, --v;
    adj[v].push_back(u);
    adj[u].push_back(v);
  }
  if (k == 1) {
    cout << n - 1 << '\n';
    for (int i = 0; i < n; i++) adj[i].clear();
    memset(is_leaf, 0, 4 * n);
    memset(cnt_leaf, 0, 4 * n);
    return;
  }
  dfs(0, -1);
  int ans = 0;
  queue<int> q;
  vector<int> inq(n);
  for (int i = 0; i < n; i++) {
    for (int v : adj[i]) {
      if (!is_leaf[v]) frd[i].insert(v);
    }
    if (cnt_leaf[i] >= k) {
      q.push(i);
      inq[i] = true;
    }
  }
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    inq[u] = false;
    ans += cnt_leaf[u] / k;
    cnt_leaf[u] %= k;
    if (frd[u].size() == 1u && cnt_leaf[u] == 0) {
      int p = *frd[u].begin();
      frd[p].erase(u);
      cnt_leaf[p]++;
      if (!inq[p] && cnt_leaf[p] >= k) {
        q.push(p);
        inq[p] = true;
      }
    }
  }
  cout << ans << '\n';
  for (int i = 0; i < n; i++) adj[i].clear(), frd[i].clear();
  memset(is_leaf, 0, 4 * n);
  memset(cnt_leaf, 0, 4 * n);
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int tt;
  cin >> tt;
  while (tt--) {
    solve();
  }
  return 0;
}
