#include <bits/stdc++.h>
using namespace std;
const int mod = 1000000007;
void solve() {
  int n, m, k;
  cin >> n >> m >> k;
  if (m == 1) {
    cout << m << "\n";
    return;
  }
  if (n == k || m == 0) {
    cout << 0 << "\n";
    return;
  } else {
    int ev = n / k;
    if (m <= ev) {
      cout << m << "\n";
    } else {
      int mx = m - ev;
      int mn = (mx + k - 2) / (k - 1);
      cout << ev - mn << "\n";
    }
  }
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int64_t test = 1;
  cin >> test;
  for (int i = 0; i < test; i++) {
    solve();
  }
  return 0;
}
