#include <bits/stdc++.h>
using namespace std;
vector<set<long long int>> ed, leaf;
struct comp {
  bool operator()(long long int a, long long int b) const {
    if (leaf[a].size() == leaf[b].size()) return a < b;
    return leaf[a].size() > leaf[b].size();
  }
};
signed main(void) {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long int t;
  cin >> t;
  while (t--) {
    long long int n, k;
    cin >> n >> k;
    ed = vector<set<long long int>>(n);
    leaf = vector<set<long long int>>(n);
    for (long long int(i) = (0); (i) < (n - 1); (i)++) {
      long long int u, v;
      cin >> u >> v;
      u--;
      v--;
      ed[u].insert(v);
      ed[v].insert(u);
    }
    for (long long int(i) = (0); (i) < (n); (i)++) {
      if (ed[i].size() == 1) {
        leaf[*ed[i].begin()].insert(i);
      }
    }
    set<long long int, comp> maxLeaf;
    for (long long int(i) = (0); (i) < (n); (i)++) {
      maxLeaf.insert(i);
    }
    long long int res = 0;
    while (1) {
      long long int v = *maxLeaf.begin();
      if (leaf[v].size() < k) break;
      maxLeaf.erase(v);
      for (long long int(i) = (0); (i) < (k); (i)++) {
        long long int x = *leaf[v].begin();
        leaf[v].erase(x);
        if (leaf[x].find(v) != leaf[x].end()) {
          leaf[x].erase(v);
          maxLeaf.erase(x);
          maxLeaf.insert(x);
        }
        ed[x].erase(v);
        ed[v].erase(x);
      }
      if (ed[v].size() == 1) {
        long long int to = *ed[v].begin();
        maxLeaf.erase(to);
        leaf[to].insert(v);
        maxLeaf.insert(to);
      }
      maxLeaf.insert(v);
      res++;
    }
    cout << res << "\n";
  }
  return 0;
}
