#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC target("sse4")
using namespace std;
template <class T>
bool ckmin(T& a, const T& b) {
  return b < a ? a = b, 1 : 0;
}
template <class T>
bool ckmax(T& a, const T& b) {
  return a < b ? a = b, 1 : 0;
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int MOD = 1000000007;
const char nl = '\n';
const int MX = 100001;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int T;
  cin >> T;
  while (T--) {
    int N;
    cin >> N;
    int K;
    cin >> K;
    vector<vector<int> > graph(N);
    int deg[N];
    for (int i = 0; i < (N); i++) deg[i] = 0;
    bool leaf[N];
    for (int i = 0; i < (N); i++) leaf[i] = false;
    int cl[N];
    for (int i = 0; i < (N); i++) cl[i] = 0;
    for (int i = 0; i < (N - 1); i++) {
      int A, B;
      cin >> A >> B;
      A--;
      B--;
      graph[A].push_back(B);
      graph[B].push_back(A);
      deg[A]++;
      deg[B]++;
    }
    if (N == 2) {
      cout << 1 << nl;
      continue;
    }
    for (int i = 0; i < (N); i++) {
      if (deg[i] == 1) {
        leaf[i] = true;
        int X = graph[i][0];
        cl[X]++;
      }
    }
    queue<int> q;
    for (int i = 0; i < (N); i++) {
      if (cl[i] >= K) {
        q.push(i);
      }
    }
    int ans = 0;
    while (!q.empty()) {
      int cur = q.front();
      q.pop();
      int rem = cl[cur] / K;
      rem *= K;
      if (rem == 0) continue;
      deg[cur] -= rem;
      cl[cur] -= rem;
      ans += rem / K;
      if (deg[cur] == 1) {
        leaf[cur] = true;
        for (auto& a : graph[cur]) {
          if (!leaf[a]) {
            cl[a]++;
            if (cl[a] == K) q.push(a);
          }
        }
      }
    }
    cout << ans << nl;
  }
  return 0;
}
