#include <bits/stdc++.h>
using namespace std;
long long int max(long long int a, long long int b) {
  if (a > b) return a;
  return b;
}
long long int min(long long int a, long long int b) {
  if (a < b) return a;
  return b;
}
string sing(char x) {
  string s(1, x);
  return s;
}
bool cmpDec(long long int a, long long int b) {
  if (a >= b) return true;
  return false;
}
void print1D(long long int a[], long long int n) {
  for (long long int i = 0; i < n; i++) cout << a[i] << " ";
  cout << "\n";
}
void print2D(long long int **dp, long long int n, long long int m) {
  for (long long int i = 0; i < n; i++) {
    for (long long int j = 0; j < m; j++) cout << dp[i][j] << " ";
    cout << "\n";
  }
}
void init1D(long long int a[], long long int n, long long int k) {
  for (long long int z = 0; z < n; z++) a[z] = k;
}
void init2D(long long int **a, long long int n, long long int m,
            long long int k) {
  for (long long int i = 0; i < n; i++)
    for (long long int j = 0; j < m; j++) a[i][j] = k;
}
long long int fastmod(long long int base, long long int exp, long long int m) {
  long long int res = 1;
  while (exp) {
    if (exp & 1ll) res = (res * base) % m;
    base = (base * base) % m;
    exp /= 2;
  }
  return res;
}
long long int gcd(long long int a, long long int b) {
  if (a == 0) return b;
  return gcd(b % a, a);
}
long long int logBase(long long int m, long long int b) {
  long long int count = 0;
  while (m > 1) {
    ++count;
    m = m / b;
  }
  return count;
}
long long int spf[1000000 + 1];
void sieve() {
  spf[1] = 1;
  for (long long int i = 2; i < 1000000 + 1; i++) spf[i] = i;
  for (long long int i = 4; i < 1000000 + 1; i += 2) spf[i] = 2;
  for (long long int i = 3; i * i < 1000000 + 1; i++) {
    if (spf[i] == i) {
      for (long long int j = i * i; j < 1000000 + 1; j += i)
        if (spf[j] == j) spf[j] = i;
    }
  }
}
vector<long long int> getFactorization(long long int x) {
  vector<long long int> ret;
  while (x != 1) {
    ret.push_back(spf[x]);
    x = x / spf[x];
  }
  return ret;
}
long long int sumofFactors(long long int n) {
  long long int res = 1;
  for (long long int i = 2; i <= sqrt(n); i++) {
    long long int curr_sum = 1;
    long long int curr_term = 1;
    while (n % i == 0) {
      n = n / i;
      curr_term *= i;
      curr_sum += curr_term;
    }
    res *= curr_sum;
  }
  if (n >= 2) res *= (1 + n);
  return res;
}
void SUM(long long int *a, long long int n) {
  long long int sum = 0;
  for (long long int i = 0; i < n; i++) sum += a[i];
  cout << sum << "\n";
}
void addEdge(vector<long long int> adj[], long long int u, long long int v) {
  adj[u].push_back(v);
  adj[v].push_back(u);
}
void printGraph(vector<long long int> adj[], long long int V) {
  for (long long int v = 1; v < V; ++v) {
    cout << "\n Adjacency list of vertex " << v << "\n head (" << v << ")";
    for (long long int x = 0; x < adj[v].size(); x++)
      cout << " -> " << adj[v][x];
    printf("\n");
  }
}
bool cycle(vector<long long int> adj[], long long int vis[], long long int n,
           long long int parent) {
  vis[n] = 1;
  if (adj[n].empty()) return false;
  bool value = false;
  for (long long int i = 0; i < adj[n].size(); i++) {
    if (adj[n][i] != parent) {
      if (vis[adj[n][i]] == 1)
        return true;
      else
        value |= cycle(adj, vis, adj[n][i], n);
    }
  }
  return value;
}
typedef struct num {
  long long int index;
  long long int value;
} num;
long long int countFactors(long long int n) {
  long long int count = 0;
  long long int ans = 1;
  for (long long int i = 2; i < sqrt(n) + 1; i++) {
    count = 0;
    while (n % i == 0) {
      n /= i;
      ++count;
    }
    ans *= count;
  }
  if (n > 1) ans *= 2;
  return ans;
}
long long int kthFactor(long long int n, long long int k) {
  vector<long long int> v;
  for (long long int i = 2; i < (long long int)sqrt(n) + 1; i++) {
    if (n % i == 0) {
      v.push_back(i);
      if (i < n / i) v.push_back(n / i);
    }
  }
  v.push_back(1);
  v.push_back(n);
  sort(v.begin(), v.end());
  if (v.size() < k) return -1;
  return v[k - 1];
}
long long int binSmaller(long long int a[], long long int n, long long int k) {
  long long int l, r;
  l = 0;
  r = n - 1;
  long long int mid = (l + r) / 2;
  if (a[r] <= k) return r;
  if (a[l] > k) return -1;
  while (l < r) {
    if (a[mid] > k && a[mid - 1] <= k) return mid - 1;
    if (k >= a[mid] && a[mid + 1] > k) return mid;
    if (k < a[mid])
      r = mid - 1;
    else
      l = mid + 1;
    mid = (l + r) / 2;
  }
  return -1;
}
long long int binarySearchCount(long long int arr[], long long int n,
                                long long int key) {
  long long int left = 0;
  long long int right = n - 1;
  long long int count = 0;
  long long int mid = 0;
  while (left <= right) {
    mid = (right + left) / 2;
    if (arr[mid] <= key) {
      count = mid + 1;
      left = mid + 1;
    } else
      right = mid - 1;
  }
  return count;
}
long long int binomialCoeff(long long int n, long long int k) {
  long long int res = 1;
  if (k > n - k) k = n - k;
  for (long long int i = 0; i < k; ++i) {
    res *= (n - i);
    res /= (i + 1);
  }
  return res;
}
long long int modInverse(long long int a,
                         long long int m = (long long int)(1000000000 + 7)) {
  long long int m0 = m, y = 0, x = 1;
  if (m == 1) return 0;
  while (a > 1) {
    long long int q = a / m;
    long long int t = m;
    m = a % m, a = t;
    t = y;
    y = x - q * y;
    x = t;
  }
  if (x < 0) x += m0;
  return x;
}
const int N = 10e5 + 1;
long long int factorialNumInverse[N + 1];
long long int naturalNumInverse[N + 1];
long long int fact[N + 1];
void InverseofNumber(long long int p) {
  naturalNumInverse[0] = naturalNumInverse[1] = 1;
  for (long long int i = 2; i <= N; i++)
    naturalNumInverse[i] = naturalNumInverse[p % i] * (p - p / i) % p;
}
void InverseofFactorial(long long int p) {
  factorialNumInverse[0] = factorialNumInverse[1] = 1;
  for (long long int i = 2; i <= N; i++)
    factorialNumInverse[i] =
        (naturalNumInverse[i] * factorialNumInverse[i - 1]) % p;
}
void factorial(long long int p) {
  fact[0] = 1;
  for (long long int i = 1; i <= N; i++) {
    fact[i] = (fact[i - 1] * i) % p;
  }
}
long long int nCr(long long int N, long long int R,
                  long long int p = (long long int)(1000000000 + 7)) {
  if (R == 0) return 1;
  long long int ans =
      ((fact[N] * factorialNumInverse[R]) % p * factorialNumInverse[N - R]) % p;
  return ans;
}
long long int numberOfPaths(long long int m, long long int n) {
  long long int path = 1;
  for (long long int i = n; i < (m + n - 1); i++) {
    path *= i;
    path /= (i - n + 1);
  }
  return path;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long int t;
  cin >> t;
  while (t--) {
    long long int m, d, w;
    cin >> m >> d >> w;
    w /= gcd(d - 1, w);
    m = min(d, m);
    long long int q = 0;
    q = (m) / w;
    long long int ans = (m)*q - ((q * (q + 1)) / 2) * w;
    cout << ans << "\n";
  }
}

