#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
void solve() {
  long long m, d, w;
  cin >> m >> d >> w;
  w = w / gcd(d - 1, w);
  m = min(m, d);
  long long c = m / w;
  long long ans = c * m - c * (c + 1) / 2 * w;
  cout << ans << endl;
}
int main() {
  int t;
  scanf("%d", &t);
  while (t--) solve();
}
