#include <bits/stdc++.h>
using namespace std;
void swap(int &a, int &b) {
  int t = a;
  a = b;
  b = t;
}
int gcd_euclid(int a, int b) {
  if (a > b) swap(a, b);
  if (a == 0) return b;
  return gcd_euclid(b % a, a);
}
void test_case() {
  int m, d, w;
  long long count = 0, cnt = 0;
  scanf("%d%d%d", &m, &d, &w);
  long long gcd = gcd_euclid(d - 1, w);
  w = w / gcd;
  long long limit = (m < d) ? m : d;
  long long div = limit / w;
  long long tmp = 0;
  count = (2 * (limit - w) - w * (div - 1)) * div / 2;
  cout << count << endl;
}
int main(int argc, char const *argv[]) {
  int t;
  scanf("%d", &t);
  while (t--) test_case();
  return 0;
}
