#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1.0);
using namespace std;
bool isPrime(long long n) {
  if (n <= 1) return false;
  if (n <= 3) return true;
  if (n % 2 == 0 || n % 3 == 0) return false;
  for (int i = 5; i * i <= n; i = i + 6)
    if (n % i == 0 || n % (i + 2) == 0) return false;
  return true;
}
bool isPowerOf2(long long x) { return x && (!(x & (x - 1))); }
template <typename T>
T factorial(T n) {
  T f = 1;
  for (T i = 2; i <= n; i++) f *= i;
  return f;
}
template <typename T>
T gcd(T a, T b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
template <typename T>
T extgcd(T a, T b, T &x, T &y) {
  if (b == 0) {
    x = 1;
    y = 0;
    return a;
  }
  T x1, y1;
  T d = extgcd(b, a % b, x1, y1);
  x = y1;
  y = x1 - y1 * (a / b);
  return d;
}
template <typename T>
T mod_inverse(T a, T mod) {
  T x, y;
  extgcd(a, mod, x, y);
  return (mod + x % mod) % mod;
}
template <typename T>
T lcm(T a, T b) {
  return a * b / gcd(a, b);
}
template <typename T>
T digitSum(T number, T mod = 10) {
  int ans = 0;
  while (number) {
    ans += number % mod;
    number /= mod;
  }
  return ans;
}
class ECalendarAmbiguity {
 public:
  static constexpr int kStressCount = 0;
  static void generateTest(ostream &test) {}
  void solve(istream &cin, ostream &cout) {
    long long m, d, w;
    cin >> m >> d >> w;
    long long x = w / gcd(d - 1, w);
    long long res = 0;
    int i = 1;
    long long tmp = x;
    long long times = min(m, d) / tmp;
    res = times * min(m, d) - (times + 1) * times / 2 * tmp;
    cout << res << "\n";
  }
};
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  istream &in(cin);
  ostream &out(cout);
  out << fixed;
  out.precision(20);
  ECalendarAmbiguity solver;
  int n;
  in >> n;
  for (int i = 0; i < n; ++i) {
    solver.solve(in, out);
  }
}
