#include <bits/stdc++.h>
using namespace std;
using lint = long long;
constexpr lint inf = 1LL << 60;
constexpr lint mod = 1000000007;
int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int t;
  cin >> t;
  while (t--) {
    int n, m, k;
    cin >> n >> m >> k;
    int win = min(n / k, m);
    m -= win;
    int los = (m + k - 2) / (k - 1);
    cout << win - los << "\n";
  }
  return 0;
}
