#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t;
  cin >> t;
  while (t--) {
    int n, k;
    cin >> n >> k;
    int bigans = 0;
    vector<int> adj[n];
    for (int i = 0; i < n - 1; i++) {
      int a, b;
      cin >> a >> b;
      adj[a - 1].push_back(b - 1);
      adj[b - 1].push_back(a - 1);
    }
    if (n == 2) {
      if (k == 1)
        cout << 1 << endl;
      else
        cout << 0 << endl;
      continue;
    }
    priority_queue<pair<int, int> > p;
    int numleaves[n];
    bool isleaf[n];
    int neighs[n];
    memset(numleaves, 0, sizeof(numleaves));
    memset(isleaf, false, sizeof(isleaf));
    for (int i = 0; i < n; i++) {
      int num_actual_leaves = 0;
      for (int j = 0; j < adj[i].size(); j++) {
        if (adj[adj[i][j]].size() == 1) num_actual_leaves++;
      }
      p.push({num_actual_leaves, i});
      numleaves[i] = num_actual_leaves;
    }
    for (int i = 0; i < n; i++)
      if (adj[i].size() == 1) isleaf[i] = true;
    for (int i = 0; i < n; i++) neighs[i] = adj[i].size();
    while (!p.empty()) {
      pair<int, int> curr = p.top();
      p.pop();
      if (curr.first < k) break;
      if (curr.first != numleaves[curr.second]) continue;
      bigans += (curr.first / k);
      numleaves[curr.second] = (curr.first % k);
      neighs[curr.second] -= ((curr.first / k) * k);
      if (curr.first % k != 0) continue;
      if (curr.first % k == 0) {
        if (neighs[curr.second] == 1) {
          isleaf[curr.second] = true;
          for (int i = 0; i < adj[curr.second].size(); i++) {
            if (isleaf[adj[curr.second][i]] == false) {
              numleaves[adj[curr.second][i]]++;
              p.push({numleaves[adj[curr.second][i]], adj[curr.second][i]});
              break;
            }
          }
        }
      }
    }
    cout << bigans << endl;
  }
}
