#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, m, k;
    cin >> n >> m >> k;
    int nk = n / k;
    if (nk >= m) {
      cout << m << endl;
    } else {
      int r = m - nk;
      int d = k - 1;
      if (r <= d) {
        cout << nk - 1 << endl;
      } else {
        if (r % d == 0) {
          cout << nk - r / d << endl;
        } else {
          cout << nk - (r / d + 1) << endl;
        }
      }
    }
  }
  return 0;
}
