#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n, m, k;
    cin >> n >> m >> k;
    int maxi = INT_MIN;
    for (int i = 0; i <= min(n / k, m); i++) {
      maxi = max(maxi, i - (k - 1 > 1 ? (m - i + k - 2) / (k - 1) : m - i));
    }
    cout << maxi << "\n";
  }
}
