#include <bits/stdc++.h>
using namespace std;
bool comparator(int a, int b) { return a > b; }
int main() {
  ;
  ios::sync_with_stdio(false);
  cin.tie(0);
  ;
  int t;
  cin >> t;
  while (t--) {
    int n, m, k;
    cin >> n >> m >> k;
    if (!m) {
      cout << 0 << endl;
      continue;
    }
    int omc = n / k;
    if (m <= omc) {
      cout << m << endl;
      continue;
    }
    int otj;
    m -= omc;
    if (m % (k - 1)) {
      otj = m / (k - 1);
      otj++;
    } else {
      otj = m / (k - 1);
    }
    cout << omc - otj << endl;
  }
}
