#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  long long t;
  cin >> t;
  while (t--) {
    long long n, m, k;
    cin >> n >> m >> k;
    if (m == 0) {
      cout << "0\n";
      continue;
    }
    long long each_ = n / k;
    if (m <= each_) {
      cout << m << endl;
      continue;
    }
    m -= each_;
    k--;
    long long maxi = (m % k == 0) ? m / k : m / k + 1;
    cout << each_ - maxi << endl;
  }
}
