#include <bits/stdc++.h>
using namespace std;
vector<int> cnt;
struct comp {
  bool operator()(int a, int b) const {
    if ((int)cnt[a] == (int)cnt[b])
      return a < b;
    else
      return cnt[a] > cnt[b];
  }
};
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int t;
  cin >> t;
  while (t--) {
    int n, k;
    cin >> n >> k;
    cnt.clear();
    cnt.resize(n);
    vector<set<int> > adj(n);
    for (int i = 0; i < n - 1; i++) {
      int u, v;
      cin >> u >> v;
      u--;
      v--;
      adj[u].insert(v);
      adj[v].insert(u);
    }
    for (int i = 0; i < n; i++) {
      if (adj[i].size() == 1) {
        cnt[*adj[i].begin()]++;
      }
    }
    set<int, comp> pq;
    for (int i = 0; i < n; i++) {
      pq.insert(i);
    }
    int ans = 0;
    while (pq.size()) {
      int it = *pq.begin();
      if (cnt[it] < k) break;
      pq.erase(pq.begin());
      int c = k;
      vector<int> vert;
      for (auto v : adj[it]) {
        if (adj[v].size() == 1) {
          if (c > 0) {
            vert.push_back(v);
            c--;
          } else
            break;
        }
      }
      for (auto v : vert) {
        adj[it].erase(v);
        adj[v].erase(it);
      }
      if (c == 0) ans++;
      cnt[it] -= k;
      if (cnt[it])
        pq.insert(it);
      else if (adj[it].size() == 1) {
        int par = *adj[it].begin();
        pq.erase(par);
        cnt[par]++;
        pq.insert(par);
      }
    }
    cout << ans << "\n";
  }
}
