#include <bits/stdc++.h>
using namespace std;
long long q, m, d, w, p, n, s, t;
long long gcd(long long a, long long b) {
  if (a > b) swap(a, b);
  if (!a) return b;
  return gcd(a, b % a);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin >> q;
  while (q--) {
    cin >> m >> d >> w, p = w / gcd(w, d - 1), n = min(m, d), s = n / p,
                        t = n % p;
    cout << t * s * (s + 1) / 2 + (p - t) * s * (s - 1) / 2 << "\n";
  }
  return 0;
}
