#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long t;
  double m, n, k;
  string s;
  cin >> t;
  while (t--) {
    cin >> n >> m >> k;
    if (m <= (n / k)) {
      cout << m << "\n";
    } else {
      cout << ((n / k) - ceil((m - (n / k)) / (k - 1))) << "\n";
    }
  }
}
