#include <bits/stdc++.h>
using namespace std;
template <class T1, class T2>
ostream& operator<<(ostream& os, const pair<T1, T2>& a) {
  return os << "(" << a.first << "," << a.second << ")";
}
template <class T>
void pv(T a, T b) {
  for (T i = a; i != b; ++i) cout << (*i) << " ";
  cout << endl;
}
template <class T>
void chmin(T& a, const T& b) {
  if (a > b) a = b;
}
template <class T>
void chmax(T& a, const T& b) {
  if (a < b) a = b;
}
long long nextLong() {
  long long x;
  scanf("%lld", &x);
  return x;
}
long long gcd(long long a, long long b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
int main2() {
  long long M = nextLong();
  long long D = nextLong();
  long long W = nextLong();
  long long N = min(M, D);
  const long long R = (D % W - 1 + W) % W;
  if (R == 0) {
    long long ans = N * (N - 1) / 2;
    cout << ans << '\n';
    return 0;
  }
  long long g = gcd(R, W);
  long long S = W / g;
  long long m = (N - 1) / S;
  long long ans = m * N - S * (m + 1) * m / 2;
  cout << ans << '\n';
  return 0;
}
int main() {
  int t = nextLong();
  while (t--) {
    main2();
  }
  return 0;
}
