#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,avx,avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
using namespace std;
vector<int> arr[200005];
long long int dp[200005]{0};
long long int dp2[200005]{0};
long long n, u, v, w, k;
long long ans = 0;
long long curr = 0;
long long dfs(int i, int from) {
  for (auto to : arr[i]) {
    if (to != from) {
      dp[i] += dfs(to, i);
      dp2[i]++;
    }
  }
  curr += dp[i] / k;
  if (dp[i] % k == 0 && dp[i] == dp2[i]) {
    return 1;
  } else {
    return 0;
  }
}
void dfs2(int i, int from) {
  ans = max(ans, curr);
  for (auto to : arr[i]) {
    if (to != from) {
      long long a = dp[to];
      long long b = dp[i];
      long long c = curr;
      curr -= dp[i] / k;
      curr -= dp[to] / k;
      if (dp[to] % k == 0 && dp2[to] == dp[to]) {
        dp[i]--;
      }
      dp2[i]--;
      dp2[to]++;
      if (dp[i] % k == 0 && dp2[i] == dp[i]) {
        dp[to]++;
      }
      curr += dp[i] / k;
      curr += dp[to] / k;
      ans = max(ans, curr);
      dfs2(to, i);
      dp2[to]--;
      dp2[i]++;
      dp[to] = a;
      dp[i] = b;
      curr = c;
    }
  }
}
void pre() {}
void solve() {
  cin >> n >> k;
  ans = 0;
  curr = 0;
  for (int i = 0; i <= n; i++) {
    arr[i].clear();
    dp[i] = 0;
    dp2[i] = 0;
  }
  for (int i = 0; i < n - 1; i++) {
    cin >> u >> v;
    arr[u].push_back(v);
    arr[v].push_back(u);
  }
  dfs(1, 0);
  ans = curr;
  dfs2(1, 0);
  cout << ans << "\n";
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long num;
  cin >> num;
  for (long long int i = 0; i < num; i++) {
    solve();
  }
}
