/*
 * @Author: Kaizyn
 * @Date: 2020-07-29 23:36:19
 * @LastEditTime: 2020-07-30 00:05:21
 */
#include <bits/stdc++.h>

// #define DEBUG

using namespace std;

const double eps = 1e-7;
const double PI = acos(-1);
typedef pair<int, int> pii;
const int MOD = 998244353;
const int INF = 0x3f3f3f3f;
// const int N = ;

long long m, d, w;

inline void solve() {
  cin >> m >> d >> w;
  long long n = min(m, d);
  if (d%w == 1 || w == 1) return cout << n*(n-1)/2 << endl, void();
  // (y-x)*d+x-y === 0 (%w)
  // (y-x)*(d-1) === 0 (%w)
  long long k = d-1;
  long long g = __gcd(k, w);
  long long t = w/g; // t*k == lcm(k, w)
  long long c = (n-1)/t; // c*t < n
  long long res = n*c-(t+c*t)*c/2;
  cout << res << endl;
}

signed main() {
  ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
  int T = 1;
  cin >> T;
  for (int t = 1; t <= T; ++t) {
    solve();
  }
  return 0;
}


/*
 * @Author: Kaizyn
 * @Date: 2020-07-29 23:36:19
 * @LastEditTime: 2020-07-30 00:04:12
 */
#include <bits/stdc++.h>

// #define DEBUG

using namespace std;

const double eps = 1e-7;
const double PI = acos(-1);
typedef pair<int, int> pii;
const int MOD = 998244353;
const int INF = 0x3f3f3f3f;
// const int N = ;

long long m, d, w;

inline void solve() {
  cin >> m >> d >> w;
  long long n = min(m, d);
  if (d%w == 1 || w == 1) return cout << n*(n+1)/2 << endl, void();
  // (y-x)*d+x-y === 0 (%w)
  // (y-x)*(d-1) === 0 (%w)
  long long k = d-1;
  long long g = __gcd(k, w);
  long long t = w/g; // t*k == lcm(k, w)
  long long c = (n-1)/t; // c*t < n
  long long res = n*c-(t+c*t)*c/2;
  cout << res << endl;
}

signed main() {
  ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
  int T = 1;
  cin >> T;
  for (int t = 1; t <= T; ++t) {
    solve();
  }
  return 0;
}


/*
 * @Author: Kaizyn
 * @Date: 2020-07-29 23:36:19
 * @LastEditTime: 2020-07-29 23:58:25
 */
#include <bits/stdc++.h>

// #define DEBUG

using namespace std;

const double eps = 1e-7;
const double PI = acos(-1);
typedef pair<int, int> pii;
const int MOD = 998244353;
const int INF = 0x3f3f3f3f;
// const int N = ;

long long m, d, w;

inline void solve() {
  cin >> m >> d >> w;
  long long n = min(m, d);
  if (d%w == 1) return cout << n*(n+1)/2 << endl, void();
  long long k = d-1;
  long long g = __gcd(k, w);
  // long long lcm = k/g*w;
  long long t = w/g;
  long long c = (n-1)/t; // c*t < n
  long long res = n*c-(t+c*t)*c/2;
  cout << res << endl;
}

