#include <bits/stdc++.h>
using namespace std;
bool sortbysec(const pair<int, int> &a, const pair<int, int> &b) {
  return (a.second < b.second);
}
bool compare(const pair<string, int> &a, const pair<string, int> &b) {
  return a.second < b.second;
}
double logn(double n, double r) { return (n > r - 1) ? 1 + logn(n / r, r) : 0; }
vector<long long int> pwx(32);
void cal(long long int x) {
  pwx[0] = 1;
  for (long long int i = 1; i < 32; i++) pwx[i] = x * pwx[i - 1];
}
void solve() {
  long long int n, m, k;
  cin >> n >> m >> k;
  long long int numcards = n / k;
  if (m <= numcards) {
    cout << m << "\n";
    return;
  }
  if (n == k || n == m) {
    cout << 0 << "\n";
    return;
  }
  long long int rem = m - numcards;
  if (rem % (k - 1)) {
    cout << numcards - rem / (k - 1) - 1 << "\n";
  } else {
    cout << numcards - rem / (k - 1) << "\n";
  }
}
int main() {
  int t = 1, k = 1;
  cin >> t;
  while (t--) {
    solve();
    k++;
  }
}
