#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
int main() {
  int T;
  cin >> T;
  while (T--) {
    long long m, d, w;
    cin >> m >> d >> w;
    if (d == 1)
      cout << 0 << endl;
    else {
      long long temp = w / gcd(d - 1, w);
      long long ans = 0;
      d = min(m, d);
      long long n1 = d / temp;
      ans = ((d - 1 * temp) + (d - n1 * temp)) * n1 / 2;
      cout << ans << endl;
    }
  }
  return 0;
}
