#include <bits/stdc++.h>
using namespace std;
int main(int argc, char *argv[]) {
  long long t, T;
  scanf("%lld", &T);
  ;
  for (t = (0); t < (T); ++t) {
    long long n, k, i;
    scanf("%lld", &n);
    ;
    scanf("%lld", &k);
    ;
    vector<unordered_set<long long>> edges(n);
    for (i = (0); i < (n - 1); ++i) {
      long long x, y;
      scanf("%lld", &x);
      ;
      scanf("%lld", &y);
      ;
      x--;
      y--;
      edges[x].insert(y);
      edges[y].insert(x);
    }
    if (k == 1) {
      printf("%lld\n", n - 1);
    } else {
      vector<long long> num_leaves(n, 0);
      vector<pair<long long, long long>> to_be_removed;
      for (i = (0); i < (n); ++i) {
        if (int((edges[i]).size()) == 1) {
          long long j = *edges[i].begin();
          num_leaves[j]++;
          to_be_removed.push_back({i, j});
        }
      }
      for (auto r : to_be_removed) {
        long long i = r.first, j = r.second;
        edges[i].erase(j);
        edges[j].erase(i);
      }
      set<pair<long long, long long>, greater<pair<long long, long long>>> nls;
      for (i = (0); i < (n); ++i) {
        nls.insert({num_leaves[i], i});
      }
      long long ans = 0;
      while (!nls.empty()) {
        auto t = *nls.begin();
        nls.erase(nls.begin());
        if (t.first < k) {
          break;
        }
        long long cur = t.second;
        long long delta = num_leaves[cur] / k;
        ans += delta;
        num_leaves[cur] %= k;
        if (int((edges[cur]).size()) == 1 && num_leaves[cur] == 0) {
          long long dst = *edges[cur].begin();
          edges[cur].erase(dst);
          edges[dst].erase(cur);
          nls.erase({num_leaves[dst], dst});
          num_leaves[dst]++;
          nls.insert({num_leaves[dst], dst});
        } else {
          nls.insert({num_leaves[cur], cur});
        }
      }
      printf("%lld\n", ans);
    }
  }
  return 0;
}
