#include <bits/stdc++.h>
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("O3")
#pragma GCC optimize("fast-math")
using namespace std;
const int INF = 1e9;
const long long INFll = 2e18;
const int BASE1 = 179;
const int BASE2 = 653;
const long long MOD = 1e9 + 7;
const int MAXN = 2e5;
const long double PI = 3.1415926535;
const long double EPS = 1e-6;
long long gcd(long long a, long long b) {
  while (b != 0) {
    a = a % b;
    swap(a, b);
  }
  return a;
}
void solve() {
  long long m, d, w;
  cin >> m >> d >> w;
  long long dopw = w / gcd(d - 1, w), q = min(d, m), n = q / dopw;
  cout << q * n - n * (n + 1) * dopw / 2 << "\n";
}
signed main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  cin >> t;
  while (t--) solve();
  return 0;
}
