#include <bits/stdc++.h>
using namespace std;
long long mod(long long a) {
  long long c = a % (long long)(1E9 + 7);
  if (c >= 0) return c;
  return c + (long long)(1E9 + 7);
}
long long inf = (long long)1E16;
long long max(long long a, long long b) {
  if (a < b)
    return b;
  else
    return a;
}
long long min(long long a, long long b) {
  if (a > b)
    return b;
  else
    return a;
}
void Max(long long &pos, long long val) { pos = max(pos, val); }
void Min(long long &pos, long long val) { pos = min(pos, val); }
void Add(long long &pos, long long val) { pos = mod(pos + val); }
long double EPS = 1E-9;
template <class T, class S>
inline bool chmax(T &a, S b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T, class S>
inline bool chmin(T &a, S b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
void mod_print(long long k) {
  long long P = 50000;
  for (long long y = 1; y <= P; y++) {
    long long x = mod(y * k);
    if (abs(x) <= P || x + P >= (long long)(1E9 + 7)) {
      if (x + P >= (long long)(1E9 + 7)) {
        x -= (long long)(1E9 + 7);
      }
      cout << x << "/" << y << "\n";
      return;
    }
  }
  cout << "nun"
       << "\n";
}
long long gcd(long long a, long long b) {
  if (a > b) swap(a, b);
  if (a == 0) return b;
  return gcd(b % a, a);
}
long long cal(long long k, long long t) { return t * k * (k - 1) / 2; }
bool anspro() {
  long long m, d, w;
  cin >> m >> d >> w;
  if (d == 1) {
    cout << 0 << "\n";
    return 0;
  }
  long long g = gcd(d - 1, w);
  w /= g;
  d = min(d, m);
  long long k = d / w;
  long long s = d % w;
  long long t = w - s;
  long long ans = cal(k, t) + cal(k + 1, s);
  cout << ans << "\n";
  return 0;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long que;
  cin >> que;
  while (que--) {
    anspro();
  }
  return 0;
}
