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