#include <bits/stdc++.h>
using namespace std;
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long int t;
  cin >> t;
  while (t--) {
    long long int n, m, k;
    cin >> n >> m >> k;
    long long int each = n / k;
    if (m > each) {
      long long int temp = each;
      long long int rem = m - each;
      if (rem % (k - 1) == 0)
        rem = rem / (k - 1);
      else
        rem = rem / (k - 1) + 1;
      if (rem == temp)
        cout << 0 << endl;
      else
        cout << (temp - rem) << endl;
    } else {
      cout << m << endl;
    }
  }
}
