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