#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) {
  if (a > b) swap(a, b);
  if (a == 0) return b;
  return gcd(b % a, a);
}
void dfs(long long s, long long* cnt,
         vector<vector<pair<long long, long long>>>& g, long long p,
         long long* par) {
  par[s] = p;
  bool leaf = true;
  for (auto i : g[s]) {
    if (i.first == par[s]) leaf = false;
    dfs(i.first, cnt, g, s, par);
    cnt[s] += cnt[i.first];
  }
  if (leaf) cnt[s] = 1;
}
signed main() {
  long long t;
  cin >> t;
  while (t--) {
    long long m, d, w;
    cin >> m >> d >> w;
    long long u = min(m, d);
    long long w1 = w / gcd(d - 1, w);
    long long k = u / w1;
    cout << (k * u) - ((k * (k + 1)) / 2) * w1 << endl;
  }
  return 0;
}
