#include <bits/stdc++.h>
using namespace std;
const long double pi = 3.14159265359;
const int maxn = 2e5 + 7;
const int mod = 1e9 + 7;
const long long INF = 9223372036854775807;
const int inf = 2e9 + 7;
int main() {
  int q;
  cin >> q;
  while (q--) {
    int n, m, k;
    cin >> n >> m >> k;
    int mxx = min(n / k, m);
    int rem = m - mxx;
    cout << mxx - (rem + k - 2) / (k - 1) << '\n';
  }
}
