#include <bits/stdc++.h>
bool solve(int n) {
  for (int i = 2; i * i <= n; i++)
    if (n % i == 0) return false;
  return true;
}
bool sol(int n) {
  for (int i = 2; i <= 32; i++)
    if ((1 << i) == n) return true;
  return false;
}
using namespace std;
void solve() {
  int n, m, k;
  cin >> n >> m >> k;
  int first = min(n / k, m);
  int second = ceil((double)(m - first) / (k - 1));
  cout << first - second << '\n';
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
