#include <bits/stdc++.h>
using namespace std;
int main(void) {
  int t;
  cin >> t;
  while (t--) {
    int n, m, k;
    cin >> n >> m >> k;
    if (m == 0) {
      cout << "0" << endl;
    } else if ((n / k) >= m) {
      cout << m << endl;
    } else {
      int x = n / k;
      if ((m - x) % (k - 1) == 0) {
        int y = (m - x) / (k - 1);
        cout << x - y << endl;
      } else {
        int y = ((m - x) / (k - 1)) + 1;
        cout << x - y << endl;
      }
    }
  }
}
