#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long int t, n, m, k;
  cin >> t;
  while (t--) {
    cin >> n >> m >> k;
    if (m == 0) {
      cout << 0 << endl;
      continue;
    }
    if (k == 1) {
      cout << m << endl;
    }
    long long int j = min(n / k, m);
    long long int rj = m - j;
    if (rj == 0) {
      cout << j << endl;
      continue;
    }
    long long int p = rj % (k - 1) == 0 ? rj / (k - 1) : rj / (k - 1) + 1;
    cout << j - p << endl;
  }
  return 0;
}
