#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007LL;
int t;
int n, k;
vector<int> edges[200005];
int deg[200005];
int ans;
void bfs() {
  ans = 0;
  int m = n;
  vector<int> ids;
  ids.clear();
  if (m == 2) {
    if (k == 1) ans++;
    return;
  }
  for (int i = 1; i <= m; ++i) {
    if (deg[i] == 1) {
      ids.push_back(i);
    }
  }
  map<int, int> cnt;
  cnt.clear();
  for (int i = 0; i < ids.size(); ++i) {
    cnt[edges[ids[i]][0]]++;
  }
  set<int> ss;
  for (auto itr : cnt) {
    if (itr.second >= k) {
      ss.insert(itr.first);
    }
  }
  while (m > 2 && !ss.empty()) {
    auto u = (*ss.begin());
    if (cnt[u] % k != 0) {
      int x = cnt[u] / k;
      ans += x;
      cnt[u] %= k;
      ss.erase(u);
      deg[u] -= x * k;
      m -= x * k;
    } else {
      int x = cnt[u] / k;
      ans += x;
      cnt.erase(u);
      deg[u] -= x * k;
      ss.erase(u);
      m -= x * k;
      if (deg[u] > 1) continue;
      for (int i = 0; i < edges[u].size(); ++i) {
        int v = edges[u][i];
        if (deg[v] > 1) {
          cnt[v]++;
          if (cnt[v] >= k) {
            ss.insert(v);
          }
          break;
        }
      }
    }
  }
  if (m == 2 && k == 1) {
    ans++;
  }
}
int main() {
  cin >> t;
  for (int cas = 1; cas <= t; ++cas) {
    cin >> n >> k;
    for (int i = 1; i <= n; ++i) {
      deg[i] = 0;
      edges[i].clear();
    }
    for (int i = 1; i < n; ++i) {
      int x, y;
      scanf("%d%d", &x, &y);
      edges[x].push_back(y);
      edges[y].push_back(x);
      deg[x]++;
      deg[y]++;
    }
    bfs();
    cout << ans << endl;
  }
  return 0;
}
