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