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