signed main() {
  ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
  int T = 1;
  cin >> T;
  for (int t = 1; t <= T; ++t) {
    solve();
  }
  return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int t;
ll m,d,w,up,ans;
int main(){
    scanf("%d",&t);
    while(t--){
        scanf("%lld%lld%lld",&m,&d,&w);
        up=min(m,d);
        w/=__gcd(w,d-1);
        ans=(up%w)*((up-1)/w);
        up=up/w*w;
        ans+=(((up-1)/w)*(1+(up-1)/w)/2)*w;
        printf("%lld\n",ans);
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int t;
ll m,d,w,up,ans;
int main(){
    scanf("%d",&t);
    while(t--){
        scanf("%lld%lld%lld",&m,&d,&w);
        up=min(m,d);
        ans=0;
        if((d-1)%w==0){
            printf("%lld\n",up*(up-1)/2);
            continue;
        }
        w/=__gcd(w,d-1);
        //printf("w:%lld\n",w);
        if(up%w==0){
            ans=(((up-1)/w)*(1+(up-1)/w)/2)*w;
        }
        else{
            ans=(up%w)*((up-1)/w);
            up=up/w*w;
            ans+=(((up-1)/w)*(1+(up-1)/w)/2)*w;
        }
        printf("%lld\n",ans);
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define int long long
void read (int &x) {
    char ch = getchar(); x = 0;
    while (!isdigit(ch)) ch = getchar();
    while (isdigit(ch)) x = x * 10 + ch - 48, ch = getchar();
}
int g (int x, int y) { return y ? g (y, x % y) : x; }
signed main() {
    int T; read (T);
    while (T--) {
        int m, d, w; read (m), read (d), read (w);
        int p = d - 1, q = w;
        for (int i = 2; i * i <= p; ++i) {
            while (p % i == 0 && q % i == 0) p /= i, q /= i;
        } if (p > 1 && q % p == 0) q /= p, p = 1;
        // int qaq = g (d - 1, w), q = w * g (d - 1, w) / (d - 1);
        int mn = min (m, d), res = 0;
        int num = mn / q, qwq = mn % q;
        if (!qwq) res = q * num * (num - 1) >> 1;
        else res = (qwq * num * (num + 1) >> 1) + ((q - qwq) * num * (num - 1) >> 1);
        printf ("%lld\n", res);
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define int long long
void read (int &x) {
    char ch = getchar(); x = 0;
    while (!isdigit(ch)) ch = getchar();
    while (isdigit(ch)) x = x * 10 + ch - 48, ch = getchar();
}
int g (int x, int y) { return y ? g (y, x % y) : x; }
signed main() {
    int T; read (T);
    while (T--) {
        int m, d, w; read (m), read (d), read (w);
        int q = w / g (w, d - 1);
        int mn = min (m, d), res = 0;
        int num = mn / q, qwq = mn % q;
        if (!qwq) res = q * num * (num - 1) >> 1;
        else res = (qwq * num * (num + 1) >> 1) + ((q - qwq) * num * (num - 1) >> 1);
        printf ("%lld\n", res);
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define int long long
void read (int &x) {
    char ch = getchar(); x = 0;
    while (!isdigit(ch)) ch = getchar();
    while (isdigit(ch)) x = x * 10 + ch - 48, ch = getchar();
}
int g (int x, int y) { return y ? g (y, x % y) : x; }
signed main() {
    int T; read (T); int cnt = 0;
    while (T--) {
        ++cnt;
        int m, d, w; read (m), read (d), read (w);
        if (cnt == 163) printf ("%lld111%lld111%lld\n", m, d, w);
        int p = d - 1, q = w;
        for (int i = 2; i * i <= p; ++i) {
            while (p % i == 0 && q % i == 0) p /= i, q /= i;
        } if (p > 1 && q % p == 0) q /= p, p = 1;
        // int qaq = g (d - 1, w), q = w * g (d - 1, w) / (d - 1);
        int mn = min (m, d), res = 0;
        int num = mn / q, qwq = mn % q;
        if (!qwq) res = q * num * (num - 1) >> 1;
        else res = (qwq * num * (num + 1) >> 1) + ((q - qwq) * num * (num - 1) >> 1);
        printf ("%lld\n", res);
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define int long long
void read (int &x) {
    char ch = getchar(); x = 0;
    while (!isdigit(ch)) ch = getchar();
    while (isdigit(ch)) x = x * 10 + ch - 48, ch = getchar();
}
int g (int x, int y) { return y ? g (y, x % y) : x; }
signed main() {
    int T; read (T); int cnt = 0;
    while (T--) {
        ++cnt;
        int m, d, w; read (m), read (d), read (w);
        if (cnt == 163) printf ("%lld %lld %lld\n", m, d, w);
        int p = d - 1, q = w;
        for (int i = 2; i * i <= p; ++i) {
            while (p % i == 0 && q % i == 0) p /= i, q /= i;
        } if (p > 1 && q % p == 0) q /= p, p = 1;
        // int qaq = g (d - 1, w), q = w * g (d - 1, w) / (d - 1);
        int mn = min (m, d), res = 0;
        int num = mn / q, qwq = mn % q;
        if (!qwq) res = q * num * (num - 1) >> 1;
        else res = (qwq * num * (num + 1) >> 1) + ((q - qwq) * num * (num - 1) >> 1);
        printf ("%lld\n", res);
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define int long long
void read (int &x) {
    char ch = getchar(); x = 0;
    while (!isdigit(ch)) ch = getchar();
    while (isdigit(ch)) x = x * 10 + ch - 48, ch = getchar();
}
int g (int x, int y) { return y ? g (y, x % y) : x; }

/*
1
4 7 9
*/
signed main() {
    int T; read (T);
    while (T--) {
        int m, d, w; read (m), read (d), read (w);
        int p = d - 1, q = w;
        for (int i = 2; i * i <= p; ++i) {
            int cnt = 0;
            while (p % i == 0) p /= i, ++cnt;
            while (q % i == 0 && cnt) q /= i, --cnt;
        } if (p > 1 && q % p == 0) q /= p, p = 1;
        if (q > 1 && p % q == 0) q = 1;
        int mn = min (m, d), res = 0;
        int num = mn / q, qwq = mn % q;
        if (!qwq) res = q * num * (num - 1) >> 1;
        else res = (qwq * num * (num + 1) >> 1) + ((q - qwq) * num * (num - 1) >> 1);
        printf ("%lld\n", res);
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define int long long
void read (int &x) {
    char ch = getchar(); x = 0;
    while (!isdigit(ch)) ch = getchar();
    while (isdigit(ch)) x = x * 10 + ch - 48, ch = getchar();
}
int g (int x, int y) { return y ? g (y, x % y) : x; }

/*
1
4 7 9
*/
signed main() {
    int T; read (T); int cnt = 0;
    while (T--) {
        ++cnt;
        int m, d, w; read (m), read (d), read (w);
        // if (cnt == 369) printf ("%lld111%lld111%lld\n", m, d, w);
        int p = d - 1, q = w;
        for (int i = 2; i * i <= p; ++i) {
            int cnt = 0;
            while (p % i == 0) p /= i, ++cnt;
            while (q % i == 0 && cnt) q /= i, --cnt;
        } if (p > 1 && q % p == 0) q /= p, p = 1;
        if (q > 1 && p % q == 0) q = 1;
        // int qaq = g (d - 1, w), q = w * g (d - 1, w) / (d - 1);
        // printf ("666 %lld\n", q);
        int mn = min (m, d), res = 0;
        int num = mn / q, qwq = mn % q;
        if (!qwq) res = q * num * (num - 1) >> 1;
        else res = (qwq * num * (num + 1) >> 1) + ((q - qwq) * num * (num - 1) >> 1);
        printf ("%lld\n", res);
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define int long long
void read (int &x) {
    char ch = getchar(); x = 0;
    while (!isdigit(ch)) ch = getchar();
    while (isdigit(ch)) x = x * 10 + ch - 48, ch = getchar();
}
int g (int x, int y) { return y ? g (y, x % y) : x; }

/*
1
2 7 3
*/
signed main() {
    int T; read (T); int cnt = 0;
    while (T--) {
        ++cnt;
        int m, d, w; read (m), read (d), read (w);
        if (cnt == 369) printf ("%lld111%lld111%lld\n", m, d, w);
        int p = d - 1, q = w;
        for (int i = 2; i * i <= p; ++i) {
            while (p % i == 0 && q % i == 0) p /= i, q /= i;
        } if (p > 1 && q % p == 0) q /= p, p = 1;
        if (q > 1 && p % q == 0) q = 1;
        // int qaq = g (d - 1, w), q = w * g (d - 1, w) / (d - 1);
        // printf ("666 %lld\n", q);
        int mn = min (m, d), res = 0;
        int num = mn / q, qwq = mn % q;
        if (!qwq) res = q * num * (num - 1) >> 1;
        else res = (qwq * num * (num + 1) >> 1) + ((q - qwq) * num * (num - 1) >> 1);
        printf ("%lld\n", res);
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define int long long
void read (int &x) {
    char ch = getchar(); x = 0;
    while (!isdigit(ch)) ch = getchar();
    while (isdigit(ch)) x = x * 10 + ch - 48, ch = getchar();
}
int g (int x, int y) { return y ? g (y, x % y) : x; }

/*
1
2 7 3
*/
signed main() {
    int T; read (T); //int cnt = 0;
    while (T--) {
        // ++cnt;
        int m, d, w; read (m), read (d), read (w);
        // if (cnt == 163) printf ("%lld111%lld111%lld\n", m, d, w);
        int p = d - 1, q = w;
        for (int i = 2; i * i <= p; ++i) {
            while (p % i == 0 && q % i == 0) p /= i, q /= i;
        } if (p > 1 && q % p == 0) q /= p, p = 1;
        if (q > 1 && p % q == 0) q = 1;
        // int qaq = g (d - 1, w), q = w * g (d - 1, w) / (d - 1);
        // printf ("666 %lld\n", q);
        int mn = min (m, d), res = 0;
        int num = mn / q, qwq = mn % q;
        if (!qwq) res = q * num * (num - 1) >> 1;
        else res = (qwq * num * (num + 1) >> 1) + ((q - qwq) * num * (num - 1) >> 1);
        printf ("%lld\n", res);
    }
    return 0;
}


#include<iostream>
#include<bits/stdc++.h>
#include<stdio.h>
#include<stdlib.h>
#include<vector>
#include<map>
#include<queue>
using namespace std;
#define N 201000
typedef long long LL;
LL m,d,w;
LL gcd(LL a,LL b){
 if(a%b==0)
  return b;
 return gcd(b,a%b);
}
int main()
{
	int T;
	cin>>T;
	while(T--)
	{
		LL ans=0;
		scanf("%lld %lld %lld",&m,&d,&w);
		if(m<=1||d<=1||w<=1)
		{
			cout<<0<<endl;
			continue;
		}
		LL Gcd=gcd(d-1,w);
		w/=Gcd;
		LL min=d<m?d:m;
//		cout<<w<<" "<<min<<endl;
		for(LL i=1;i*w<min;++i)
		{
			ans+=min-i*w;
//			cout<<ans<<endl;
		}
		cout<<ans<<endl;
	}
}


#include<iostream>
#include<bits/stdc++.h>
#include<stdio.h>
#include<stdlib.h>
#include<vector>
#include<map>
#include<queue>
using namespace std;
#define N 201000
typedef long long LL;
LL m,d,w;
LL gcd(LL a,LL b){
 if(a%b==0)
  return b;
 return gcd(b,a%b);
}
int main()
{
	int T;
	cin>>T;
	while(T--)
	{
		LL ans=0;
		scanf("%lld %lld %lld",&m,&d,&w);
		if(m<=1||d<=1||w<=1)
		{
			cout<<0;
			continue;
		}
		LL Gcd=gcd(d-1,w);
		w/=Gcd;
		LL min=d<m?d:m;
//		cout<<w<<" "<<min<<endl;
		for(LL i=1;i*w<min;++i)
		{
			ans+=min-i*w;
//			cout<<ans<<endl;
		}
		cout<<ans<<endl;
	}
}


#include<iostream>
#include<bits/stdc++.h>
#include<stdio.h>
#include<stdlib.h>
#include<vector>
#include<map>
#include<queue>
using namespace std;
#define N 201000
typedef long long LL;
LL m,d,w;
LL gcd(LL a,LL b){
 if(a%b==0)
  return b;
 return gcd(b,a%b);
}
int main()
{
	int T;
	cin>>T;
	while(T--)
	{
		LL ans=0;
		scanf("%lld %lld %lld",&m,&d,&w);
		if(m<=1||d<=1)
		{
			cout<<0<<endl;
			continue;
		}
		LL Gcd=gcd(d-1,w);
		w/=Gcd;
		LL min=d<m?d:m;
//		cout<<w<<" "<<min<<endl;
		LL chu=min/w;
		ans=chu*min-(w+chu*w)*chu/2;
		cout<<ans<<endl;
	}
}


#include<iostream>
#include<bits/stdc++.h>
#include<stdio.h>
#include<stdlib.h>
#include<vector>
#include<map>
#include<queue>
using namespace std;
#define N 201000
typedef long long LL;
LL m,d,w;
LL gcd(LL a,LL b){
 if(a%b==0)
  return b;
 return gcd(b,a%b);
}
int main()
{
	int T;
	cin>>T;
	while(T--)
	{
		LL ans=0;
		scanf("%lld %lld %lld",&m,&d,&w);
		if(m<=1||d<=1)
		{
			cout<<0<<endl;
			continue;
		}
		LL Gcd=gcd(d-1,w);
		w/=Gcd;
		LL min=d<m?d:m;
//		cout<<w<<" "<<min<<endl;
		for(LL i=1;i*w<min;++i)
		{
			ans+=min-i*w;
//			cout<<ans<<endl;
		}
		cout<<ans<<endl;
	}
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=100010;
ll n,m,T,a[N],s[N],l,r,k,x,y,z,d,w;
ll gcd(ll x,ll y)
{
	if (!y) return x; else return gcd(y,x%y);
}
int main()
{
	scanf("%lld",&T);
	while (T--)
	{
		scanf("%lld%lld%lld",&m,&d,&w);
		k=gcd(d-1,w);
		w/=k;
		x=min(m,d)-w; y=x%w; z=x/w;
		printf("%lld\n",(x+y)*(z+1)/2);
	}
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=100010;
ll n,m,T,a[N],s[N],l,r,k,x,y,z,d,w;
ll gcd(ll x,ll y)
{
	if (!y) return x; else return gcd(y,x%y);
}
int main()
{
	scanf("%lld",&T);
	while (T--)
	{
		scanf("%lld%lld%lld",&m,&d,&w);
		k=gcd(d-1,w);
		w/=k;
		x=min(m,d)-w; y=x%w; z=x/w;
		if (x<0) puts("0"); else printf("%lld\n",(x+y)*(z+1)/2);
	}
}


#include<bits/stdc++.h>
using namespace std;

typedef long long int ll;
typedef long double ld;

//****************************************************
#define lp(var,start,end) for (ll var = start; var <end ; ++var)
#define rlp(var,start,end) for(ll var = start; var>=end ; var--)
#define pb push_back
#define mp make_pair
#define pf push_front
#define ff first
#define ss second
#define vll vector<ll>
#define pll pair<ll,ll>
#define vpll vector<pll>
#define all(X) X.begin(),X.end()
#define endl "\n" //comment it for interactive questions
#define trace1(a) cerr << #a << ": " << a << endl;
#define trace2(a,b) cerr << #a << ": " << a << " " << #b << ": " << b << endl;
#define trace3(a,b,c) cerr << #a << ": " << a << " " << #b << ": " << b << " " << #c << ": " << c << endl;
#define trace4(a,b,c,d) cerr << #a << ": " << a << " " << #b << ": " << b << " " << #c << ": " << c << #d << ": " << d << endl;
#define OC
#define FAST_IO ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
        //*******************************************************
//Some Functions

const ll MOD = (ll)1e9+7; //change it for other mods

ll powmod(ll a,ll b)
{
   ll res = 1;
   while(b > 0) {
      if(b & 1) res = (res * a) % MOD;
      a = (a*a)%MOD;
      b = b >> 1;
  }
  return res % MOD;
}

void solve(ll testnum)
{
    ll m,d,w;
    cin >> m >> d >> w;
    ll mt = min(m, d);
    ll req = w/__gcd(d-1, w);
    ll ans = 0;
    for(ll i = 1;i*req < mt; i++) {
    	ans += (mt - i*req);
    }
    cout << ans << "\n";
}

int main()
{
    FAST_IO;
    #ifdef STIO
      freopen("input.txt" , "r", stdin);
      freopen("output.txt" , "w", stdout);
    #endif
    ll t = 1;
    cin >> t;
    for (ll i = 1; i <= t; i++) {
        solve(i);
    }
}


#include<bits/stdc++.h>
using namespace std;

typedef long long int ll;
typedef long double ld;

//****************************************************
#define lp(var,start,end) for (ll var = start; var <end ; ++var)
#define rlp(var,start,end) for(ll var = start; var>=end ; var--)
#define pb push_back
#define mp make_pair
#define pf push_front
#define ff first
#define ss second
#define vll vector<ll>
#define pll pair<ll,ll>
#define vpll vector<pll>
#define all(X) X.begin(),X.end()
#define endl "\n" //comment it for interactive questions
#define trace1(a) cerr << #a << ": " << a << endl;
#define trace2(a,b) cerr << #a << ": " << a << " " << #b << ": " << b << endl;
#define trace3(a,b,c) cerr << #a << ": " << a << " " << #b << ": " << b << " " << #c << ": " << c << endl;
#define trace4(a,b,c,d) cerr << #a << ": " << a << " " << #b << ": " << b << " " << #c << ": " << c << #d << ": " << d << endl;
#define OC
#define FAST_IO ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
        //*******************************************************
//Some Functions

const ll MOD = (ll)1e9+7; //change it for other mods

ll powmod(ll a,ll b)
{
   ll res = 1;
   while(b > 0) {
      if(b & 1) res = (res * a) % MOD;
      a = (a*a)%MOD;
      b = b >> 1;
  }
  return res % MOD;
}

void solve(ll testnum)
{
    ll m,d,w;
    cin >> m >> d >> w;
    ll mt = min(m, d);
    ll req = w/__gcd(d-1, w);
    ll ans = 0;
    ll mx = mt/req;
    ans += mx*mt;
    ll sub = (mx*(mx+1))/2;
    ans -= sub*req;
    // for(ll i = 1;i*req < mt; i++) {
    // 	ans += (mt - i*req);
    // }
    cout << ans << "\n";
}

int main()
{
    FAST_IO;
    #ifdef STIO
      freopen("input.txt" , "r", stdin);
      freopen("output.txt" , "w", stdout);
    #endif
    ll t = 1;
    cin >> t;
    for (ll i = 1; i <= t; i++) {
        solve(i);
    }
}


#include<bits/stdc++.h>
using namespace std;
int T;
long long m , d , w , ans;
int main()
{
	scanf("%d" , &T);
	while(T--)
	{
		scanf("%lld%lld%lld" , &m , &d , &w);
		if(d == w)
		{
			printf("0\n");
			continue;
		}
		if(d % w == 0)
		{
			ans = min(m , d) - w;
			printf("%lld\n" , ans);
			continue;
		}
		if(d % w == 1)
		{
			ans = min(m , d) * (min(m , d) + 1) / 2ll;
			printf("%lld\n" , ans);
			continue;
		}
		long long qwq = w / __gcd(d % w - 1 , w);
		long long k = min(m , d) / qwq;
		ans = k * min(m , d) - k * (k + 1) / 2ll * qwq;
		printf("%lld\n" , ans);
	}
	return 0;
}
/*
5
2 2 2
6 6 2
3 4 3
*/


#include<bits/stdc++.h>
using namespace std;
int T;
long long m , d , w , ans;
int main()
{
	scanf("%d" , &T);
	while(T--)
	{
		scanf("%lld%lld%lld" , &m , &d , &w);
		long long qwq = w / __gcd(d - 1 , w);
		long long k = min(m , d) / qwq;
		ans = k * min(m , d) - k * (k + 1) / 2ll * qwq;
		printf("%lld\n" , ans);
	}
	return 0;
}
/*
5
2 2 2
6 6 2
3 4 3
*/


#include<bits/stdc++.h>
using namespace std;
int T;
long long m , d , w , ans;
int main()
{
	scanf("%d" , &T);
	while(T--)
	{
		scanf("%lld%lld%lld" , &m , &d , &w);
		if(d == w)
		{
			printf("0\n");
			continue;
		}
		if(d % w == 0)
		{
			ans = min(m , d) - w;
			printf("%lld\n" , ans);
			continue;
		}
		long long qwq = w / __gcd(d % w - 1 , w);
		long long k = min(m , d) / qwq;
		ans = k * min(m , d) - k * (k + 1) / 2ll * qwq;
		printf("%lld\n" , ans);
	}
	return 0;
}
/*
5
2 2 2
6 6 2
3 3 2
*/


#include<bits/stdc++.h>
using namespace std;
int T;
long long m , d , w , ans;
int main()
{
	scanf("%d" , &T);
	while(T--)
	{
		scanf("%lld%lld%lld" , &m , &d , &w);
		long long qwq = w / __gcd(d % w - 1 , w);
		long long k = min(m , d) / qwq;
		ans = k * min(m , d) - k * (k + 1) / 2 * qwq;
		printf("%lld\n" , ans);
	}
	return 0;
}


#include<iostream>
#include<cstdio>
using namespace std;
int gcd(int x,int y)
{
    if(y==0)  return x;
    return gcd(y,x%y);
}
int main()
{
    int t;
    int m,w,d;
    cin>>t;
    while(t--)
    {
        cin>>m>>d>>w;
        if(d==1)  cout<<0<<endl;
        else
        {
            int g=gcd(w,d-1);
            int limit=min(m,d);
            int up=w/g;
            int a=limit/up;
            long long ans=0;
            ans=limit*1LL*a-(a+1)*1LL*a/2*up;
            cout<<ans<<endl;
        }

    }
    return 0;
}


#include<iostream>
#include<cstdio>
using namespace std;
int gcd(int x,int y)
{
    if(y==0)  return x;
    return gcd(y,x%y);
}
int main()
{
    int t;
    int m,w,d;
    cin>>t;
    while(t--)
    {
        cin>>m>>d>>w;
        if(d==1)  cout<<m-1<<endl;
        else
        {
            int g=gcd(w,d-1);
            int limit=min(m,d);
            int up=w/g;
            int a=limit/up;
            long long ans=0;
            ans=limit*1LL*a+a-(a+1)*1LL*a/2*up;
            cout<<ans<<endl;
        }

    }
    return 0;
}


#include<iostream>
#include<cstdio>
using namespace std;
int gcd(int x,int y)
{
    if(y==0)  return x;
    return gcd(y,x%y);
}
int main()
{
    int t;
    int m,w,d;
    cin>>t;
    while(t--)
    {
        cin>>m>>d>>w;
        if(d==1)  cout<<m-1<<endl;
        else
        {
            int g=gcd(w,d-1);
            int limit=min(m,d);
            int up=w/g;
            int a=limit/up;
            long long ans=0;
            ans=limit*1LL*a-(a+1)*1LL*a/2*up;
            cout<<ans<<endl;
        }

    }
    return 0;
}


#include<bits/stdc++.h>
#define fi first
#define se second
#define lc (x<<1)
#define rc (x<<1|1)
#define gc getchar()//(p1==p2&&(p2=(p1=buf)+fread(buf,1,size,stdin),p1==p2)?EOF:*p1++)
#define mk make_pair
#define pi pair<int,int>
#define fi first
#define se second
#define pb push_back
#define IT iterator
#define SZ(a) ((int)a.size())
#define all(a) a.begin(),a.end()
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
const int N=2e5+10,size=1<<20,mod=998244353,inf=2e9;

template<class o> void qr(o &x) {
	char c=gc; x=0; int f=1;
	while(!isdigit(c)){if(c=='-')f=-1; c=gc;}
	while(isdigit(c)) x=x*10+c-'0',c=gc;
	x*=f;
}
template<class o> void qw(o x) {
	if(x/10) qw(x/10);
	putchar(x%10+'0');
}
template<class o> void pr1(o x) {
	if(x<0)x=-x,putchar('-');
	qw(x); putchar(' ');
}
template<class o> void pr2(o x) {
	if(x<0)x=-x,putchar('-');
	qw(x); puts("");
}

//math
ll mult(ll a,ll b,ll p) {
	a=(a%p+p)%p; b=(b%p+p)%p;
	ll c=(ld)a*b/p;
	return a*b-c*p;
}
ll gcd(ll a,ll b) {return !a?b:gcd(b%a,a);}
ll lcm(ll a,ll b) {return a/gcd(a,b)*b;}
ll power(ll a,ll b=mod-2) {
	ll c=1;
	while(b) {
		if(b&1) c=c*a%mod;
		b /= 2; a=a*a%mod;
	}
	return c;
}
ll Power(ll a,ll b=mod-2) {
	ll c=1;
	while(b) {
		if(b&1) c=mult(c,a,mod);
		b /= 2; a=mult(a,a,mod);
	}
	return c;
}

ll f(ll x) {return (x+1)*x/2;}

int main() {
	int _;qr(_); while(_--) {
		ll m,d,w,ans;
		qr(m); qr(d); qr(w);
		m=min(m,d); w/=gcd(d-1,w);
		ans=0;
		ll t=(m-1)/w-1;
		if(t>0) ans += f(t)*w;
		if(m%w) ans += (++t)*(m%w);
		else ans+=(++t)*w;
		pr2(ans);
	}
	return 0;
}
/*
设(m=min(m,d))>=x>y>0.
对于确定的x,y的个数为floor((x-1)/y).
(x-1)*d+y=(y-1)*d+x(mod w)
(x-y)(d-1)=0(mod w)
x-y=0(mod w=(w/gcd(w,d-1)))

floor((x-1)/w),T=w.
*/


#include<bits/stdc++.h>
#define fi first
#define se second
#define lc (x<<1)
#define rc (x<<1|1)
#define gc getchar()//(p1==p2&&(p2=(p1=buf)+fread(buf,1,size,stdin),p1==p2)?EOF:*p1++)
#define mk make_pair
#define pi pair<int,int>
#define fi first
#define se second
#define pb push_back
#define IT iterator
#define SZ(a) ((int)a.size())
#define all(a) a.begin(),a.end()
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
const int N=2e5+10,size=1<<20,mod=998244353,inf=2e9;

template<class o> void qr(o &x) {
	char c=gc; x=0; int f=1;
	while(!isdigit(c)){if(c=='-')f=-1; c=gc;}
	while(isdigit(c)) x=x*10+c-'0',c=gc;
	x*=f;
}
template<class o> void qw(o x) {
	if(x/10) qw(x/10);
	putchar(x%10+'0');
}
template<class o> void pr1(o x) {
	if(x<0)x=-x,putchar('-');
	qw(x); putchar(' ');
}
template<class o> void pr2(o x) {
	if(x<0)x=-x,putchar('-');
	qw(x); puts("");
}

//math
ll mult(ll a,ll b,ll p) {
	a=(a%p+p)%p; b=(b%p+p)%p;
	ll c=(ld)a*b/p;
	return a*b-c*p;
}
ll gcd(ll a,ll b) {return !a?b:gcd(b%a,a);}
ll lcm(ll a,ll b) {return a/gcd(a,b)*b;}
ll power(ll a,ll b=mod-2) {
	ll c=1;
	while(b) {
		if(b&1) c=c*a%mod;
		b /= 2; a=a*a%mod;
	}
	return c;
}
ll Power(ll a,ll b=mod-2) {
	ll c=1;
	while(b) {
		if(b&1) c=mult(c,a,mod);
		b /= 2; a=mult(a,a,mod);
	}
	return c;
}

ll f(ll x) {return (x+1)*x/2;}

int main() {
	int _;qr(_); while(_--) {
		ll m,d,w,ans;
		qr(m); qr(d); qr(w);
		m=min(m,d); w/=gcd(d-1,w);
		ans=0;
		ll t=(m-1)/w-1;
		if(t>0) ans += f(t)*w;
		if(m%w) ans += (++t)*(m%w);
		else ans+=(++t)*w;
		pr2(ans);
	}
	return 0;
}


    #include<bits/stdc++.h>
using namespace std;
#define ll __int64_t
#define MOD 1000000007ll
#define pb push_back
#define vl vector<ll>
#define pll pair<ll,ll>
#define mp make_pair
using namespace std;
#define pi (long double)3.14159265358979323846
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
 struct sch{
  ll s,e,t;
};
bool sortinrev(const struct sch &a, const struct sch &b)
{
    return (a.s < b.s);
}
ll gcd(ll a, ll b)
{
    if (b == 0)
        return a;
    return gcd(b, a % b);

}
ll lcm(ll a,ll b)
{
  return a*b/gcd(a,b);
}
ll power(ll x, ll y, ll p)
{
    ll res = 1;
    x = x % p;
    while (y > 0)
    {
        if (y & 1)
            res = (res*x) % p;
        y = y>>1; // y = y/2
        x = (x*x) % p;
    }
    return res;
}
int main()
{
    fast
    ll t;
    cin>>t;
    while(t--)
    {
        ll m,d,w;
        cin>>m>>d>>w;
        if(d==1)
        {
            ll ans = m*d;
            ans = ans*(ans-1)/2;
            cout<<ans<<endl;
            continue;
        }
        else
        {
            d--;
            ll div = gcd(d,w);
            w /= div;
            ll m1 = min(m,d+1);
            ll k = (m1-1)/w;
         //   cout<<k<<endl;
            ll ans = 0;
            ll t1 = k*m1;
            ll t2 = ((k)*(k+1)*w)/2;
            ans = t1-t2;
            cout<<ans<<endl;
        }
    }
}


    #include<bits/stdc++.h>
using namespace std;
#define ll __int64_t
#define MOD 1000000007ll
#define pb push_back
#define vl vector<ll>
#define pll pair<ll,ll>
#define mp make_pair
using namespace std;
#define pi (long double)3.14159265358979323846
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
 struct sch{
  ll s,e,t;
};
bool sortinrev(const struct sch &a, const struct sch &b)
{
    return (a.s < b.s);
}
ll gcd(ll a, ll b)
{
    if (b == 0)
        return a;
    return gcd(b, a % b);

}
ll lcm(ll a,ll b)
{
  return a*b/gcd(a,b);
}
ll power(ll x, ll y, ll p)
{
    ll res = 1;
    x = x % p;
    while (y > 0)
    {
        if (y & 1)
            res = (res*x) % p;
        y = y>>1; // y = y/2
        x = (x*x) % p;
    }
    return res;
}
int main()
{
    fast
    ll t;
    cin>>t;
    while(t--)
    {
        ll m,d,w;
        cin>>m>>d>>w;
        if(d==1)
        {
            cout<<0<<endl;
            continue;
        }
        else
        {
            d--;
            ll div = gcd(d,w);
            w /= div;
            ll m1 = min(m,d+1);
            ll k = (m1-1)/w;
         //   cout<<k<<endl;
            ll ans = 0;
            ll t1 = k*m1;
            ll t2 = ((k)*(k+1)*w)/2;
            ans = t1-t2;
            cout<<ans<<endl;
        }
    }
}


    #include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define MOD 1000000007ll
#define pb push_back
#define vl vector<ll>
#define pll pair<ll,ll>
#define mp make_pair
using namespace std;
#define pi (long double)3.14159265358979323846
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
 struct sch{
  ll s,e,t;
};
bool sortinrev(const struct sch &a, const struct sch &b)
{
    return (a.s < b.s);
}
ll gcd(ll a, ll b)
{
    if (b == 0)
        return a;
    return gcd(b, a % b);

}
ll lcm(ll a,ll b)
{
  return a*b/gcd(a,b);
}
ll power(ll x, ll y, ll p)
{
    ll res = 1;
    x = x % p;
    while (y > 0)
    {
        if (y & 1)
            res = (res*x) % p;
        y = y>>1; // y = y/2
        x = (x*x) % p;
    }
    return res;
}
int main()
{
    fast
    ll t;
    cin>>t;
    while(t--)
    {
        ll m,d,w;
        cin>>m>>d>>w;
        if(d==1)
        {
            cout<<0<<endl;
            continue;
        }
        else
        {
            d--;
            ll div = gcd(d,w);
            w /= div;
            ll m1 = min(m,d+1);
            ll k = (m1-1)/w;
         //   cout<<k<<endl;
            ll ans = 0;
            ll t1 = k*m1;
            ll t2 = ((k)*(k+1)*w)/2;
            ans = t1-t2;
            cout<<ans<<endl;
        }
    }
}


    #include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define MOD 1000000007ll
#define pb push_back
#define vl vector<ll>
#define pll pair<ll,ll>
#define mp make_pair
using namespace std;
#define pi (long double)3.14159265358979323846
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
 struct sch{
  ll s,e,t;
};
bool sortinrev(const struct sch &a, const struct sch &b)
{
    return (a.s < b.s);
}
ll gcd(ll a, ll b)
{
    if (b == 0)
        return a;
    return gcd(b, a % b);

}
ll lcm(ll a,ll b)
{
  return a*b/gcd(a,b);
}
ll power(ll x, ll y, ll p)
{
    ll res = 1;
    x = x % p;
    while (y > 0)
    {
        if (y & 1)
            res = (res*x) % p;
        y = y>>1; // y = y/2
        x = (x*x) % p;
    }
    return res;
}
int main()
{
    fast
    ll t;
    cin>>t;
    while(t--)
    {
        ll m,d,w;
        cin>>m>>d>>w;
        if(d==1)
        {
            ll ans = m*d;
            ans = ans*(ans-1)/2;
            cout<<ans<<endl;
            continue;
        }
        else
        {
            d--;
            ll div = gcd(d,w);
            w /= div;
            ll m1 = min(m,d+1);
            ll k = (m1-1)/w;
         //   cout<<k<<endl;
            ll ans = 0;
            ll t1 = k*m1;
            ll t2 = ((k)*(k+1)*w)/2;
            ans = t1-t2;
            cout<<ans<<endl;
        }
    }
}


#include <bits/stdc++.h>

using namespace std;
typedef long long int ll;

ll gcd(ll a, ll b)
{
    if (a == 0)
        return b;
    return gcd(b % a, a);
}

int main()
{
    ll t,m,d,w,count;
    ll divi,rem,need;
    cin>>t;
    while(t--)
    {
        cin>>m>>d>>w;
        need = w/gcd(w,d-1);

        if(d==1||need>(d-1))
        cout<<0<<endl;
        else
        {
            d=min(d,m);
            divi = (d-1)/need;
            rem = (d-1)%need;
            count = ((divi*(divi-1))/2)*need + (rem+1)*divi;
            cout<<count<<endl;
        }
    }
}


#include <bits/stdc++.h>

using namespace std;
typedef long long int ll;

ll gcd(ll a, ll b)
{
    // Everything divides 0
    if (a == 0)
       return b;
    if (b == 0)
       return a;

    // base case
    if (a == b)
        return a;

    // a is greater
    if (a > b)
        return gcd(a-b, b);
    return gcd(a, b-a);
}

int main()
{
    ll t,m,d,w,count;
    ll divi,rem,need;
    cin>>t;
    while(t--)
    {
        cin>>m>>d>>w;
        need = w/gcd(w,d-1);

        if(d==1||need>(d-1))
        cout<<0<<endl;
        else
        {
            d=min(d,m);
            divi = (d-1)/need;
            rem = (d-1)%need;
            count = ((divi*(divi-1))/2)*need + (rem+1)*divi;
            cout<<count<<endl;
        }
    }
}


#include <bits/stdc++.h>

using namespace std;
typedef long long int ll;

int main()
{
    ll t,m,d,w,count;
    ll divi,rem,need;
    cin>>t;
    while(t--)
    {
        cin>>m>>d>>w;
        need = w/__gcd(w,d-1);

        if(d==1||need>(d-1))
        cout<<0<<endl;
        else
        {
            d=min(d,m);
            divi = (d-1)/need;
            rem = (d-1)%need;
            count = ((divi*(divi-1))/2)*need + (rem+1)*divi;
            cout<<count<<endl;
        }
    }
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<long long, long long> pll;
typedef complex<double> cd;
typedef vector<int> veci;
#define tern(flag,true_val,false_val) (flag ? true_val : false_val)
#define all(container) container.begin(),container.end()
#define pb push_back
#define forn(i,n) for(int i=0;i<n;i++)
#define forup(i,f,l) for(int i=f;i<l;i++)
#define fordown(i,l,f) for(int i=l;i>=f;i--)
#define fi first
#define se second
#define debug1 cout<<"Start"<<endl;
#define debug2 cout<<"End"<<endl;
#define print(vec) for(auto x:vec)cout<<x<<" ";cout<<endl;
const double PI=acos(-1);

ll gcd(ll a, ll b){
	if(a%b==0){
		return b;
	}
	return gcd(b,a%b);
}

ll lcm(ll a, ll b){
	return (a*b)/gcd(a,b);
}

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int T;
	cin>>T;
	for(int q=0;q<T;q++){
		ll m,d,w;
		cin>>m>>d>>w;
		if(d==1){
			cout<<0<<endl;
			continue;
		}
		ll L = lcm(w,(d-1));
		ll k = L/(d-1);
		ll temp = min(d,m);
		ll rows = temp/k;
		ll left = temp%k;
		ll ans = k*(rows*(rows-1))/2ll;
		ans += left*rows;
		cout<<ans<<endl;
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<long long, long long> pll;
typedef complex<double> cd;
typedef vector<int> veci;
#define tern(flag,true_val,false_val) (flag ? true_val : false_val)
#define all(container) container.begin(),container.end()
#define pb push_back
#define forn(i,n) for(int i=0;i<n;i++)
#define forup(i,f,l) for(int i=f;i<l;i++)
#define fordown(i,l,f) for(int i=l;i>=f;i--)
#define fi first
#define se second
#define debug1 cout<<"Start"<<endl;
#define debug2 cout<<"End"<<endl;
#define print(vec) for(auto x:vec)cout<<x<<" ";cout<<endl;
const double PI=acos(-1);

ll gcd(ll a, ll b){
	if(a%b==0){
		return b;
	}
	return gcd(b,a%b);
}

ll lcm(ll a, ll b){
	return (a*b)/gcd(a,b);
}

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int T;
	cin>>T;
	for(int q=0;q<T;q++){
		ll m,d,w;
		cin>>m>>d>>w;
		if(d==1){
			cout<<0<<endl;
			continue;
		}
		ll L = lcm(w,(d-1));
		debug1
		ll k = L/(d-1);
		ll temp = min(d,m);
		ll rows = temp/k;
		ll left = temp%k;
		ll ans = k*(rows*(rows-1))/2ll;
		ans += left*rows;
		cout<<ans<<endl;
	}
	return 0;
}


%:pragma GCC optimize "-O3"
%:pragma GCC optimize("Ofast")
%:pragma GCC optimize("inline")
%:pragma GCC optimize("fast-math")
%:pragma GCC optimize("unroll-loops")
%:pragma GCC optimize("inline-functions")
%:pragma GCC optimize("no-stack-protector")
%:pragma GCC optimize("inline-small-functions")
%:pragma GCC optimize("inline-functions-called-once")
%:pragma GCC target("sse3","sse2","sse")
#include<bits/stdc++.h>
#define ll long long
#define heap priority_queue
using namespace std;
ll ans,t,m,d,w,g,h,maxd;
int main()
{
	cin>>t;
	while(t--)
	{
		ans=0;
		cin>>m>>d>>w;
		m=min(m,d);
		if(d==1)
		{
			cout<<"0\n";
			continue;
		}
		g=(d-1)%w;
		h=w/__gcd(w,d-1);
		if(1+h>m)
		{
			cout<<"0\n";
			continue;
		}
		ans=(m-1)/h*m-(1+(m-1)/h)*((m-1)/h)/2*h;
		cout<<ans<<"\n";
	}
	return 0;
}


%:pragma GCC optimize "-O3"
%:pragma GCC optimize("Ofast")
%:pragma GCC optimize("inline")
%:pragma GCC optimize("fast-math")
%:pragma GCC optimize("unroll-loops")
%:pragma GCC optimize("inline-functions")
%:pragma GCC optimize("no-stack-protector")
%:pragma GCC optimize("inline-small-functions")
%:pragma GCC optimize("inline-functions-called-once")
%:pragma GCC target("sse3","sse2","sse")
#include<bits/stdc++.h>
#define ll long long
#define heap priority_queue
using namespace std;
ll ans,t,m,d,w,g,h,maxd;
int main()
{
	cin>>t;
	while(t--)
	{
		ans=0;
		cin>>m>>d>>w;
		if(d==1)
		{
			cout<<"0\n";
			continue;
		}
		g=(d-1)%w;
		h=w/g;
		if(1+h>m)
		{
			cout<<"0\n";
			continue;
		}
		ans=(m-1)/h*m-(1+(m-1)/h)*((m-1)/h)/2*h;
		cout<<ans<<"\n";
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int t, d, m, w;

int main()
{
    cin>>t;
    for(;t;t--)
    {
        cin>>m>>d>>w;
        int w2=w/__gcd(d-1, w);
        int mn=min(m, d);
        int cnt=mn/w2;
        cout<<((2*(mn-w2)-w2*(cnt-1))*cnt/2)<<'\n';
    }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int t, d, m, w;

int main()
{
    cin>>t;
    for(;t;t--)
    {
        cin>>m>>d>>w;
        int w2=w/__gcd(d-1, w);
        int mn=min(m, d);
        int cnt=mn/w2;
        cout<<1LL*(1LL*(2LL*(mn-w2)-1LL*w2*(cnt-1))*cnt/2)<<'\n';
    }

    return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
typedef long long ll;
inline int read(){
	int x=0,f=1;
	char c=getchar();
	while(c<'0'||c>'9'){
		if(c=='-')f=-1;
		c=getchar();
	}
	while(c>='0'&&c<='9'){
		x=(x<<1)+(x<<3)+c-'0';
		c=getchar();
	}
	return x*f;
}
int T;
ll m,d,w;
ll _gcd(ll a,ll b){
	if(b==0)return a;
	return _gcd(b,a%b);
}
ll calc(ll x,ll i){
	ll tot=0;
	ll p=x/i-1;
	tot+=p*(x/i)/2*i;
	tot+=1LL*(x/i)*(x%i+1);
	return tot;
}
int main(){
	T=read();
	while(T--){
		m=read(),d=read(),w=read();
		if(d==1){
			printf("0\n");
			continue;
		}
		w/=_gcd(d-1,w);
		printf("%I64d\n",calc(min(m,d)-1,w));
	}
	return 0;
}


#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
#include<streambuf>
#include<string>
#include<queue>
#include<map>
#include<math.h>
#include<vector>
#include<set>
#include<time.h>
typedef long long ll;
using namespace std;
const int maxx=0;
const double eps=1e-8;
const double pi=atan(1.0)*4;
ll gcd(ll n,ll m)
{
    return m==0?n:gcd(m,n%m);
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        ll n,m,k,i,ans=0;
        scanf("%lld%lld%lld",&n,&m,&k);
        m=m-1;
        ll w=gcd(m,k);
        w=k/w;
        m=m+1;
        ll x=min(n,m);
        ll z=x/w;
        //printf("%lld %lld %lld\n",w,x,z);
        for(i=1; i<=z; i++)
        {
            ans=ans+x-i*w;
        }
        printf("%lld\n",ans);
    }
    return 0;
}


#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
#include<streambuf>
#include<string>
#include<queue>
#include<map>
#include<math.h>
#include<vector>
#include<set>
#include<time.h>
typedef long long ll;
using namespace std;
const int maxx=0;
const double eps=1e-8;
const double pi=atan(1.0)*4;
ll gcd(ll n,ll m)
{
    return m==0?n:gcd(m,n%m);
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        ll n,m,k,i,ans=0;
        scanf("%lld%lld%lld",&n,&m,&k);
        m=m-1;
        ll w=gcd(m,k);
        w=k/w;
        m=m+1;
        ll x=min(n,m);
        ll z=x/w;
        //printf("%lld %lld %lld\n",w,x,z);
        printf("%lld\n",z*x-((z*(z+1)/2)*w));
    }
    return 0;
}


#include <bits/stdc++.h>
#define rei register int
#define int long long
using namespace std;
int T;
int gcd(int x,int y){
	return y ? gcd(y,x % y) : x;
}
signed main(){
	cin >> T;
	while(T--){
		int m,d,w;
		scanf("%lld%lld%lld",&m,&d,&w);
		int lim = min(m,d);
		w = w / gcd(d - 1,w);
		int be = lim - w,ed = lim % w,N = lim / w;
		if(be <= 0) {puts("0"); continue;}
		printf("%lld\n",(be + ed)*N / 2);
	} return 0;
}


#include <bits/stdc++.h>
#define rei register int
using namespace std;
int T;
int gcd(int x,int y){
	return y ? gcd(y,x % y) : x;
}
int main(){
	cin >> T;
	while(T--){
		int m,d,w;
		scanf("%d%d%d",&m,&d,&w);
		int lim = min(m,d);
		w = w / gcd(d - 1,w);
		int be = lim - w,ed = lim % w,N = lim / w;
		if(be <= 0) {puts("0"); continue;}
		printf("%d\n",(be + ed)*N / 2);
	} return 0;
}


#include <bits/stdc++.h>
#define rei register int
#define int long long
using namespace std;
int T;
int gcd(int x,int y){
	return y ? gcd(y,x % y) : x;
}
signed main(){
	cin >> T;
	while(T--){
		int m,d,w;
		scanf("%d%d%d",&m,&d,&w);
		int lim = min(m,d);
		w = w / gcd(d - 1,w);
		int be = lim - w,ed = lim % w,N = lim / w;
		if(be <= 0) {puts("0"); continue;}
		printf("%d\n",(be + ed)*N / 2);
	} return 0;
}


#include <bits/stdc++.h>
#define INF 1e9
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef tuple<int,int,int> iii;
typedef vector<ll> vll;
typedef map<int, int> mii;
typedef pair<char,char> cc;
typedef vector<pair<char,char>> vcc;

int gcd(int a, int b){
	while(b){
		int t = a%b;
		a = b;
		b = t;
	}
	return a;
}

int main() {
    ios::sync_with_stdio(false); cin.tie(NULL);

    int tc; cin>>tc;
    while(tc--){
    	int a,b,c; cin>>a>>b>>c;
    	c /= gcd(b-1,c);
    	int m = min(a,b);
		int n = m/c;
		ll ans = (m%c) * n + c*n*(n-1)/2;
		cout<<ans<<endl;
	}

    return 0;
}


#include <bits/stdc++.h>
#define INF 1e9
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef tuple<int,int,int> iii;
typedef vector<ll> vll;
typedef map<int, int> mii;
typedef pair<char,char> cc;
typedef vector<pair<char,char>> vcc;

int gcd(int a, int b){
	while(b){
		int t = a%b;
		a = b;
		b = t;
	}
	return a;
}

int main() {
    ios::sync_with_stdio(false); cin.tie(NULL);

    int tc; cin>>tc;
    while(tc--){
    	int a,b,c; cin>>a>>b>>c;
    	c /= gcd(b-1,c);
    	int m = min(a,b);
		int n = m/c;
		ll ans = (m%c) * n + c*(n*(n-1)/2);
		cout<<ans<<endl;
	}

    return 0;
}


#include <bits/stdc++.h>
#define INF 1e9
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef tuple<int,int,int> iii;
typedef vector<ll> vll;
typedef map<int, int> mii;
typedef pair<char,char> cc;
typedef vector<pair<char,char>> vcc;

int gcd(int a, int b){
	while(b){
		int t = a%b;
		a = b;
		b = t;
	}
	return a;
}

int main() {
    ios::sync_with_stdio(false); cin.tie(NULL);

    int tc; cin>>tc;
    while(tc--){
    	int a,b,c; cin>>a>>b>>c;
    	c /= gcd(b-1,c);
    	ll m = min(a,b);
		ll n = m/c;
		ll ans = (m%c) * n + c*(n*(n-1)/2);
		cout<<ans<<endl;
	}

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int t,m,d,w;
int main()
{
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d%d%d",&m,&d,&w);
		w=w/gcd(w,d-1);
		int n=min(m,d);
		int num=n/w;
		int ans=(2*n-w*num-w)*num/2;
		cout<<ans<<endl;
	}
	return 0;
}


#include<bits/stdc++.h>
#define int long long
using namespace std;
int t,m,d,w;
signed main()
{
	scanf("%lld",&t);
	while(t--)
	{
		scanf("%lld%lld%lld",&m,&d,&w);
		w=w/__gcd(w,d-1);
		int n=min(m,d);
		int num=n/w;
		int ans=(2*n-w*num-w)*num/2;
		cout<<ans<<endl;
	}
	return 0;
}


#include <iostream>
#include <algorithm>
#include <string>
#include <cmath>
#include <iomanip>
#include <vector>
#include <queue>
#include <map>
#include <set>

#define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define pb push_back
#define in insert
#define er erase
#define fi first
#define se second
#define mp make_pair
#define all(x) x.begin(),x.end()
#define le(x) (int) x.length()
#define rsz resize

using namespace std;
typedef vector <int> vi;
typedef vector <vector <int> > vii;
typedef pair <int, int> pii;
typedef queue <int> qi;
typedef double db;
typedef long long ll;
typedef long double ld;

const long double PI=3.141592653589793238462643383279502884;
const long long PRIME1=1e9+787;

ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
ll minll(ll a,ll b) {return a<=b?a:b;}
ll maxll(ll a,ll b) {return a>=b?a:b;}

int main () {
    fast;
    int t;
    cin>>t;
    while (t--) {
        ll m, d, w;
        cin>>m>>d>>w;
        w/=gcd(d-1,w);
        ll ans=0;
        ll d1=minll(m,d);
        for (ll i=1; i<d1; i++) {
            ans+=(d1-i)/w;
        }
        cout<<ans<<"\n";
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#include <string>
#include <cmath>
#include <iomanip>
#include <vector>
#include <queue>
#include <map>
#include <set>

#define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define pb push_back
#define in insert
#define er erase
#define fi first
#define se second
#define mp make_pair
#define all(x) x.begin(),x.end()
#define le(x) (int) x.length()
#define rsz resize

using namespace std;
typedef vector <int> vi;
typedef vector <vector <int> > vii;
typedef pair <int, int> pii;
typedef queue <int> qi;
typedef double db;
typedef long long ll;
typedef long double ld;

const long double PI=3.141592653589793238462643383279502884;
const long long PRIME1=1e9+787;

ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
ll minll(ll a,ll b) {return a<=b?a:b;}
ll maxll(ll a,ll b) {return a>=b?a:b;}

int main () {
    fast;
    int t;
    cin>>t;
    while (t--) {
        ll m, d, w;
        cin>>m>>d>>w;
        w/=gcd(d-1,w);
        ll ans=0;
        ll d1=minll(m,d)-1;
        ans=(d1/w)*(d1/w-1)/2*w+(d1%w+1)*(d1/w);
        cout<<ans<<"\n";
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int,int> ii;
typedef complex<ld> cp;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<ii> vii;
typedef vector<cp> vcp;
typedef vector<ld> vld;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef vector<vii> vvii;

#define fastIO ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define forw(i,l,r) for( int i = (l) ; i < (r) ; i++ )
#define forb(i,r,l) for( int i = (r) ; i >= (l) ; i-- )
#define log2i(x) 32 - __builtin_clz((x)) - 1
#define log2ll(x) 64 - __builtin_clzll((x)) - 1
#define Pi acos(-1.0)
#define sz(x) (int)x.size()
#define mt make_tuple
#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define pf push_front
#define pob pop_back
#define pof pop_front
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()

int t;
ull m,d,w,tmp;

int main() {
#ifndef ONLINE_JUDGE
    freopen("test.inp","r",stdin);
    freopen("test.out","w",stdout);
#endif
    fastIO;
    cin >> t;
    while(t--) {
        cin >> m >> d >> w;
        m=min(m,d);
        w/=__gcd(w,d-1);
        cout << ((m-1)/w)*m-(((m-1)/w)*(((m-1)/w)+1))/2*w << '\n';
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int,int> ii;
typedef complex<ld> cp;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<ii> vii;
typedef vector<cp> vcp;
typedef vector<ld> vld;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef vector<vii> vvii;

#define fastIO ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define forw(i,l,r) for( int i = (l) ; i < (r) ; i++ )
#define forb(i,r,l) for( int i = (r) ; i >= (l) ; i-- )
#define log2i(x) 32 - __builtin_clz((x)) - 1
#define log2ll(x) 64 - __builtin_clzll((x)) - 1
#define Pi acos(-1.0)
#define sz(x) (int)x.size()
#define mt make_tuple
#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define pf push_front
#define pob pop_back
#define pof pop_front
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()

int t;
ll m,d,w,tmp;

int main() {
#ifndef ONLINE_JUDGE
    freopen("test.inp","r",stdin);
    freopen("test.out","w",stdout);
#endif
    fastIO;
    cin >> t;
    while(t--) {
        cin >> m >> d >> w;
        m=min(m,d);
        tmp=__gcd(w,d-1);
        cout << ((m-1)/(w/tmp))*m-(((m-1)/(w/tmp))*(((m-1)/(w/tmp))+1))/2*w/tmp << '\n';
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
inline int R()
{
	char c;int sign=1,res=0;
	while((c=getchar())>'9'||c<'0') if(c=='-') sign=-1;res+=c-'0';
	while((c=getchar())>='0'&&c<='9') res=res*10+c-'0';
	return res*sign;
}
int t,a,b,c;
long long n,ans;
signed main()
{
	t=R();
	while(t--)
	{a=R();b=R();c=R();
	c/=__gcd(b-1,c);
	ans=0;n=min(a,b);
	n--;
	ans+=(n%c+1)*(n/c);
	n/=c;n--;
	ans+=c*(1+n)*n/2;
	//for(int i=1;i<=n;i++)
	//ans+=(i-1)/c;
  printf("%lld\n",ans);
}
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
inline int R()
{
	char c;int sign=1,res=0;
	while((c=getchar())>'9'||c<'0') if(c=='-') sign=-1;res+=c-'0';
	while((c=getchar())>='0'&&c<='9') res=res*10+c-'0';
	return res*sign;
}
int t,a,b,c;
long long n,ans;
signed main()
{
	t=R();
	while(t--)
	{a=R();b=R();c=R();
	c/=__gcd(b-1,c);
	ans=0;n=min(a,b);
	for(int i=1;i<=n;i++)
	ans+=(i-1)/c;
  printf("%lld\n",ans);
}
}


#include <bits/stdc++.h>
using namespace std;

int gcd(int x, int y){
		if(x < y) swap(x,y);
		if(!y) return x;
		return gcd(y, x%y);
}

int main(){
		ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
		int t;
		cin >> t;
		while(t--){
				int m,d,w;
				cin >> m >> d >> w;
				int T = w/gcd(w,d-1);
				int M = min(d,m);
				if(M <= T){
					cout << 0 << endl;
					continue;
				}
				int mst = (M-1)/T;
				int r = mst - 1;
				long long ans = T*(long long)((r*(r+1))/2);
				ans += (M-T*mst)*mst;
				cout << ans << endl;
		}
		return 0;
}


    #include <bits/stdc++.h>
    using namespace std;

    int gcd(int x, int y){
    		if(x < y) swap(x,y);
    		if(!y) return x;
    		return gcd(y, x%y);
    }

    int main(){
    		ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    		int t;
    		cin >> t;
    		while(t--){
    				int m,d,w;
    				cin >> m >> d >> w;
    				int T = w/gcd(w,d-1);
    				int M = min(d,m);
    				if(M <= T){
    					cout << 0 << endl;
    					continue;
    				}
    				int mst = (M-1)/T;
    				int r = mst - 1;
    				long long ans = (long long)T*(long long)(((long long)r*(long long)(r+1))/2);
    				ans += (M-T*mst)*mst;
    				cout << ans << endl;
    		}
    		return 0;
    }


#include<iostream>
#include<string.h>
#include<string>
#include<stdio.h>
#include<algorithm>
#include<vector>
#include<bitset>
#include<math.h>
#include<stack>
#include<queue>
#include<set>
#include<map>
using namespace std;
typedef long long ll;
typedef long double db;
typedef vector<int> vi;
typedef pair<int,int> pii;
const int N=100000+100;
const db pi=acos(-1.0);
#define lowbit(x) ((x)&(-x))
#define sqr(x) (x)*(x)
#define rep(i,a,b) for (register int i=a;i<=b;i++)
#define per(i,a,b) for (register int i=a;i>=b;i--)
#define go(u,i) for (register int i=head[u],v=sq[i].to;i;i=sq[i].nxt,v=sq[i].to)
#define fir first
#define sec second
#define mkp make_pair
#define pb push_back
#define maxd 998244353
#define eps 1e-8
#define int long long
inline int read()
{
    int x=0,f=1;char ch=getchar();
    while ((ch<'0') || (ch>'9')) {if (ch=='-') f=-1;ch=getchar();}
    while ((ch>='0') && (ch<='9')) {x=x*10+(ch-'0');ch=getchar();}
    return x*f;
}

inline ll readll()
{
    ll x=0;int f=1;char ch=getchar();
    while ((ch<'0') || (ch>'9')) {if (ch=='-') f=-1;ch=getchar();}
    while ((ch>='0') && (ch<='9')) {x=x*10+(ch-'0');ch=getchar();}
    return x*f;
}

signed main()
{
    int T=read();
    while (T--)
    {
        int m=read(),d=read(),w=read();
        int g=__gcd(d-1,w),n=min(m,d);
        if (!g)
        {
            printf("%lld\n",n*(n-1)/2);
            continue;
        }
        int p=w/g;
        //cout << p << " " << n << endl;
        int cnt=(n-1)/p,r=n-p,l=n-cnt*p;
        ll ans=(r+l)*cnt/2;
        printf("%lld\n",ans);
    }
    return 0;
}


#include<iostream>
#include<string.h>
#include<string>
#include<stdio.h>
#include<algorithm>
#include<vector>
#include<bitset>
#include<math.h>
#include<stack>
#include<queue>
#include<set>
#include<map>
using namespace std;
typedef long long ll;
typedef long double db;
typedef vector<int> vi;
typedef pair<int,int> pii;
const int N=100000+100;
const db pi=acos(-1.0);
#define lowbit(x) ((x)&(-x))
#define sqr(x) (x)*(x)
#define rep(i,a,b) for (register int i=a;i<=b;i++)
#define per(i,a,b) for (register int i=a;i>=b;i--)
#define go(u,i) for (register int i=head[u],v=sq[i].to;i;i=sq[i].nxt,v=sq[i].to)
#define fir first
#define sec second
#define mkp make_pair
#define pb push_back
#define maxd 998244353
#define eps 1e-8
#define int long long
inline int read()
{
    int x=0,f=1;char ch=getchar();
    while ((ch<'0') || (ch>'9')) {if (ch=='-') f=-1;ch=getchar();}
    while ((ch>='0') && (ch<='9')) {x=x*10+(ch-'0');ch=getchar();}
    return x*f;
}

inline ll readll()
{
    ll x=0;int f=1;char ch=getchar();
    while ((ch<'0') || (ch>'9')) {if (ch=='-') f=-1;ch=getchar();}
    while ((ch>='0') && (ch<='9')) {x=x*10+(ch-'0');ch=getchar();}
    return x*f;
}

signed main()
{
    int T=read();
    while (T--)
    {
        int m=read(),d=read(),w=read();
        int g=__gcd(d-1,w),n=min(m,d);
        if (!g)
        {
            printf("%lld\n",n*(n-1)/2);
            continue;
        }
        int p=w/g;
        //cout << p << " " << n << endl;
        int cnt=(n-1)/p,r=n-p,l=n-cnt*p;
        ll ans=(r+l)*cnt/2;
        printf("ans=%lld\n",ans);
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
signed main(){
    ios::sync_with_stdio(0);cin.tie(0);
    int t;cin>>t;
    while(t--){
        int m,d,w;cin>>m>>d>>w;
        if(d==1){
            cout<<0<<endl;
            continue;
        }
        int x=w/__gcd(d-1,w);
        int y=min(m,d)/x;
        int r=min(m,d)%x;
        // cout<<"x:"<<x<<endl;
        if(y==0){
            cout<<0<<endl;
            continue;
        }
        cout<<((y-1)*y/2*x+r*y)<<endl;
    }
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
signed main(){
    ios::sync_with_stdio(0);cin.tie(0);
    int t;cin>>t;
    while(t--){
        int m,d,w;cin>>m>>d>>w;
        if(d==1){
            cout<<m*(m-1)/2<<endl;
            continue;
        }
        int x=w/__gcd(d-1,w);
        int y=min(m,d)/x;
        int r=min(m,d)%x;
        // cout<<"x:"<<x<<endl;
        if(y==0){
            cout<<0<<endl;
            continue;
        }
        cout<<((y-1)*y/2*x+r*y)<<endl;
    }
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
signed main(){
    ios::sync_with_stdio(0);cin.tie(0);
    int t;cin>>t;
    while(t--){
        int m,d,w;cin>>m>>d>>w;
        if(d==1){
            cout<<((m-1)/w+1)*(((m-1)/w+1)-1)/2<<endl;
            continue;
        }
        int x=w/__gcd(d-1,w);
        int y=min(m,d)/x;
        int r=min(m,d)%x;
        // cout<<"x:"<<x<<endl;
        if(y==0){
            cout<<0<<endl;
            continue;
        }
        cout<<((y-1)*y/2*x+r*y)<<endl;
    }
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
signed main(){
    ios::sync_with_stdio(0);cin.tie(0);
    int t;cin>>t;
    while(t--){
        int m,d,w;cin>>m>>d>>w;
        if(d==1){
            cout<<m*(m-1)/2<<endl;
            continue;
        }
        int x=w/__gcd(d-1,w);
        int y=m/x;
        int r=m%x;
        // cout<<"x:"<<x<<endl;
        if(y==0){
            cout<<0<<endl;
            continue;
        }
        cout<<((y-1)*y/2*x+r*y)<<endl;
    }
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=100005;
ll pri[N];
int len;
bool np[N];
int c1[N],c2[N];
int main(){
	ios::sync_with_stdio(0);
	int i,j;
	int T;cin>>T;
	for(i=2;i<N;i++){
		if(!np[i]) pri[++len]=i;
		for(j=1;j<=len;j++){
			if(i*pri[j]>=N) break;
			np[i*pri[j]]=1;
			if(i%pri[j]==0) break;
		}
	}
	while(T--){
		ll m,d,w;cin>>m>>d>>w;
		ll n=min(m,d);
		ll x=d-1;
		if(x==0){
			cout<<0<<endl;
			continue;
		}
		for(i=1;i<=len;i++){
			while(x%pri[i]==0){
				x/=pri[i];
				c1[i]++;
			}
		}
		ll t0=0;
		if(x>1) t0=x;
		x=w;
		for(i=1;i<=len;i++){
			while(x%pri[i]==0){
				x/=pri[i];
				c2[i]++;
			}
		}
		ll d0=1;
		for(i=1;i<=len;i++){
			while(c2[i]>c1[i]){
				c2[i]--;
				d0*=pri[i];
			}
		}
		if(t0!=x) d0*=x;
		ll ans=n*(n/d0);
		ans-=(n/d0)*(n/d0+1)/2*d0;
		cout<<ans<<endl;
		for(i=1;i<=len;i++) c1[i]=c2[i]=0;
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=100005;
ll pri[N];
int len;
bool np[N];
int c1[N],c2[N];
int main(){
	ios::sync_with_stdio(0);
	int i,j;
	int T;cin>>T;
	for(i=2;i<N;i++){
		if(!np[i]) pri[++len]=i;
		for(j=1;j<=len;j++){
			if(i*pri[j]>=N) break;
			np[i*pri[j]]=1;
			if(i%pri[j]==0) break;
		}
	}
	while(T--){
		ll m,d,w;cin>>m>>d>>w;
		ll n=min(m,d);
		ll x=d-1;
		if(x==0){
			cout<<0<<endl;
			continue;
		}
		for(i=1;i<=len;i++){
			while(x%pri[i]==0){
				x/=pri[i];
				c1[i]++;
			}
		}
		ll t0=0;
		if(x>1) t0=x;
		x=w;
		for(i=1;i<=len;i++){
			while(x%pri[i]==0){
				x/=pri[i];
				c2[i]++;
			}
		}
		ll d0=1;
		for(i=1;i<=len;i++){
			while(c2[i]>c1[i]){
				c2[i]--;
				d0*=pri[i];
			}
		}
		if(t0!=x) d0*=x;
		ll ans=n*(n/d0);
		ans-=(n/d0)*(n/d0+1)/2*d0;
		cout<<ans<<endl;
	}
	return 0;
}


#include <iostream>
#include <stdio.h>
#include <algorithm>
using namespace std;
typedef long long ll;
#define endl '\n'

int yy = 1;

void solve(){
	int m,d,w;
	cin>>m>>d>>w;
	if(yy)
	{
	    yy = 1;
	}
	if(d == 1){
		cout<<0<<endl;
		return;
	}
	if(yy)
	{
	    yy = 1;
	}
	int z = min(m,d);
	if(yy)
	{
	    yy = 1;
	}
	w /= __gcd(w,d-1);
	if(yy)
	{
	    yy = 1;
	}
	int y = z/w;
	if(y == 0){
		cout<<0<<endl;
		return;
	}
	if(yy)
	{
	    yy = 1;
	}
	ll ans = 1ll* y*z - 1ll*y*w*(y+1)/2;
	if(yy)
	{
	    yy = 1;
	}
	cout<<ans<<endl;
}

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int t = 1;
	if(yy)
	{
	    yy = 1;
	}
	cin>>t;
	if(yy)
	{
	    yy = 1;
	}
	while(t--){
		solve();
		if(yy)
	{
	    yy = 1;
	}
	}
}


#include <iostream>
#include <stdio.h>
using namespace std;
typedef long long ll;
#define endl '\n'

int yy = 1;

void solve(){
	int m,d,w;
	cin>>m>>d>>w;
	if(yy)
	{
	    yy = 1;
	}
	if(d == 1){
		cout<<0<<endl;
		return;
	}
	if(yy)
	{
	    yy = 1;
	}
	int z = min(m,d);
	if(yy)
	{
	    yy = 1;
	}
	w /= __gcd(w,d-1);
	if(yy)
	{
	    yy = 1;
	}
	int y = z/w;
	if(y == 0){
		cout<<0<<endl;
		return;
	}
	if(yy)
	{
	    yy = 1;
	}
	ll ans = 1ll* y*z - 1ll*y*w*(y+1)/2;
	if(yy)
	{
	    yy = 1;
	}
	cout<<ans<<endl;
}

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int t = 1;
	if(yy)
	{
	    yy = 1;
	}
	cin>>t;
	if(yy)
	{
	    yy = 1;
	}
	while(t--){
		solve();
		if(yy)
	{
	    yy = 1;
	}
	}
}


//Don't act like a loser.
//You can only use the code for studying or finding mistakes
//Or,you'll be punished by Sakyamuni!!!
#include<bits/stdc++.h>
#define int long long
using namespace std;

int read() {
	char ch=getchar();
	int f=1,x=0;
	while(ch<'0'||ch>'9') {
		if(ch=='-')
			f=-1;
		ch=getchar();
	}
	while(ch>='0'&&ch<='9') {
		x=x*10+ch-'0';
		ch=getchar();
	}
	return f*x;
}

int m,d,w;

int gcd(int x,int y) {
	return y==0? x:gcd(y,x%y);
}

signed main() {
	//freopen(".in","r",stdin);
	//freopen(".out","w",stdout);

	int t=read();
	while(t--) {
		m=read();d=read();w=read();

		w=w/gcd(w,d-1);
		int n=min(m,d);
		int num=n/w;
		int ans=(2*(n-w)-w*(num-1))*num/2;

		cout<<ans<<endl;
	}

	//fclose(stdin);
	//fclose(stdout);
	return 0;
}


//Don't act like a loser.
//You can only use the code for studying or finding mistakes
//Or,you'll be punished by Sakyamuni!!!
#include<bits/stdc++.h>
#define int long long
using namespace std;

int read() {
	char ch=getchar();
	int f=1,x=0;
	while(ch<'0'||ch>'9') {
		if(ch=='-')
			f=-1;
		ch=getchar();
	}
	while(ch>='0'&&ch<='9') {
		x=x*10+ch-'0';
		ch=getchar();
	}
	return f*x;
}

int m,d,w;

int gcd(int x,int y) {
	return y==0? x:gcd(y,x%y);
}

signed main() {
	int t=read();
	while(t--) {
		m=read();d=read();w=read();

		w=w/gcd(w,d-1);
		int n=min(m,d);
		int num=n/w;
		int ans=(2*n-w*num-w)*num/2;

		cout<<ans<<endl;
	}

	return 0;
}


#include<bits/stdc++.h>

using namespace std;
using ll = long long;
using pii = pair<int, int>;
using db = double;
using vi = vector<int>;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
#define debug(x) cout << #x <<"\t" << x <<endl
const int inf = 0x3f3f3f3f;
const db eps = 1e-8;
const int mod = 1e9+7;
ll qpow(ll a, ll b){
	ll ret = 1;
	while(b){
		if(b&1)ret = ret*a%mod;
		a = a*a%mod;
		b>>=1;
	}
	return ret;
}
int n, k;
const int maxn= 2e5+7;

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	int t;
	cin >> t;
	while(t--){
		int d, m, w;
		cin >> m >> d >> w;
		int g = __gcd(d-1, w);
		w/=g;
		int n = min(d, m);
		int x = n/w;
		cout << (2 * (n - w) - w * (x - 1)) * x / 2 <<"\n";
	}

}


#include<bits/stdc++.h>

using namespace std;
using ll = long long;
using pii = pair<int, int>;
using db = double;
using vi = vector<int>;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
#define debug(x) cout << #x <<"\t" << x <<endl
const int inf = 0x3f3f3f3f;
const db eps = 1e-8;
const int mod = 1e9+7;
ll qpow(ll a, ll b){
	ll ret = 1;
	while(b){
		if(b&1)ret = ret*a%mod;
		a = a*a%mod;
		b>>=1;
	}
	return ret;
}
int n, k;
const int maxn= 2e5+7;

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	int t;
	cin >> t;
	while(t--){
		int d, m, w;
		cin >> m >> d >> w;
		int g = __gcd(d-1, w);
		w/=g;
		int n = min(d, m);
		int x = n/w;
		cout << 1ll*(2 * (n - w) - w * (x - 1)) * x / 2 <<"\n";
	}

}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		int n,d,w;
		scanf("%d%d%d",&n,&d,&w);
		int m=min(n,d);
		if((d-1)%w==0)
		{
			printf("%lld\n",m*(m-1)/2);
		}
		else{
		int gcd=__gcd(d-1,w);
		int now=w/gcd;
		int num=(m-1)/now;
		ll ans=1ll*(m)*num-1ll*(num+1)*num*now/2;
		printf("%lld\n",ans);
	    }
	}
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		int n,d,w;
		scanf("%d%d%d",&n,&d,&w);
		int m=min(n,d);
		if((d-1)%w==0)
		{
			printf("%lld\n",1ll*m*(m-1)/2);
		}
		else{
		int gcd=__gcd(d-1,w);
		int now=w/gcd;
		int num=(m-1)/now;
		ll ans=1ll*(m)*num-1ll*(num+1)*num*now/2;
		printf("%lld\n",ans);
	    }
	}
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int m, d, w;

int gcd(int a, int b) {
    if (b == 0) return a;
    return gcd(b, a % b);
}

ll solve() {
    ll tt = 0, mn = min(d, m), w2 = w / gcd(d - 1, w);
    for (int i = 1; i * w2 < mn; i++) {
        tt += mn - w2 * i;
    }
    return tt;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(0);
    int t;
    cin >> t;
    for (int ti = 0; ti < t; ti++) {
        cin >> m >> d >> w;
        cout << solve() << "\n";
    }
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int m, d, w;

int gcd(int a, int b) {
    if (b == 0) return a;
    return gcd(b, a % b);
}

ll solve() {
    ll mn = min(d, m), w2 = w / gcd(d - 1, w), mxI = mn / w2;
    return mxI * (2 * mn - (mxI + 1) * w2) / 2;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(0);
    int t;
    cin >> t;
    for (int ti = 0; ti < t; ti++) {
        cin >> m >> d >> w;
        cout << solve() << "\n";
    }
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int m, d, w;

int gcd(int a, int b) {
    if (b == 0) return a;
    return gcd(b, a % b);
}

ll solve() {
    ll tt = 0, mn = min(d, m), w2 = w / gcd(d - 1, w), mxI = mn / w2, cm = 0;
    for (int i = 1; i <= mxI; i++) {
        cm += w2;
        tt += mn - cm;
    }
    return tt;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(0);
    int t;
    cin >> t;
    for (int ti = 0; ti < t; ti++) {
        cin >> m >> d >> w;
        cout << solve() << "\n";
    }
}


#include <bits/stdc++.h>
#define int long long
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define pre(i,a,b) for(int i=(b);i>=(a);i--)
#define ios ios::sync_with_stdio(false),cin.tie(0);
#define pb push_back
#define ps push
#define fi first
#define se second
#define ps push
#define Inf 0x3f3f3f3f3f3f3f3f
#define eps 1e-5
typedef long long ll;

const int N=2e5+5;
const int M=2e5+5;
const int mod=998244353;
using namespace std;

ll quick_power_mod(ll a,ll b,ll m)//pow(a,b)%m
{
    a%=m;
  ll result = 1;
  while(b>0)
  {
    if(b&1)
    result=(result*a)%m;
    a=(a*a)%m;
    b>>=1;
  }
  return result;
}
//计算组合数取模
ll comp(ll a,ll b,int p)//composite num C(a,b)%p
{
  if(a<b)return 0;
  if(a==b)return 1;
  if(b>a-b)b=a-b;
  ll ans=1,ca=1,cb=1;
  for(ll i=0;i<b;i++)
  {
    ca=(ca*(a-i))%p;
    cb=(cb*(b-i))%p;
  }
  ans=(ca*quick_power_mod(cb,p-2,p))%p;
  return ans;
}
ll lucas(ll n,ll m,ll p)
{
  ll ans=1;
  while(n&&m&&ans)
  {
    ans=(ans*comp(n%p,m%p,p))%p;//also can be recusive
    n/=p;
    m/=p;
  }
  return ans;
}

bool flagg[1005];
int numm[1005];
int cntt = 1;
void PRIME(){
    flagg[0] = false;
    flagg[1] = false;
    for(int i = 2;i*i<1000;i++){
        if(flagg[i]){
            for(int j = i;i*j<1000;j++)
                flagg[i*j] = false;
        }
    }
    for(int i = 1;i<1000;i++)
        if(flagg[i])
            numm[cntt++] = i;
}
ll gcd(ll a,ll b){ return b==0?a:gcd(b,a%b); }
;
pair<int , int>p1[N] , p2[N];
signed main()
{
  ios;
  int T = 1;
  cin >> T;
  int x,y,z,w;
  PRIME();
  lucas(2000,1500,mod);
  while(T --)
  {
     int xx=0;

     cin>>x>>y>>z;
      rep(i,1,100){
        xx+=gcd(x,y);
     }
     lucas(5000,2000,mod);
     w=w=z/gcd(z,y-1);
     int tmp=(min(x,y)-1)/w;
     int  ans=tmp*min(x,y)-tmp*(tmp+1)/2*w;
     lucas(5000,3000,mod);
      rep(i,1,200){
        xx+=quick_power_mod(2,i,mod);
     }
     cout<<ans<<endl;
//
  }
  lucas(2000,1200,mod);
  return 0 ;
}


#include <bits/stdc++.h>
#define int long long
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define pre(i,a,b) for(int i=(b);i>=(a);i--)
#define ios ios::sync_with_stdio(false),cin.tie(0);
#define pb push_back
#define ps push
#define fi first
#define se second
#define ps push
#define Inf 0x3f3f3f3f3f3f3f3f
#define eps 1e-5
typedef long long ll;

const int N=2e5+5;
const int M=2e5+5;
const int mod=998244353;
using namespace std;

ll quick_power_mod(ll a,ll b,ll m)//pow(a,b)%m
{
    a%=m;
  ll result = 1;
  while(b>0)
  {
    if(b&1)
    result=(result*a)%m;
    a=(a*a)%m;
    b>>=1;
  }
  return result;
}
//计算组合数取模
ll comp(ll a,ll b,int p)//composite num C(a,b)%p
{
  if(a<b)return 0;
  if(a==b)return 1;
  if(b>a-b)b=a-b;
  ll ans=1,ca=1,cb=1;
  for(ll i=0;i<b;i++)
  {
    ca=(ca*(a-i))%p;
    cb=(cb*(b-i))%p;
  }
  ans=(ca*quick_power_mod(cb,p-2,p))%p;
  return ans;
}
ll lucas(ll n,ll m,ll p)
{
  ll ans=1;
  while(n&&m&&ans)
  {
    ans=(ans*comp(n%p,m%p,p))%p;//also can be recusive
    n/=p;
    m/=p;
  }
  return ans;
}

bool flagg[1005];
int numm[1005];
int cntt = 1;
void PRIME(){
    flagg[0] = false;
    flagg[1] = false;
    for(int i = 2;i*i<1000;i++){
        if(flagg[i]){
            for(int j = i;i*j<1000;j++)
                flagg[i*j] = false;
        }
    }
    for(int i = 1;i<1000;i++)
        if(flagg[i])
            numm[cntt++] = i;
}
ll gcd(ll a,ll b){ return b==0?a:gcd(b,a%b); }
;
pair<int , int>p1[N] , p2[N];
signed main()
{
  ios;
  int T = 1;
  cin >> T;
  int x,y,z,w;
  PRIME();
  lucas(2000,1500,mod);
  while(T --)
  {
     int xx=0;

     cin>>x>>y>>z;
      rep(i,1,100){
        xx+=gcd(x,y);
     }
     lucas(5000,2000,mod);
     w=w=z/gcd(z,y-1);
     int tmp=(min(x,y)-1)/w;
     int  ans=tmp*min(x,y)-tmp*(tmp+1)/2*w;
     lucas(5000,3000,mod);
      rep(i,1,200){
        xx+=quick_power_mod(2,i,mod);
     }
     cout<<ans<<endl;

  }
  lucas(2000,1200,mod);
  return 0 ;
}


#include<bits/stdc++.h>
#define LL long long
#define ull unsigned long long
#define F(i,j,k) for(int i=(j);i<=(k);i++)
#define DF(i,j,k) for(int i=(j);i>=(k);i--)
#define M make_pair
#define P pair
#define dui priority_queue
using namespace std;
template<typename T>inline void read(T &n){
    T w=1;n=0;char ch=getchar();
    while(!isdigit(ch)&&ch!=EOF){if(ch=='-')w=-1;ch=getchar();}
    while(isdigit(ch)&&ch!=EOF){n=(n<<1)+(n<<3)+(ch^48);ch=getchar();}
    n*=w;
}
template<typename T>inline void write(T x){
    ull y=0;
    T l=0;
    if(x<0){x=-x;putchar('-');}
    if(!x){putchar(48);return;}
    while(x){y=y*10+x%10;x/=10;l++;}
    while(l){putchar(y%10+48);y/=10;l--;}
}
template<typename T>inline void writeln(T x){
    write(x);
    puts("");
}
template<typename T>inline void writes(T x){
    write(x);
    putchar(' ');
}
template<typename T>inline void checkmax(T &a,T b){a=a>b?a:b;}
template<typename T>inline void checkmin(T &a,T b){a=a<b?a:b;}
inline LL c(LL x){return x*(x-1LL)/2;}
inline LL gcd(LL x,LL y){return y==0?x:gcd(y,x%y);}
int main(){
 	//freopen(".in","r",stdin);
 	//freopen(".out","w",stdout);
 	int t;read(t);
 	while(t--){
 		LL m,d,w;read(m);read(d);read(w);
 		checkmin(m,d);
 		d--;
 		LL g=w/gcd(d,w);
 		if(m%g==0)writeln(c(m/g)*g);
 		else {
 			LL m1=m/g;
 			LL m2=m/g+1,s2=m%g;
 			LL s1=g-s2;
 			writeln(c(m1)*s1+c(m2)*s2);
 		}
 	}
    return 0;
}


// writer: w33z8kqrqk8zzzx33
#include <bits/stdc++.h>
using namespace std;

// begin fast read template by CYJian (source: https://www.luogu.com.cn/paste/i11c3ppx)

namespace io {
	const int __SIZE = (1 << 21) + 1;
	char ibuf[__SIZE], *iS, *iT, obuf[__SIZE], *oS = obuf, *oT = oS + __SIZE - 1, __c, qu[55]; int __f, qr, _eof;
	#define Gc() (iS == iT ? (iT = (iS = ibuf) + fread (ibuf, 1, __SIZE, stdin), (iS == iT ? EOF : *iS ++)) : *iS ++)
	inline void flush () { fwrite (obuf, 1, oS - obuf, stdout), oS = obuf; }
	inline void gc (char &x) { x = Gc(); }
	inline void pc (char x) { *oS ++ = x; if (oS == oT) flush (); }
	inline void pstr (const char *s) { int __len = strlen(s); for (__f = 0; __f < __len; ++__f) pc (s[__f]); }
	inline void gstr (char *s) { for(__c = Gc(); __c < 32 || __c > 126 || __c == ' ';)  __c = Gc();
		for(; __c > 31 && __c < 127 && __c != ' '; ++s, __c = Gc()) *s = __c; *s = 0; }
	template <class I> inline bool gi (I &x) { _eof = 0;
		for (__f = 1, __c = Gc(); (__c < '0' || __c > '9') && !_eof; __c = Gc()) { if (__c == '-') __f = -1; _eof |= __c == EOF; }
		for (x = 0; __c <= '9' && __c >= '0' && !_eof; __c = Gc()) x = x * 10 + (__c & 15), _eof |= __c == EOF; x *= __f; return !_eof; }
	template <class I> inline void print (I x) { if (!x) pc ('0'); if (x < 0) pc ('-'), x = -x;
		while (x) qu[++ qr] = x % 10 + '0',  x /= 10; while (qr) pc (qu[qr --]); }
	struct Flusher_ {~Flusher_(){flush();}}io_flusher_;
} using io::pc; using io::gc; using io::pstr; using io::gstr; using io::gi; using io::print;

// end fast read template by CYJian

#define iter(i, a, b) for(int i=(a); i<(b); i++)
#define rep(i, a) iter(i, 0, a)
#define rep1(i, a) iter(i, 1, (a)+1)
#define log(a) cerr<<"\033[32m[DEBUG] "<<#a<<'='<<(a)<<" @ line "<<__LINE__<<"\033[0m"<<endl
#define all(a) a.begin(), a.end()
#define fi first
#define se second
#define pb push_back
#define mp make_pair

using ll=long long;
using pii=pair<int, int>;
//#define int ll
const int MOD = 1000000007;

signed main() {
    ios_base::sync_with_stdio(false); cin.tie(0);
    int T; gi(T);
	while(T--) {
		int m, d, w; gi(m), gi(d), gi(w);
		if(d == 1) {
			pstr("0\n");
			continue;
		}
		ll th = min(m, d);
		ll g = w / __gcd(w, d-1);
	    ll HI = th/g;
		ll ans = th * HI - g * (HI * (HI+1) / 2);
		print(ans); pc('\n');
	}
}


#include <bits/stdc++.h>
#define fastIO ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define ll long long
#define mx LLONG_MAX
#define mn LLONG_MIN
#define pii pair<int,int>
#define vi vector<int>
#define vll vector< long long >
#define pb push_back
#define pop pop_back
#define all(x) (x).begin(), (x).end()
#define F first
#define S second
#define f(i,a,n) for( i = a ; i < n ; i++)
using namespace std;

int main()
{
    #ifndef ONLINE_JUDGE
        freopen ("input.txt","r",stdin);
        freopen ("output.txt","w",stdout);
    #endif
    fastIO;
    int TESTS = 1;
    cin>>TESTS;
    while(TESTS--)
    {
        ll i,m,d,w; cin>>m>>d>>w;

        if(d == 1)
        {
            cout<<"0"<<endl;
            continue;
        }

        ll hcf = __gcd(d-1,w);

        ll fac = w/hcf, t = fac, ans = 0 , r = min(d,m)/t;

        ans = r*min(d,m) - (r*(r+1))/2*t;

        cout<<ans<<endl;

    }
}


#include <bits/stdc++.h>
#define fastIO ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define ll long long
#define mx LLONG_MAX
#define mn LLONG_MIN
#define pii pair<int,int>
#define vi vector<int>
#define vll vector< long long >
#define pb push_back
#define pop pop_back
#define all(x) (x).begin(), (x).end()
#define F first
#define S second
#define f(i,a,n) for( i = a ; i < n ; i++)
using namespace std;

int main()
{
    #ifndef ONLINE_JUDGE
        freopen ("input.txt","r",stdin);
        freopen ("output.txt","w",stdout);
    #endif
    fastIO;
    int TESTS = 1;
    cin>>TESTS;
    while(TESTS--)
    {
        ll i,m,d,w; cin>>m>>d>>w;

        if(d == 1)
        {
            cout<<"0"<<endl;
            continue;
        }

        ll hcf = __gcd(d-1,w);

        ll fac = w/hcf, t = fac, ans = 0; i = 2;

        while(t<min(m,d))
        {
            ans+=(min(m,d)-t);
            t = fac*(i++);
        }

        cout<<ans<<endl;

    }
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

template<typename has_less>
using ordered_set =
tree<has_less,
	null_type,
	less<has_less>,
	rb_tree_tag,
	tree_order_statistics_node_update>;

typedef long double ld;
typedef long long ll;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;

#define pb push_back
#define F first
#define S second
#define ALL(v) v.begin(),v.end()
#define fast_cin() ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
#define lc sidx * 2
#define rc lc + 1
#define smid ((i + j) >> 1)

#define TRACE
#ifndef ONLINE_JUDGE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
	cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
	const char* comma = strchr(names + 1, ',');cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
#else
#define trace(...)
#endif

const int mod = 1e9+7;
const ll inf = 5e18;

int main() {
	fast_cin();
	int t; cin >> t;
	while(t--) {
		ll m, d, w; cin >> m >> d >> w;
		ll mn = min(m, d);
		ll ans = 0;
		// if(d % w == 1) {
		// 	ans = (mn * (mn - 1)) / 2;
		// } else {
			ll dw = (d % w) - 1;
			ll mdl = (w / (__gcd(dw, w)));
			ans = (mn / mdl) * ((mn % mdl));
			mn -= (mn % mdl);

			// trace(mdl, mn, ans);
			ans += (mn * ((mn / mdl) - 1)) / 2;
		// }
		cout << ans << endl;
	}
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

template<typename has_less>
using ordered_set =
tree<has_less,
	null_type,
	less<has_less>,
	rb_tree_tag,
	tree_order_statistics_node_update>;

typedef long double ld;
typedef long long ll;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;

#define pb push_back
#define F first
#define S second
#define ALL(v) v.begin(),v.end()
#define fast_cin() ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
#define lc sidx * 2
#define rc lc + 1
#define smid ((i + j) >> 1)

#define TRACE
#ifndef ONLINE_JUDGE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
	cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
	const char* comma = strchr(names + 1, ',');cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
#else
#define trace(...)
#endif

const int mod = 1e9+7;
const ll inf = 5e18;

int main() {
	fast_cin();
	int t; cin >> t;
	while(t--) {
		ll m, d, w; cin >> m >> d >> w;
		ll mn = min(m, d);
		ll ans = 0;
		if(d % w == 1) {
			ans = (mn * (mn - 1)) / 2;
		} else {
			ll dw = (d % w) - 1;
			ll mdl = (w / (__gcd(dw, w)));
			ans = (mn / mdl) * ((mn % mdl));
			mn -= (mn % mdl);

			// trace(mdl, mn, ans);
			ans += (mn * ((mn / mdl) - 1)) / 2;
		}
		cout << ans << endl;
	}
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define _for(i,a,b) for(int i = (a);i < b;i ++)
#define _rep(i,a,b) for(int i = (a);i > b;i --)
#define INF 0x3f3f3f3f
#define pb push_back
#define lowbit(x) ((x)&(-x))
#define mod 1000000007
#define MIKU 39
#define maxn 100039
#define Design ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)

ll n, x;
ll a[maxn];
bool judge(int y)
{
	return true;
}
int Bsearch(int l,int r)
{
	while(l < r)
	{
		int mid = (l + r + 1) >> 1;
		if(judge(mid))
			l = mid;
		else
			r = mid - 1;
	}
	return l;
}
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%lld%lld",&n,&x);
		_for(i,1,n+1)
			scanf("%lld",&a[i]);
		sort(a+1,a+1+n);
		ll ans = 0;
		ll cur = 1;
		ll minn = a[1];
		_for(i,1,n+1)
		{
			if(cur * minn >= x)
			{
				ans ++;
				cur = 1;
				minn = a[i];
			}
			else
				cur ++;
		}
		if(cur * minn >= x)
			ans ++;
		else if(cur == n)
			ans = 0;
		printf("%lld\n",ans);
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define _for(i,a,b) for(int i = (a);i < b;i ++)
#define _rep(i,a,b) for(int i = (a);i > b;i --)
#define INF 0x3f3f3f3f
#define pb push_back
#define lowbit(x) ((x)&(-x))
#define mod 1000000007
#define MIKU 39
#define maxn 100039
#define Design ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)

ll n, x;
ll a[maxn];
bool judge(int y)
{
	return true;
}
int Bsearch(int l,int r)
{
	while(l < r)
	{
		int mid = (l + r + 1) >> 1;
		if(judge(mid))
			l = mid;
		else
			r = mid - 1;
	}
	return l;
}
int main()
{
	Design;
	int t;
	cin >> t;
	while(t--)
	{
		scanf("%lld%lld",&n,&x);
		_for(i,1,n+1)
			scanf("%lld",&a[i]);
		sort(a+1,a+1+n);
		ll ans = 0;
		ll cur = 1;
		ll minn = a[1];
		_for(i,1,n+1)
		{
			if(cur * minn >= x)
			{
				ans ++;
				cur = 1;
				minn = a[i];
			}
			else
			{
				cur ++;
			}
		}
		if(cur * minn >= x)
			ans ++;
		printf("%lld\n",ans);
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define _for(i,a,b) for(int i = (a);i < b;i ++)
#define _rep(i,a,b) for(int i = (a);i > b;i --)
#define INF 0x3f3f3f3f
#define pb push_back
#define lowbit(x) ((x)&(-x))
#define mod 1000000007
#define MIKU 39
#define maxn 100039
#define Design ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)

int n, x;
ll a[maxn];
bool judge(int y)
{
	return true;
}
int Bsearch(int l,int r)
{
	while(l < r)
	{
		int mid = (l + r + 1) >> 1;
		if(judge(mid))
			l = mid;
		else
			r = mid - 1;
	}
	return l;
}
int main()
{
	Design;
	int t;
	cin >> t;
	while(t--)
	{
		scanf("%d%d",&n,&x);
		_for(i,1,n+1)
			scanf("%lld",&a[i]);
		sort(a+1,a+1+n);
		ll ans = 0;
		ll cur = 1;
		ll minn = a[1];
		_for(i,1,n+1)
		{
			if(cur * minn >= x)
			{
				ans ++;
				cur = 1;
				minn = a[i];
			}
			else
			{
				cur ++;
			}
		}
		if(cur * minn >= x)
		{
			ans ++;
		}
		printf("%lld\n",ans);
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define _for(i,a,b) for(int i = (a);i < b;i ++)
#define _rep(i,a,b) for(int i = (a);i > b;i --)
#define INF 0x3f3f3f3f
#define pb push_back
#define lowbit(x) ((x)&(-x))
#define mod 1000000007
#define MIKU 39
#define maxn 100039
#define Design ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)

ll n, x;
ll a[maxn];
bool judge(int y)
{
	return true;
}
int Bsearch(int l,int r)
{
	while(l < r)
	{
		int mid = (l + r + 1) >> 1;
		if(judge(mid))
			l = mid;
		else
			r = mid - 1;
	}
	return l;
}
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%lld%lld",&n,&x);
		_for(i,1,n+1)
			scanf("%lld",&a[i]);
		sort(a+1,a+1+n,greater<ll>());
		ll ans = 0;
		ll cur = 1;
		_for(i,1,n+1)
		{
			if(cur * a[i] >= x)
			{
				ans ++;
				cur = 1;
			}
			else
				cur ++;
		}
		printf("%lld\n",ans);
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define _for(i,a,b) for(int i = (a);i < b;i ++)
#define _rep(i,a,b) for(int i = (a);i > b;i --)
#define INF 0x3f3f3f3f
#define pb push_back
#define lowbit(x) ((x)&(-x))
#define mod 1000000007
#define MIKU 39
#define maxn 100039
#define Design ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)

ll n, x;
ll a[maxn];
bool judge(int y)
{
	return true;
}
int Bsearch(int l,int r)
{
	while(l < r)
	{
		int mid = (l + r + 1) >> 1;
		if(judge(mid))
			l = mid;
		else
			r = mid - 1;
	}
	return l;
}
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%lld%lld",&n,&x);
		_for(i,1,n+1)
			scanf("%lld",&a[i]);
		sort(a+1,a+1+n);
		ll ans = 0;
		ll cur = 1;
		ll minn = a[1];
		_for(i,1,n+1)
		{
			if(cur * minn >= x)
			{
				ans ++;
				cur = 1;
				minn = a[i];
			}
			else
			{
				cur ++;
			}
		}
		if(cur * minn >= x)
			ans ++;
		printf("%lld\n",ans);
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define _for(i,a,b) for(int i = (a);i < b;i ++)
#define _rep(i,a,b) for(int i = (a);i > b;i --)
#define INF 0x3f3f3f3f
#define pb push_back
#define lowbit(x) ((x)&(-x))
#define mod 1000000007
#define MIKU 39
#define maxn 100039
#define Design ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)

ll n, x;
ll a[maxn];
bool judge(int y)
{
	return true;
}
int Bsearch(int l,int r)
{
	while(l < r)
	{
		int mid = (l + r + 1) >> 1;
		if(judge(mid))
			l = mid;
		else
			r = mid - 1;
	}
	return l;
}
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%lld%lld",&n,&x);
		_for(i,1,n+1)
			scanf("%lld",&a[i]);
		sort(a+1,a+1+n);
		ll ans = 0;
		ll cur = 1;
		ll minn = a[1];
		_for(i,1,n+1)
		{
			if(cur * minn >= x)
			{
				ans ++;
				cur = 1;
				minn = a[i];
			}
			else
				cur ++;
		}
		if(cur * minn >= x)
			ans ++;
		else
			ans = 0;
		printf("%lld\n",ans);
	}
	return 0;
}


#include <bits/stdc++.h>
#define int int64_t
using namespace std;

void done(){
  int n , x; cin>>n>>x;
  vector<int>v(n);
  for(int& i : v)cin>>i;
  int ans = 0;
  sort(v.rbegin() , v.rend());
  for(int i = 0; i < n; ++i){
    int j = i , cnt = 1;
    while(j < n && v[j] * cnt < x){
      j++; cnt++;
    }
    if(v[j] * cnt >= x)ans++;
    i = j;
  }
  cout<<ans<<endl;
}

int32_t main() {
  int t; cin>>t;
  for(;t--;)done();
}


#include <bits/stdc++.h>

using namespace std;

void done(){
  int n , x; cin>>n>>x;
  vector<int>v(n);
  for(int& i : v)cin>>i;
  int ans = 0;
  sort(v.rbegin() , v.rend());
  for(int i = 0; i < n; ++i){
    int j = i , cnt = 1;
    while(j < n && v[j] * cnt < x){
      j++; cnt++;
    }
    if(v[j] * cnt >= x)ans++;
    i = j;
  }
  cout<<ans<<endl;
}

int main() {
  int t; cin>>t;
  for(;t--;)done();
}


#include <bits/stdc++.h>

using namespace std;

void done(){
  int n , x; cin>>n>>x;
  vector<int>v(n);
  for(int& i : v)cin>>i;
  int ans = 0;
  sort(v.rbegin() , v.rend());
  for(int i = 0; i < n; ++i){
    int j = i , cnt = 1;
    while(j < n && (long long)v[j] * cnt < x){
      j++; cnt++;
    }
    if((long long)v[j] * cnt >= x)ans++;
    i = j;
  }
  cout<<ans<<endl;
}

int main() {
  int t; cin>>t;
  for(;t--;)done();
}


#include <bits/stdc++.h>

using namespace std;

void done(){
  int n , x; cin>>n>>x;
  vector<int>v(n);
  for(int& i : v)cin>>i;
  int ans = 0;
  sort(v.rbegin() , v.rend());

  for(int i = 0; i < n; ++i){
    int j = i , cnt = 1;
    while(j < n - 1 && v[j] * cnt < x){
      ++j; cnt++;
    }
    if(v[j] * cnt >= x)ans++;
    i = j;
  }
  cout<<ans<<endl;
}

int main() {
  int t; cin>>t;
  for(;t--;)done();
}


#include<bits/stdc++.h>
#define ll long long
#define ld double
#define endl "\n"
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define vc vector
#define ff first
#define ss second
#define ll_limit 1000000000000000000
#define maxn 2000007
#define prime_fact_value 1e7+5
#define fastIO ios_base::sync_with_stdio(0); cin.tie(0),cout.tie(0);
using namespace std;

//ll mod_all(ll n, ll p){ if(p==-1) p=mod-2; ll ret = 1; while(p){ if(p&1) ret = (ret*1LL*n)%mod; p>>=1; n=(n*1LL*n)%mod; } return ret; }

/*
***************prime fact and prime number****************
vc<pair<int,int>> v; vc<bool> isPrime; vc<int> sFactor,primes;
void prime_fact(){
    isPrime.assign(prime_fact_value+1,true); sFactor.assign(prime_fact_value+1,0); isPrime[0]=isPrime[1]=false; primes={};
    for(ll i=2;i<=prime_fact_value;i++){ if(isPrime[i]){ sFactor[i]=i; primes.pb(i); for(ll j=i*i;j<=prime_fact_value;j+=i){ if(isPrime[j]){ isPrime[j]=false; sFactor[j]=i; } } } }
}
*/

void solve_with_case(int cas){
    cout<<"Case "<<++cas<<": ";
}
void solve_without_case(){
	int n,x; cin>>n>>x;
	int arr[n];
	for(int i=0;i<n;i++) cin>>arr[i];
	sort(arr,arr+n);
	int cnt=0;
	for(int i=0,j=n-1;i<j;i++,j--){
		//cout<<i<<" "<<j<<endl;
		if(arr[i]*arr[j]>=x) cnt++;
		else break;
	}
	cout<<cnt<<endl;
}

int main()
{
    fastIO;
#ifndef ONLINE_JUDGE
    double start_time = clock();
    freopen("kacchi.txt","r",stdin);
    freopen("burger.txt","w",stdout);
#endif

     int t,cas=0; cin>>t;
     while(t--){
         solve_without_case();
    //     solve_with_case(cas);
	   //  cas++;
     }

#ifndef ONLINE_JUDGE
    double end_time = clock();
    cout<<"Time = "<<((end_time-start_time)/CLOCKS_PER_SEC)<<"\n";
#endif

}


#include<bits/stdc++.h>
#define ll long long
#define ld double
#define endl "\n"
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define vc vector
#define ff first
#define ss second
#define ll_limit 1000000000000000000
#define maxn 2000007
#define prime_fact_value 1e7+5
#define fastIO ios_base::sync_with_stdio(0); cin.tie(0),cout.tie(0);
using namespace std;

//ll mod_all(ll n, ll p){ if(p==-1) p=mod-2; ll ret = 1; while(p){ if(p&1) ret = (ret*1LL*n)%mod; p>>=1; n=(n*1LL*n)%mod; } return ret; }

/*
***************prime fact and prime number****************
vc<pair<int,int>> v; vc<bool> isPrime; vc<int> sFactor,primes;
void prime_fact(){
    isPrime.assign(prime_fact_value+1,true); sFactor.assign(prime_fact_value+1,0); isPrime[0]=isPrime[1]=false; primes={};
    for(ll i=2;i<=prime_fact_value;i++){ if(isPrime[i]){ sFactor[i]=i; primes.pb(i); for(ll j=i*i;j<=prime_fact_value;j+=i){ if(isPrime[j]){ isPrime[j]=false; sFactor[j]=i; } } } }
}
*/

void solve_with_case(int cas){
    cout<<"Case "<<++cas<<": ";
}
void solve_without_case(){
	ll n,x; cin>>n>>x;
	ll arr[n];
	for(int i=0;i<n;i++) cin>>arr[i];
	sort(arr,arr+n,greater<ll>());
	int cnt=0;
	vc<int> v;
	ll miin = ll_limit;
	for(int i=0;i<n;i++){
		if(miin>arr[i]) miin = arr[i];
		v.pb(arr[i]);
		ll siz = v.size();
		ll ans = siz*miin;
		if(ans>=x){
			//cout<<miin<<" "<<v.size()<<endl;
			cnt++;
			miin = ll_limit;
			v.clear();
		}
	}
	cout<<cnt<<endl;
}

int main()
{
    fastIO;
#ifndef ONLINE_JUDGE
    double start_time = clock();
    freopen("kacchi.txt","r",stdin);
    freopen("burger.txt","w",stdout);
#endif

     int t,cas=0; cin>>t;
     while(t--){
         solve_without_case();
    //     solve_with_case(cas);
	   //  cas++;
     }

#ifndef ONLINE_JUDGE
    double end_time = clock();
    cout<<"Time = "<<((end_time-start_time)/CLOCKS_PER_SEC)<<"\n";
#endif

}


#include<bits/stdc++.h>
#define ll long long
#define ld double
#define endl "\n"
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define vc vector
#define ff first
#define ss second
#define ll_limit 1000000000000000000
#define maxn 2000007
#define prime_fact_value 1e7+5
#define fastIO ios_base::sync_with_stdio(0); cin.tie(0),cout.tie(0);
using namespace std;

//ll mod_all(ll n, ll p){ if(p==-1) p=mod-2; ll ret = 1; while(p){ if(p&1) ret = (ret*1LL*n)%mod; p>>=1; n=(n*1LL*n)%mod; } return ret; }

/*
***************prime fact and prime number****************
vc<pair<int,int>> v; vc<bool> isPrime; vc<int> sFactor,primes;
void prime_fact(){
    isPrime.assign(prime_fact_value+1,true); sFactor.assign(prime_fact_value+1,0); isPrime[0]=isPrime[1]=false; primes={};
    for(ll i=2;i<=prime_fact_value;i++){ if(isPrime[i]){ sFactor[i]=i; primes.pb(i); for(ll j=i*i;j<=prime_fact_value;j+=i){ if(isPrime[j]){ isPrime[j]=false; sFactor[j]=i; } } } }
}
*/

void solve_with_case(int cas){
    cout<<"Case "<<++cas<<": ";
}
void solve_without_case(){
	ll n,x; cin>>n>>x;
	ll arr[n];
	for(int i=0;i<n;i++) cin>>arr[i];
	sort(arr,arr+n);
	int cnt=0;
	for(int i=0,j=n-1;i<j;i++,j--){
		ll ans = arr[i]*arr[j];
		if(ans>=x) cnt++;
		else break;
	}
	cout<<cnt<<endl;
}

int main()
{
    fastIO;
#ifndef ONLINE_JUDGE
    double start_time = clock();
    freopen("kacchi.txt","r",stdin);
    freopen("burger.txt","w",stdout);
#endif

     int t,cas=0; cin>>t;
     while(t--){
         solve_without_case();
    //     solve_with_case(cas);
	   //  cas++;
     }

#ifndef ONLINE_JUDGE
    double end_time = clock();
    cout<<"Time = "<<((end_time-start_time)/CLOCKS_PER_SEC)<<"\n";
#endif

}


#include<bits/stdc++.h>
#define ll long long
#define ld double
#define endl "\n"
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define vc vector
#define ff first
#define ss second
#define ll_limit 1000000000000000000
#define maxn 2000007
#define prime_fact_value 1e7+5
#define fastIO ios_base::sync_with_stdio(0); cin.tie(0),cout.tie(0);
using namespace std;

//ll mod_all(ll n, ll p){ if(p==-1) p=mod-2; ll ret = 1; while(p){ if(p&1) ret = (ret*1LL*n)%mod; p>>=1; n=(n*1LL*n)%mod; } return ret; }

/*
***************prime fact and prime number****************
vc<pair<int,int>> v; vc<bool> isPrime; vc<int> sFactor,primes;
void prime_fact(){
    isPrime.assign(prime_fact_value+1,true); sFactor.assign(prime_fact_value+1,0); isPrime[0]=isPrime[1]=false; primes={};
    for(ll i=2;i<=prime_fact_value;i++){ if(isPrime[i]){ sFactor[i]=i; primes.pb(i); for(ll j=i*i;j<=prime_fact_value;j+=i){ if(isPrime[j]){ isPrime[j]=false; sFactor[j]=i; } } } }
}
*/

void solve_with_case(int cas){
    cout<<"Case "<<++cas<<": ";
}
void solve_without_case(){
	ll n,x; cin>>n>>x;
	ll arr[n];
	for(int i=0;i<n;i++) cin>>arr[i];
	sort(arr,arr+n);
	int cnt=0;
	vc<int> v;
	ll miin = ll_limit;
	for(int i=0;i<n;i++){
		if(miin>arr[i]) miin = arr[i];
		v.pb(arr[i]);
		ll siz = v.size();
		ll ans = siz*miin;
		if(ans>=x){
			//cout<<miin<<" "<<v.size()<<endl;
			cnt++;
			miin = ll_limit;
		}
	}
	cout<<cnt<<endl;
}

int main()
{
    fastIO;
#ifndef ONLINE_JUDGE
    double start_time = clock();
    freopen("kacchi.txt","r",stdin);
    freopen("burger.txt","w",stdout);
#endif

     int t,cas=0; cin>>t;
     while(t--){
         solve_without_case();
    //     solve_with_case(cas);
	   //  cas++;
     }

#ifndef ONLINE_JUDGE
    double end_time = clock();
    cout<<"Time = "<<((end_time-start_time)/CLOCKS_PER_SEC)<<"\n";
#endif

}


#include<bits/stdc++.h>
#define ll long long
#define ld double
#define endl "\n"
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define vc vector
#define ff first
#define ss second
#define ll_limit 1000000000000000000
#define maxn 2000007
#define prime_fact_value 1e7+5
#define fastIO ios_base::sync_with_stdio(0); cin.tie(0),cout.tie(0);
using namespace std;

//ll mod_all(ll n, ll p){ if(p==-1) p=mod-2; ll ret = 1; while(p){ if(p&1) ret = (ret*1LL*n)%mod; p>>=1; n=(n*1LL*n)%mod; } return ret; }

/*
***************prime fact and prime number****************
vc<pair<int,int>> v; vc<bool> isPrime; vc<int> sFactor,primes;
void prime_fact(){
    isPrime.assign(prime_fact_value+1,true); sFactor.assign(prime_fact_value+1,0); isPrime[0]=isPrime[1]=false; primes={};
    for(ll i=2;i<=prime_fact_value;i++){ if(isPrime[i]){ sFactor[i]=i; primes.pb(i); for(ll j=i*i;j<=prime_fact_value;j+=i){ if(isPrime[j]){ isPrime[j]=false; sFactor[j]=i; } } } }
}
*/

void solve_with_case(int cas){
    cout<<"Case "<<++cas<<": ";
}
void solve_without_case(){
	ll n,x; cin>>n>>x;
	ll arr[n];
	for(int i=0;i<n;i++) cin>>arr[i];
	sort(arr,arr+n);
	int cnt=0;
	vc<int> v;
	ll miin = ll_limit;
	for(int i=0;i<n;i++){
		if(miin>arr[i]) miin = arr[i];
		v.pb(arr[i]);
		if(v.size()*miin>=x){
			//cout<<miin<<" "<<v.size()<<endl;
			cnt++;
			miin = ll_limit;
		}
	}
	cout<<cnt<<endl;
}

int main()
{
    fastIO;
#ifndef ONLINE_JUDGE
    double start_time = clock();
    freopen("kacchi.txt","r",stdin);
    freopen("burger.txt","w",stdout);
#endif

     int t,cas=0; cin>>t;
     while(t--){
         solve_without_case();
    //     solve_with_case(cas);
	   //  cas++;
     }

#ifndef ONLINE_JUDGE
    double end_time = clock();
    cout<<"Time = "<<((end_time-start_time)/CLOCKS_PER_SEC)<<"\n";
#endif

}


#include<bits/stdc++.h>
#define ll long long
#define ld double
#define endl "\n"
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define vc vector
#define ff first
#define ss second
#define ll_limit 1000000000000000000
#define maxn 2000007
#define prime_fact_value 1e7+5
#define fastIO ios_base::sync_with_stdio(0); cin.tie(0),cout.tie(0);
using namespace std;

//ll mod_all(ll n, ll p){ if(p==-1) p=mod-2; ll ret = 1; while(p){ if(p&1) ret = (ret*1LL*n)%mod; p>>=1; n=(n*1LL*n)%mod; } return ret; }

/*
***************prime fact and prime number****************
vc<pair<int,int>> v; vc<bool> isPrime; vc<int> sFactor,primes;
void prime_fact(){
    isPrime.assign(prime_fact_value+1,true); sFactor.assign(prime_fact_value+1,0); isPrime[0]=isPrime[1]=false; primes={};
    for(ll i=2;i<=prime_fact_value;i++){ if(isPrime[i]){ sFactor[i]=i; primes.pb(i); for(ll j=i*i;j<=prime_fact_value;j+=i){ if(isPrime[j]){ isPrime[j]=false; sFactor[j]=i; } } } }
}
*/

void solve_with_case(int cas){
    cout<<"Case "<<++cas<<": ";
}
void solve_without_case(){
	ll n,x; cin>>n>>x;
	ll arr[n];
	for(int i=0;i<n;i++) cin>>arr[i];
	sort(arr,arr+n);
	int cnt=0;
	for(int i=0,j=n-1;i<=j;i++,j--){
		ll ans = arr[i]*arr[j];
		if(ans>=x) cnt++;
		else break;
	}
	cout<<cnt<<endl;
}

int main()
{
    fastIO;
#ifndef ONLINE_JUDGE
    double start_time = clock();
    freopen("kacchi.txt","r",stdin);
    freopen("burger.txt","w",stdout);
#endif

     int t,cas=0; cin>>t;
     while(t--){
         solve_without_case();
    //     solve_with_case(cas);
	   //  cas++;
     }

#ifndef ONLINE_JUDGE
    double end_time = clock();
    cout<<"Time = "<<((end_time-start_time)/CLOCKS_PER_SEC)<<"\n";
#endif

}


/// **********************************************************************************************************************
/// ********************************** ~ CODE - TrI HiEn Diamond ❤ LINH CHI ~ ******************************************
/// **********************************************************************************************************************
#include <bits/stdc++.h>
#define fi first
#define se second
#define mem0(CHI)  memset(CHI,0,sizeof(CHI))
#define memt(CHI)  memset(CHI,true,sizeof(CHI))
#define mem1(CHI)  memset(CHI,1,sizeof(CHI))
#define mp make_pair
#define PB push_back
#define btcount(x) __builtin_popcount(x)
#define sqr(x) ((x) * (x))
#define INF 9999999999
#define MOD 1000000007
#define maxn 100005

#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define FORD(i, a, b) for (int i = (a); i >= (b); --i)
#define REP(i,n) for (int i=0;i<(n);i=i+1)

#define BAYMAXX ""

#define ull unsigned long long
#define uld unsigned long double
#define ld long double
#define ll long long

const long base = 151 ;
const ll MM = 1ll * MOD * MOD ;

using namespace std ;

typedef pair <long, long> pii;
typedef pair <ll, ll> pll;
typedef vector <int> vi;
typedef vector <string> vs;

// bit operater
int BIT(int i,ll x) { return(x&(1<<i));}
ll ONBIT(int i,ll x){ return(x|(1<<i));}
ll OFFBIT(int i,ll x){return (x& ~(1<<i));}
ll FLIPBIT(int i,ll x){return (x^(1<<i));}
ll NUMBIT(ll x) {return __builtin_popcount(x);}

// math
template<class T> T GCD(T a, T b){ T r; while (b != 0) { r = a % b; a = b; b = r; } return a;}
template<class T> T LCM(T a, T b) { return a / GCD(a, b) * b; }
long n, x;
long a[maxn] ;
long F[maxn];
bool cmp ( const long &a , const long &b )
{
    return a>b ;
}
void read()
{
    cin >> n >> x ;
    for ( long i=1; i<=n; i++) cin >> a[i],F[i]=0;
    sort ( a + 1 , a + 1 + n, cmp ) ;
}

void solve()
{
    for ( long i = 1 ; i <= n ; i ++)
    {
        F[i]=F[i-1];
        if ( 1ll*a[i] * 1ll*i < x ) continue ;
        long long k = x / a[i] ;
        if ( k * a[i] < x ) k++ ;
        long l = i - k ;
        F[i] = max ( F[l]+1,F[i]) ;
    }
    cout << F[n] << endl ;
}
long te ;
int main()
{
    ios_base::sync_with_stdio(0) ; cin.tie(NULL) ; cout.tie(NULL) ;
    //freopen( BAYMAXX".inp" , "r" , stdin ) ;
    //freopen( BAYMAXX".out" , "w" , stdout ) ;
    cin >> te ;
    while ( te--)
    {
        read() ;
        solve() ;
    }
}


/// **********************************************************************************************************************
/// ********************************** ~ CODE - TrI HiEn Diamond ❤ LINH CHI ~ ******************************************
/// **********************************************************************************************************************
#include <bits/stdc++.h>
#define fi first
#define se second
#define mem0(CHI)  memset(CHI,0,sizeof(CHI))
#define memt(CHI)  memset(CHI,true,sizeof(CHI))
#define mem1(CHI)  memset(CHI,1,sizeof(CHI))
#define mp make_pair
#define PB push_back
#define btcount(x) __builtin_popcount(x)
#define sqr(x) ((x) * (x))
#define INF 9999999999
#define MOD 1000000007
#define maxn 100005

#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define FORD(i, a, b) for (int i = (a); i >= (b); --i)
#define REP(i,n) for (int i=0;i<(n);i=i+1)

#define BAYMAXX ""

#define ull unsigned long long
#define uld unsigned long double
#define ld long double
#define ll long long

const long base = 151 ;
const ll MM = 1ll * MOD * MOD ;

using namespace std ;

typedef pair <long, long> pii;
typedef pair <ll, ll> pll;
typedef vector <int> vi;
typedef vector <string> vs;

// bit operater
int BIT(int i,ll x) { return(x&(1<<i));}
ll ONBIT(int i,ll x){ return(x|(1<<i));}
ll OFFBIT(int i,ll x){return (x& ~(1<<i));}
ll FLIPBIT(int i,ll x){return (x^(1<<i));}
ll NUMBIT(ll x) {return __builtin_popcount(x);}

// math
template<class T> T GCD(T a, T b){ T r; while (b != 0) { r = a % b; a = b; b = r; } return a;}
template<class T> T LCM(T a, T b) { return a / GCD(a, b) * b; }
long n, x;
long a[maxn] ;

bool cmp ( const long &a , const long &b )
{
    return a>=b ;
}
void read()
{
    cin >> n >> x ;
    for ( long i=1; i<=n; i++) cin >> a[i];
    sort ( a + 1 , a + 1 + n, cmp ) ;
}

long F[maxn];

void solve()
{
    for ( long i=0 ; i <= n ; i ++ ) F[i]= 0 ;
    for ( long i = 1 ; i <= n ; i ++)
    {
        F[i]=F[i-1];
        if ( 1ll*a[i] * 1ll*i < x ) continue ;
        long long k = x / a[i] ;
        if ( k * a[i] < x ) k++ ;
        long l = i - k ;
        F[i] = max ( F[l]+1,F[i]) ;
    }
    cout << F[n] << endl ;
}
long te ;
int main()
{
    ios_base::sync_with_stdio(0) ; cin.tie(NULL) ; cout.tie(NULL) ;
    //freopen( BAYMAXX".inp" , "r" , stdin ) ;
    //freopen( BAYMAXX".out" , "w" , stdout ) ;
    cin >> te ;
    while ( te--)
    {
        read() ;
        solve() ;
    }
}