#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n, m, k;
    cin >> n >> m >> k;
    if (m == n) {
      cout << 0 << "\n";
      continue;
    }
    int c = n / k;
    if (m <= c) {
      cout << m << "\n";
      continue;
    } else {
      m -= c;
      if (m % (k - 1) == 0) {
        cout << (c - (m / (k - 1))) << "\n";
      } else {
        int ma = 0;
        k = k - 1;
        while (m > 0) {
          if (k == 0) break;
          int x = m / k;
          if (x > ma) ma = x;
          m -= x;
          k -= 1;
        }
        cout << c - ma << "\n";
      }
    }
  }
}
