#include <bits/stdc++.h>
using namespace std;
int n, k, ans;
vector<set<int>> adj;
vector<int> leaf;
void init() {
  cin >> n >> k;
  ans = 0;
  adj = vector<set<int>>(n);
  leaf = vector<int>(n);
  for (int i = 0; i < n - 1; i++) {
    int x, y;
    cin >> x >> y;
    --x;
    --y;
    adj[x].insert(y);
    adj[y].insert(x);
  }
  for (int i = 0; i < n; i++) {
    for (int x : adj[i]) {
      if (adj[x].size() == 1) leaf[i]++;
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int tt;
  cin >> tt;
  while (tt--) {
    init();
    if (k == 1) {
      cout << n - 1 << '\n';
      continue;
    }
    queue<int> qq;
    for (int i = 0; i < n; i++) {
      if (leaf[i] >= k) qq.push(i);
    }
    int ans = 0;
    while (!qq.empty()) {
      int cur = qq.front();
      qq.pop();
      if (leaf[cur] < k) continue;
      ans += leaf[cur] / k;
      int take = leaf[cur] - (leaf[cur] % k);
      leaf[cur] %= k;
      for (auto it = adj[cur].begin(); take > 0;) {
        if (adj[*it].size() == 1) {
          it = adj[cur].erase(it);
          take--;
        } else
          it++;
      }
      if (leaf[cur] == 0 && adj[cur].size() == 1) {
        int x = *adj[cur].begin();
        leaf[x]++;
        qq.push(x);
      }
    }
    cout << ans << '\n';
  }
}
