#include <bits/stdc++.h>
using namespace std;
long long int gcd(long long int a, long long int b) {
  return b ? gcd(b, a % b) : a;
}
long long int power(long long int x, long long int y, long long int p) {
  long long int res = 1;
  x %= p;
  while (y > 0) {
    if (y & 1) res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
long long int lcm(long long int a, long long int b) {
  return (a / gcd(a, b)) * b;
}
long long int searchll(long long int val, vector<long long int> vec) {
  for (long long int i = 0; i < vec.size(); i++) {
    if (vec[i] == val) {
      return i;
    }
  }
  return -1;
}
bool isPrime(long long int n) {
  int broken = 0;
  for (long long int i = 2; i < sqrt(n) + 1; i++) {
    if (n % i == 0) {
      broken = 1;
      break;
    }
  }
  if (broken == 1) {
    return false;
  } else {
    return true;
  }
}
bool isPerfectSquare(long double x) {
  long double sr = sqrt(x);
  return ((sr - floor(sr)) == 0);
}
vector<bool> SieveOfEratosthenes(long long int num) {
  vector<bool> pno;
  pno.assign(num + 1, true);
  pno[0] = false;
  pno[1] = false;
  for (long long int i = 2; i * i <= num; i++) {
    if (pno[i] == true) {
      for (long long int j = i * 2; j <= num; j += i) pno[j] = false;
    }
  }
  return pno;
}
vector<long long int> SieveOfEratosthenesExtended(long long int num) {
  vector<long long int> pno(num + 1);
  for (long long int i = 0; i < num + 1; i++) {
    pno[i] = i;
  }
  for (long long int i = 2; i * i <= num; i++) {
    if (pno[i] == i) {
      for (long long int j = i * 2; j <= num; j += i)
        if (pno[j] == j) pno[j] = i;
    }
  }
  return pno;
}
vector<long long int> PrimeFactorize(long long int num) {
  vector<long long int> spf;
  spf = SieveOfEratosthenesExtended(num);
  set<long long int> pf;
  while (num != 1) {
    pf.insert(spf[num]);
    num = num / spf[num];
  }
  vector<long long int> v;
  for (auto it : pf) {
    v.push_back(it);
  }
  return v;
}
long long int calculateNcR(long long int n, long long int r) {
  long long int p = 1, k = 1;
  if (n - r < r) r = n - r;
  if (r != 0) {
    while (r) {
      p *= n;
      k *= r;
      long long int m = gcd(p, k);
      p /= m;
      k /= m;
      n--;
      r--;
    }
  } else
    p = 1;
  return p;
}
long long int stringToInt(string s) {
  long long int num = 0;
  for (long long int i = 0; i < s.length(); i++) {
    num = num * 10 + (s[i] - '0');
  }
  return num;
}
long long int BinarySearch(vector<long long int> arr, long long int num) {
  long long int left = 0, right = arr.size(), mid;
  while (left < right) {
    mid = (left + right) / 2;
    if (arr[mid] == num) {
      return mid;
    } else if (arr[mid] < num) {
      left = mid + 1;
    } else {
      right = mid;
    }
  }
  return left;
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long int t;
  cin >> t;
  while (t--) {
    long long int m, d, w;
    cin >> m >> d >> w;
    long long int fact;
    long long int g = gcd(w, d - 1);
    fact = w / g;
    long long int max_num = min(m, d);
    long long int pairs = 0;
    long long int rem = max_num % fact;
    long long int t = (max_num - (rem + 1)) / fact;
    if (fact == 1) {
    }
    pairs += fact * (t * (t + 1)) / 2;
    t = (max_num - rem) / fact;
    pairs += t * rem;
    cout << pairs << "\n";
  }
}

### Prompt

Your task is to create a Cpp solution to the following problem:

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 gcd(long long int a, long long int b) {
  return b ? gcd(b, a % b) : a;
}
long long int power(long long int x, long long int y, long long int p) {
  long long int res = 1;
  x %= p;
  while (y > 0) {
    if (y & 1) res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
long long int lcm(long long int a, long long int b) {
  return (a / gcd(a, b)) * b;
}
long long int searchll(long long int val, vector<long long int> vec) {
  for (long long int i = 0; i < vec.size(); i++) {
    if (vec[i] == val) {
      return i;
    }
  }
  return -1;
}
bool isPrime(long long int n) {
  int broken = 0;
  for (long long int i = 2; i < sqrt(n) + 1; i++) {
    if (n % i == 0) {
      broken = 1;
      break;
    }
  }
  if (broken == 1) {
    return false;
  } else {
    return true;
  }
}
bool isPerfectSquare(long double x) {
  long double sr = sqrt(x);
  return ((sr - floor(sr)) == 0);
}
vector<bool> SieveOfEratosthenes(long long int num) {
  vector<bool> pno;
  pno.assign(num + 1, true);
  pno[0] = false;
  pno[1] = false;
  for (long long int i = 2; i * i <= num; i++) {
    if (pno[i] == true) {
      for (long long int j = i * 2; j <= num; j += i) pno[j] = false;
    }
  }
  return pno;
}
vector<long long int> SieveOfEratosthenesExtended(long long int num) {
  vector<long long int> pno(num + 1);
  for (long long int i = 0; i < num + 1; i++) {
    pno[i] = i;
  }
  for (long long int i = 2; i * i <= num; i++) {
    if (pno[i] == i) {
      for (long long int j = i * 2; j <= num; j += i)
        if (pno[j] == j) pno[j] = i;
    }
  }
  return pno;
}
vector<long long int> PrimeFactorize(long long int num) {
  vector<long long int> spf;
  spf = SieveOfEratosthenesExtended(num);
  set<long long int> pf;
  while (num != 1) {
    pf.insert(spf[num]);
    num = num / spf[num];
  }
  vector<long long int> v;
  for (auto it : pf) {
    v.push_back(it);
  }
  return v;
}
long long int calculateNcR(long long int n, long long int r) {
  long long int p = 1, k = 1;
  if (n - r < r) r = n - r;
  if (r != 0) {
    while (r) {
      p *= n;
      k *= r;
      long long int m = gcd(p, k);
      p /= m;
      k /= m;
      n--;
      r--;
    }
  } else
    p = 1;
  return p;
}
long long int stringToInt(string s) {
  long long int num = 0;
  for (long long int i = 0; i < s.length(); i++) {
    num = num * 10 + (s[i] - '0');
  }
  return num;
}
long long int BinarySearch(vector<long long int> arr, long long int num) {
  long long int left = 0, right = arr.size(), mid;
  while (left < right) {
    mid = (left + right) / 2;
    if (arr[mid] == num) {
      return mid;
    } else if (arr[mid] < num) {
      left = mid + 1;
    } else {
      right = mid;
    }
  }
  return left;
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long int t;
  cin >> t;
  while (t--) {
    long long int m, d, w;
    cin >> m >> d >> w;
    long long int fact;
    long long int g = gcd(w, d - 1);
    fact = w / g;
    long long int max_num = min(m, d);
    long long int pairs = 0;
    long long int rem = max_num % fact;
    long long int t = (max_num - (rem + 1)) / fact;
    if (fact == 1) {
    }
    pairs += fact * (t * (t + 1)) / 2;
    t = (max_num - rem) / fact;
    pairs += t * rem;
    cout << pairs << "\n";
  }
}

```