#include <bits/stdc++.h>
using namespace std;
void solve() {
  int n, m, k;
  cin >> n >> m >> k;
  int one = n / k;
  cout << min(one, m) - max(0, (m - one + (k - 1) - 1) / (k - 1)) << endl;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int q;
  cin >> q;
  while (q--) {
    solve();
  }
}
