#include <bits/stdc++.h>
using namespace std;
template <class T>
void println(const vector<T> &data) {
  int n = data.size();
  cout << "values: [";
  for (int i = 0; i < data.size(); ++i) {
    if (i != (n - 1)) {
      cout << data[i] << ", ";
    } else {
      cout << data[i];
    }
  }
  cout << "]" << endl;
}
const int MOD = 1e9 + 7;
const int oo = int(1e9 + 20);
const int noo = -oo;
const long long inf = 2e18 + 11;
int solve() {
  int n, m, k;
  cin >> n >> m >> k;
  vector<int> player(k, 0);
  int num = int(n / k);
  if (num >= m) {
    return m;
  } else {
    player[0] = num;
    m -= num;
    int maxi = 0;
    int i = -1;
    while (m > 0) {
      i++;
      int index = (i % k) + 1;
      if (index >= k) {
        continue;
      }
      player[index] += 1;
      m--;
      maxi = max(maxi, player[index]);
    }
    return num - maxi;
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t;
  cin >> t;
  while (t--) {
    cout << solve() << '\n';
  }
  return 0;
}