### Prompt

Please provide a CPP coded solution to the problem described below:

Berland year consists of m months with d days each. Months are numbered from 1 to m. Berland week consists of w days. The first day of the year is also the first day of the week. Note that the last week of the year might be shorter than w days.

A pair (x, y) such that x < y is ambiguous if day x of month y is the same day of the week as day y of month x.

Count the number of ambiguous pairs.

Input

The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of testcases.

Each of the next t lines contains three integers m, d and w (1 ≤ m, d, w ≤ 10^9) — the number of months in a year, the number of days in a month and the number of days in a week.

Output

Print t integers — for each testcase output the number of pairs (x, y) such that x < y and day x of month y is the same day of the week as day y of month x.

Example

Input


5
6 7 4
10 7 12
12 30 7
1 1 1
3247834 10298779 625324


Output


6
9
5
0
116461800

Note

Here are the pairs for the first test case: 

<image>

### Response

```cpp
#include <bits/stdc++.h>
using namespace std;
long long int max(long long int a, long long int b) {
  if (a > b) return a;
  return b;
}
long long int min(long long int a, long long int b) {
  if (a < b) return a;
  return b;
}
string sing(char x) {
  string s(1, x);
  return s;
}
bool cmpDec(long long int a, long long int b) {
  if (a >= b) return true;
  return false;
}
void print1D(long long int a[], long long int n) {
  for (long long int i = 0; i < n; i++) cout << a[i] << " ";
  cout << "\n";
}
void print2D(long long int **dp, long long int n, long long int m) {
  for (long long int i = 0; i < n; i++) {
    for (long long int j = 0; j < m; j++) cout << dp[i][j] << " ";
    cout << "\n";
  }
}
void init1D(long long int a[], long long int n, long long int k) {
  for (long long int z = 0; z < n; z++) a[z] = k;
}
void init2D(long long int **a, long long int n, long long int m,
            long long int k) {
  for (long long int i = 0; i < n; i++)
    for (long long int j = 0; j < m; j++) a[i][j] = k;
}
long long int fastmod(long long int base, long long int exp, long long int m) {
  long long int res = 1;
  while (exp) {
    if (exp & 1ll) res = (res * base) % m;
    base = (base * base) % m;
    exp /= 2;
  }
  return res;
}
long long int gcd(long long int a, long long int b) {
  if (a == 0) return b;
  return gcd(b % a, a);
}
long long int logBase(long long int m, long long int b) {
  long long int count = 0;
  while (m > 1) {
    ++count;
    m = m / b;
  }
  return count;
}
long long int spf[1000000 + 1];
void sieve() {
  spf[1] = 1;
  for (long long int i = 2; i < 1000000 + 1; i++) spf[i] = i;
  for (long long int i = 4; i < 1000000 + 1; i += 2) spf[i] = 2;
  for (long long int i = 3; i * i < 1000000 + 1; i++) {
    if (spf[i] == i) {
      for (long long int j = i * i; j < 1000000 + 1; j += i)
        if (spf[j] == j) spf[j] = i;
    }
  }
}
vector<long long int> getFactorization(long long int x) {
  vector<long long int> ret;
  while (x != 1) {
    ret.push_back(spf[x]);
    x = x / spf[x];
  }
  return ret;
}
long long int sumofFactors(long long int n) {
  long long int res = 1;
  for (long long int i = 2; i <= sqrt(n); i++) {
    long long int curr_sum = 1;
    long long int curr_term = 1;
    while (n % i == 0) {
      n = n / i;
      curr_term *= i;
      curr_sum += curr_term;
    }
    res *= curr_sum;
  }
  if (n >= 2) res *= (1 + n);
  return res;
}
void SUM(long long int *a, long long int n) {
  long long int sum = 0;
  for (long long int i = 0; i < n; i++) sum += a[i];
  cout << sum << "\n";
}
void addEdge(vector<long long int> adj[], long long int u, long long int v) {
  adj[u].push_back(v);
  adj[v].push_back(u);
}
void printGraph(vector<long long int> adj[], long long int V) {
  for (long long int v = 1; v < V; ++v) {
    cout << "\n Adjacency list of vertex " << v << "\n head (" << v << ")";
    for (long long int x = 0; x < adj[v].size(); x++)
      cout << " -> " << adj[v][x];
    printf("\n");
  }
}
bool cycle(vector<long long int> adj[], long long int vis[], long long int n,
           long long int parent) {
  vis[n] = 1;
  if (adj[n].empty()) return false;
  bool value = false;
  for (long long int i = 0; i < adj[n].size(); i++) {
    if (adj[n][i] != parent) {
      if (vis[adj[n][i]] == 1)
        return true;
      else
        value |= cycle(adj, vis, adj[n][i], n);
    }
  }
  return value;
}
typedef struct num {
  long long int index;
  long long int value;
} num;
long long int countFactors(long long int n) {
  long long int count = 0;
  long long int ans = 1;
  for (long long int i = 2; i < sqrt(n) + 1; i++) {
    count = 0;
    while (n % i == 0) {
      n /= i;
      ++count;
    }
    ans *= count;
  }
  if (n > 1) ans *= 2;
  return ans;
}
long long int kthFactor(long long int n, long long int k) {
  vector<long long int> v;
  for (long long int i = 2; i < (long long int)sqrt(n) + 1; i++) {
    if (n % i == 0) {
      v.push_back(i);
      if (i < n / i) v.push_back(n / i);
    }
  }
  v.push_back(1);
  v.push_back(n);
  sort(v.begin(), v.end());
  if (v.size() < k) return -1;
  return v[k - 1];
}
long long int binSmaller(long long int a[], long long int n, long long int k) {
  long long int l, r;
  l = 0;
  r = n - 1;
  long long int mid = (l + r) / 2;
  if (a[r] <= k) return r;
  if (a[l] > k) return -1;
  while (l < r) {
    if (a[mid] > k && a[mid - 1] <= k) return mid - 1;
    if (k >= a[mid] && a[mid + 1] > k) return mid;
    if (k < a[mid])
      r = mid - 1;
    else
      l = mid + 1;
    mid = (l + r) / 2;
  }
  return -1;
}
long long int binarySearchCount(long long int arr[], long long int n,
                                long long int key) {
  long long int left = 0;
  long long int right = n - 1;
  long long int count = 0;
  long long int mid = 0;
  while (left <= right) {
    mid = (right + left) / 2;
    if (arr[mid] <= key) {
      count = mid + 1;
      left = mid + 1;
    } else
      right = mid - 1;
  }
  return count;
}
long long int binomialCoeff(long long int n, long long int k) {
  long long int res = 1;
  if (k > n - k) k = n - k;
  for (long long int i = 0; i < k; ++i) {
    res *= (n - i);
    res /= (i + 1);
  }
  return res;
}
long long int modInverse(long long int a,
                         long long int m = (long long int)(1000000000 + 7)) {
  long long int m0 = m, y = 0, x = 1;
  if (m == 1) return 0;
  while (a > 1) {
    long long int q = a / m;
    long long int t = m;
    m = a % m, a = t;
    t = y;
    y = x - q * y;
    x = t;
  }
  if (x < 0) x += m0;
  return x;
}
const int N = 10e5 + 1;
long long int factorialNumInverse[N + 1];
long long int naturalNumInverse[N + 1];
long long int fact[N + 1];
void InverseofNumber(long long int p) {
  naturalNumInverse[0] = naturalNumInverse[1] = 1;
  for (long long int i = 2; i <= N; i++)
    naturalNumInverse[i] = naturalNumInverse[p % i] * (p - p / i) % p;
}
void InverseofFactorial(long long int p) {
  factorialNumInverse[0] = factorialNumInverse[1] = 1;
  for (long long int i = 2; i <= N; i++)
    factorialNumInverse[i] =
        (naturalNumInverse[i] * factorialNumInverse[i - 1]) % p;
}
void factorial(long long int p) {
  fact[0] = 1;
  for (long long int i = 1; i <= N; i++) {
    fact[i] = (fact[i - 1] * i) % p;
  }
}
long long int nCr(long long int N, long long int R,
                  long long int p = (long long int)(1000000000 + 7)) {
  if (R == 0) return 1;
  long long int ans =
      ((fact[N] * factorialNumInverse[R]) % p * factorialNumInverse[N - R]) % p;
  return ans;
}
long long int numberOfPaths(long long int m, long long int n) {
  long long int path = 1;
  for (long long int i = n; i < (m + n - 1); i++) {
    path *= i;
    path /= (i - n + 1);
  }
  return path;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long int t;
  cin >> t;
  while (t--) {
    long long int m, d, w;
    cin >> m >> d >> w;
    w /= gcd(d - 1, w);
    m = min(d, m);
    long long int q = 0;
    q = (m) / w;
    long long int ans = (m)*q - ((q * (q + 1)) / 2) * w;
    cout << ans << "\n";
  }
}

```