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