#include <bits/stdc++.h>
using namespace std;
int main() {
  int t = 0;
  cin >> t;
  for (int i = 0; i < t; i++) {
    int n = 0, m = 0, k = 0;
    cin >> n >> m >> k;
    int group = n / k;
    if (group > m) {
      cout << m << endl;
    } else {
      int remain = m - group;
      if (remain % (k - 1) == 0) {
        cout << group - remain / (k - 1) << endl;
      } else {
        cout << group - remain / (k - 1) - 1 << endl;
      }
    }
  }
}
