#include<bits/stdc++.h>
#define ll long long
const int md=1000000007;
ll inf=1000000000001;
#define fast                          \
    ios_base::sync_with_stdio(false); \
    cin.tie(0);                       \
    cout.tie(0);
/* VECTORS */
#define pb push_back
#define co(v,x) count(v.begin(),v.end(),x)
#define mxe(v) *max_element(v.begin(),v.end())
#define mne(v) *min_element(v.begin(),v.end())
#define vi vector<int>
#define vll vector<ll>
#define srt(v) sort(v.begin(),v.end())
#define rsrt(v) sort(v.begin(),v.end(),greater<int>())
/* INPUT/OUTPUT*/
#define pr printf
#define sc scanf
#define pi(a) printf("%d\n",a)
#define pll(a) printf("%lld\n",a)
#define si(a) scanf("%d",&a)
#define sll(a) scanf("%lld",&a)
/* LOOPS*/
#define fi(i,a,b) for(int i=a;i<b;i++)
#define fll(i,a,b) for(ll i=a;i<b;i++)
using namespace std;
using namespace std::chrono;
ll pw(ll a, ll b)
{
    ll c=1,m=a;
    while (b)
    {
        if(b&1) c=(c * m);
        m=(m*m);
        b/=2;
    }
    return c;
}
ll pwmd(ll a,ll b)
{
    ll c=1,m=a;
    while(b)
    {
        if(b&1) c=(c*m)%md;
        m=(m*m)%md;
        b/=2;
    }
    return c;
}
ll modinv(ll n)
{
    return pwmd(n, md - 2);
}
ll inverse(ll i){
    if(i==1) return 1;
    return (md - ((md/i)*inverse(md%i))%md+md)%md;
}
int ceel(int a,int b)
{
    if(a%b==0) return a/b;
    else return a/b+1;
}
int my_log(int n, int b)
{
    ll i=1;
    ll ans=0;
    while(1)
    {
        if(i>n)
        {
            ans--;
            break;
        }
        if(i==n) break;
        i*=b;
        ans++;
    }
    return ans;
}
bool sortbysec(const pair<ll,ll> &a,
              const pair<ll,ll> &b)
{
    return (a.second < b.second);
}
   /* auto start = high_resolution_clock::now();
    auto stop = high_resolution_clock::now();
    auto duration = duration_cast<microseconds>(stop - start);
    cerr << "Time taken: "
         << duration.count()<< "ms" <<"\n";*/
ll gcd(ll a, ll b)
{
    if (b == 0)
        return a;
    return gcd(b, a % b);

}
vector<char> capl={'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
vector<char> sml={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
string conv(int a)
{
    string b;
    int c=my_log(a,2);
    c++;
    b='1';
    for(int i=0;i<c-1;i++)
    {
        ll k=a>>(c-2-i);
        if(k&1) b+='1';
        else b+='0';
    }
    if(a==0) return "0";
    return b;
}
int main()
{
    fast;
    ll t,m,d,w,ans;
    cin>>t;
    while(t--)
    {
        cin>>m>>d>>w;
        ll p=d;
        if((d-1)%w==0)
        {
            if(m<=w) ans=0;
            else if(m>w)
            {
                m=min(m,d);
                ans=(m*(m-1))/2;
            }
        }
        else
        {
            d=(d-1)%w;
            ll mn=w;
            ll i=1;
            while(i<=sqrt(w))
            {
                if(w%i==0)
                {
                    if((d*i)%w==0) mn=min(mn,i);
                    if(((w*d)/i)%w==0) mn=min(mn,w/i);
                }
                i++;
            }
            if(m<=mn) ans=0;
            else if(m>mn)
            {
                m=min(m,p);
                ans=(m/mn)*(m-mn+m%mn)/2;
            }
        }
        cout<<ans<<"\n";
    }
}


#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;

void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T,V> &x){cerr<<'{';__print(x.first);cerr<<',';__print(x.second);cerr<<'}';}
template<typename T>
void __print(const T &x){int f=0;cerr<<'{';for(auto &i:x)cerr<<(f++ ? "," : ""),__print(i);cerr<<"}";}
void _print(){cerr<<"]\n";}
template<typename T,typename... V>
void _print(T t, V... v) {__print(t);if(sizeof...(v))cerr<<", ";_print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif

#define pb push_back
#define mp make_pair
#define F first
#define S second
#define SpeedForce ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define lb lower_bound
#define ub upper_bound

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

template<typename T> using ordered_set = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;
template<typename T> void print(T x){cerr<<x<<'\n';}
inline ll bip(ll x,ll n,ll mod){ll res=1;while(n){if(n&1){res=(res*x)%mod;}x=(x*x)%mod;n>>=1;}return res;}

const int maxn = 1e9;
const ll inf = 1e18;
const int mod = 1e9 + 7;
//const int mod = 998244353;
const int N = 1e5 + 10;

void solve() {
	ll m, d, w;
	cin >> m >> d >> w;
	cout << (min(m, d) - 1) / (w / __gcd(w, d - 1)) * ((min(m, d) - 1) % (w / __gcd(w, d - 1)) + 1) + (w / __gcd(w, d - 1)) * ((min(m, d) - 1) / (w / __gcd(w, d - 1))) * ((min(m, d) - 1) / (w / __gcd(w, d - 1)) - 1) / 2;
}

int main() {
    SpeedForce;
    int tt = 1;
    cin >> tt;
    while (tt--) {
        solve();
        cout << '\n';
    }
}


#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;

void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T,V> &x){cerr<<'{';__print(x.first);cerr<<',';__print(x.second);cerr<<'}';}
template<typename T>
void __print(const T &x){int f=0;cerr<<'{';for(auto &i:x)cerr<<(f++ ? "," : ""),__print(i);cerr<<"}";}
void _print(){cerr<<"]\n";}
template<typename T,typename... V>
void _print(T t, V... v) {__print(t);if(sizeof...(v))cerr<<", ";_print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif

#define pb push_back
#define mp make_pair
#define F first
#define S second
#define SpeedForce ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define lb lower_bound
#define ub upper_bound

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

template<typename T> using ordered_set = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;
template<typename T> void print(T x){cerr<<x<<'\n';}
inline ll bip(ll x,ll n,ll mod){ll res=1;while(n){if(n&1){res=(res*x)%mod;}x=(x*x)%mod;n>>=1;}return res;}

const int maxn = 1e9;
const ll inf = 1e18;
const int mod = 1e9 + 7;
//const int mod = 998244353;
const int N = 1e5 + 10;

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

ll sum(ll m) {
	return m * (m + 1) / 2;
}

void solve() {
	int m, d, w;
	cin >> m >> d >> w;

	ll ans = 0;
	int k = min(m, d);
	int p = d - 1;
	ll g = lcm(p, w);
	if (p) g /= p;
	else g = 1;
	//cout << g;
	//return;

	/*
	for (int z = 1; z <= 50; z++) {
		if (p * z % w == 0) {
			cout << z << ' ';
		}
	}
	return; */
	/*
	ans <= k ^ 2
	y = x + z
	((x + z - 1) * d + x) % w = ((x - 1) * d + x + z) % w;

	*/
	//for (int i = 1; i <= k; i++) {

	//}
	ll mx = (k - 1) / g;
	ans += mx * ((k - 1) % g + 1);
	ans += g * sum(mx - 1);

	//for (int i = 0; i < k; i++) ans += i / g;
	cout << ans;
	return;

	for (int x = 1; x <= k; x++) {
		//int l = 1, r = k - x;
		ans += (k - x) / g;
		/*
		for (int y = x + 1; y <= k; y++) {
			int z = y - x;
			//ans += ((z * d) % w == z % w);
			ans += (z * (d - 1) % w == 0);
			//int l = (y - 1) * d + x, r = (x - 1) * d + y;
			//if (l % w == r % w) ans++;
		}*/
	}
	cout << ans;
}

int main() {
    SpeedForce;
    int tt = 1;
    cin >> tt;
    while (tt--) {
        solve();
        cout << '\n';
    }
}


#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;

void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T,V> &x){cerr<<'{';__print(x.first);cerr<<',';__print(x.second);cerr<<'}';}
template<typename T>
void __print(const T &x){int f=0;cerr<<'{';for(auto &i:x)cerr<<(f++ ? "," : ""),__print(i);cerr<<"}";}
void _print(){cerr<<"]\n";}
template<typename T,typename... V>
void _print(T t, V... v) {__print(t);if(sizeof...(v))cerr<<", ";_print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif

#define pb push_back
#define mp make_pair
#define F first
#define S second
#define SpeedForce ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define lb lower_bound
#define ub upper_bound

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

template<typename T> using ordered_set = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;
template<typename T> void print(T x){cerr<<x<<'\n';}
inline ll bip(ll x,ll n,ll mod){ll res=1;while(n){if(n&1){res=(res*x)%mod;}x=(x*x)%mod;n>>=1;}return res;}

const int maxn = 1e9;
const ll inf = 1e18;
const int mod = 1e9 + 7;
//const int mod = 998244353;
const int N = 1e5 + 10;

void solve() {
	ll m, d, w;
	cin >> m >> d >> w;
	cout << (min(m,d)-1)/(w/__gcd(w,d-1))*((min(m,d)-1)%(w/__gcd(w,d-1))+1)+(w/__gcd(w,d-1))*((min(m,d)-1)/(w/__gcd(w,d-1)))*((min(m,d)-1)/(w/__gcd(w,d-1))-1)/2;
}

int main() {
    SpeedForce;
    int tt = 1;
    cin >> tt;
    while (tt--) {
        solve();
        cout << '\n';
    }
}


#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;

void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T,V> &x){cerr<<'{';__print(x.first);cerr<<',';__print(x.second);cerr<<'}';}
template<typename T>
void __print(const T &x){int f=0;cerr<<'{';for(auto &i:x)cerr<<(f++ ? "," : ""),__print(i);cerr<<"}";}
void _print(){cerr<<"]\n";}
template<typename T,typename... V>
void _print(T t, V... v) {__print(t);if(sizeof...(v))cerr<<", ";_print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif

#define pb push_back
#define mp make_pair
#define F first
#define S second
#define SpeedForce ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define lb lower_bound
#define ub upper_bound

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

template<typename T> using ordered_set = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;
template<typename T> void print(T x){cerr<<x<<'\n';}
inline ll bip(ll x,ll n,ll mod){ll res=1;while(n){if(n&1){res=(res*x)%mod;}x=(x*x)%mod;n>>=1;}return res;}

const int maxn = 1e9;
const ll inf = 1e18;
const int mod = 1e9 + 7;
//const int mod = 998244353;
const int N = 1e5 + 10;

void solve() {
	int m, d, w;
	cin >> m >> d >> w;
	cout << (min(m, d) - 1) / (w / __gcd(w, d - 1)) * ((min(m, d) - 1) % (w / __gcd(w, d - 1)) + 1) + (w / __gcd(w, d - 1)) * ((min(m, d) - 1) / (w / __gcd(w, d - 1))) * ((min(m, d) - 1) / (w / __gcd(w, d - 1)) - 1) / 2;
}

int main() {
    SpeedForce;
    int tt = 1;
    cin >> tt;
    while (tt--) {
        solve();
        cout << '\n';
    }
}


#include <bits/stdc++.h>

using namespace std;
#define  ll long long
#define f first
#define s second

int main()
{
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    ll t;
    cin >> t;
    while(t--)
    {
        ll m,d,w,ans=0,lst,mn;
        cin >> m >> d >> w;
        mn = min(m,d);
        if (d==1) cout << "0\n";
        else
        {
            d--;
            ll diff = w/__gcd(d,w); //the required diff
            ll en = mn-diff;
            ll tmp = en/diff;
            lst = tmp;
            ans += (lst*(lst+1)/2)*diff;
            tmp = en%diff , lst++;
            ans += tmp*lst;
            cout << ans << '\n';
        }
    }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;
#define  ll long long
#define f first
#define s second

int main()
{
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    ll t;
    cin >> t;
    while(t--)
    {
        ll m,d,w,ans=0,lst,mn;
        cin >> m >> d >> w;
        mn = min(m,d);

        d--;
        ll diff = w/__gcd(d,w); //the required diff
        ll en = mn-diff;
        ll tmp = en/diff;

        if (en > 0)
        {
            lst = tmp;
            ans += (lst*(lst+1)/2)*diff;
            tmp = en%diff , lst++;
            ans += tmp*lst;
        }
        cout << ans << '\n';
    }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;
#define  ll long long
#define ld long double
#define f first
#define s second
#define x real()
#define y imag()
#define length(a) (hypot((a).imag() , (a).real()))
#define point complex<ld>
#define perp(a) point((-(a).Y,(a).X))
#define dot(a,b) ( (conj(a)*(b)).real() )
#define cross(a,b) ( (conj(a)*(b)).imag() )
#define rotate0(p,ang) (p * exp(point(0,ang)) )

ld pi = acos(-1);

int main()
{
//    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    ll t;
    cin >> t;
    while(t--)
    {
        ll m,d,w,ans=0,lst,mn;
        cin >> m >> d >> w;
        mn = min(m,d);

        d--;
        ll diff = w/__gcd(d,w); //the required diff
        ll en = mn-diff;
        ll tmp = en/diff;

        if (en > 0)
        {
            lst = tmp;
            ans += (lst*(lst+1)/2)*diff;
            tmp = en%diff , lst++;
            ans += tmp*lst;
        }

        cout << ans << '\n';

    }

    return 0;
}


//Author: AnandRaj doubleux
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vpii;
typedef pair<ll,ll> pll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<pll> vpll;

#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define test() int t;cin>>t;while(t--)
#define all(v) v.begin(),v.end()
#define prinp(p) cout<<p.first<<" "<<p.second<<endl
#define prinv(V) for(auto v:V) cout<<v<<" ";cout<<endl
#define take(V,f,n) for(int in=f;in<f+n;in++) cin>>V[in]
#define what(x) cerr<<#x<<" = "<<x<<endl
#define KStest() int t,t1;cin>>t;t1=t;while(t--)
#define KScout cout<<"Case #"<<t1-t<<": "
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int MOD = 1e9+7,MAX = 1e6+5;
const ll INF = 1e18+5;
/////////////////FastExp///////////////////
ll powN(ll a,ll p)
{
	if(p==0) return 1;
	ll z=powN(a,p/2);
	z=(z*z)%MOD;
	if(p%2) z=(z*a)%MOD;
	return z;
}
/////////////////FastExp///////////////////
///////////////SegmentTree/////////////////
struct SegTree
{
	int n;
	vector<ll> Tree;
	void init(int sz)
	{
		n = sz;
		Tree.resize(4*n);
	}
	void set(int k, ll x)
	{
		k+=n;
		Tree[k]=x;
		for(k=k/2;k>=1;k/=2)
		{
			Tree[k]=Tree[2*k]+Tree[2*k+1];
		}
	}
	ll sum(int a, int b)
	{
		a+=n;b+=n;
		ll s=0;
		while(a<=b)
		{
			if(a%2==1) s+=Tree[a++];
			if(b%2==0) s+=Tree[b--];
			a/=2;b/=2;
		}
		return s;
	}
};
///////////////SegmentTree/////////////////
///////////////////PnC/////////////////////
struct PnC
{
	int n;
	vl F,I;
	void init(int n)
	{
		F.resize(n+1);
		I.resize(n+1);
		F[0]=I[0]=1;
		for(int i=1;i<=n;i++)
		{
			F[i]=(F[i-1]*i)%MOD;
			I[i]=powN(F[i],MOD-2);
		}
	}
	ll nCr(int n,int r)
	{
		if(r>n) return 0;
		return (((F[n]*I[r])%MOD)*I[n-r])%MOD;
	}
};
///////////////////PnC/////////////////////
//////////////////Sieve////////////////////
vector<bool> is_prime(MAX, true);
vi MinDiv(MAX);
void Sieve()
{
	is_prime[0] = is_prime[1] = false;
	int i,j;
	for (i = 2; i*i <= MAX; i++)
	{
    	if (is_prime[i])
    	{
    		MinDiv[i]=i;
        	for (j = i * i; j <= MAX; j += i)
            {
				is_prime[j] = false;
				MinDiv[j]=i;
    		}
    	}
	}
	for(int i=2;i<=MAX;i++) if(is_prime[i]) MinDiv[i]=i;
}
//////////////////Sieve////////////////////

int main()
{
	test()
	{
		int m,d,w;
		cin>>m>>d>>w;
		int g = __gcd(d-1,w);
		//(d - 1)(x - y) = 0 MOD w;

		int div = w/g;
		// y - x = p.div

		//For every p, ans += min(d , m-p.div);

		// m-p.div >= d -> p<= (m-d) / div;
		ll ans = 0;
		ll p = (m-d)/div;
		ans += p*d;

		// m-p.div>=0 -> p<=m / div;
		ll r = m/div;

		ans += (r-p)*m - ((r-p)*(p+1+r)/2)*div;

		cout<<ans<<endl;

	}
}


//Author: AnandRaj doubleux
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vpii;
typedef pair<ll,ll> pll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<pll> vpll;

#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define test() int t;cin>>t;while(t--)
#define all(v) v.begin(),v.end()
#define prinp(p) cout<<p.first<<" "<<p.second<<endl
#define prinv(V) for(auto v:V) cout<<v<<" ";cout<<endl
#define take(V,f,n) for(int in=f;in<f+n;in++) cin>>V[in]
#define what(x) cerr<<#x<<" = "<<x<<endl
#define KStest() int t,t1;cin>>t;t1=t;while(t--)
#define KScout cout<<"Case #"<<t1-t<<": "
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int MOD = 1e9+7,MAX = 1e6+5;
const ll INF = 1e18+5;
/////////////////FastExp///////////////////
ll powN(ll a,ll p)
{
	if(p==0) return 1;
	ll z=powN(a,p/2);
	z=(z*z)%MOD;
	if(p%2) z=(z*a)%MOD;
	return z;
}
/////////////////FastExp///////////////////
///////////////SegmentTree/////////////////
struct SegTree
{
	int n;
	vector<ll> Tree;
	void init(int sz)
	{
		n = sz;
		Tree.resize(4*n);
	}
	void set(int k, ll x)
	{
		k+=n;
		Tree[k]=x;
		for(k=k/2;k>=1;k/=2)
		{
			Tree[k]=Tree[2*k]+Tree[2*k+1];
		}
	}
	ll sum(int a, int b)
	{
		a+=n;b+=n;
		ll s=0;
		while(a<=b)
		{
			if(a%2==1) s+=Tree[a++];
			if(b%2==0) s+=Tree[b--];
			a/=2;b/=2;
		}
		return s;
	}
};
///////////////SegmentTree/////////////////
///////////////////PnC/////////////////////
struct PnC
{
	int n;
	vl F,I;
	void init(int n)
	{
		F.resize(n+1);
		I.resize(n+1);
		F[0]=I[0]=1;
		for(int i=1;i<=n;i++)
		{
			F[i]=(F[i-1]*i)%MOD;
			I[i]=powN(F[i],MOD-2);
		}
	}
	ll nCr(int n,int r)
	{
		if(r>n) return 0;
		return (((F[n]*I[r])%MOD)*I[n-r])%MOD;
	}
};
///////////////////PnC/////////////////////
//////////////////Sieve////////////////////
vector<bool> is_prime(MAX, true);
vi MinDiv(MAX);
void Sieve()
{
	is_prime[0] = is_prime[1] = false;
	int i,j;
	for (i = 2; i*i <= MAX; i++)
	{
    	if (is_prime[i])
    	{
    		MinDiv[i]=i;
        	for (j = i * i; j <= MAX; j += i)
            {
				is_prime[j] = false;
				MinDiv[j]=i;
    		}
    	}
	}
	for(int i=2;i<=MAX;i++) if(is_prime[i]) MinDiv[i]=i;
}
//////////////////Sieve////////////////////

int main()
{
	test()
	{
		int m,d,w;
		cin>>m>>d>>w;
		int g = __gcd(d-1,w);
		//(d - 1)(x - y) = 0 MOD w;

		int div = w/g;
		// y - x = p.div

		//For every p, ans += min(d-p.div , m-p.div);

		ll ans = 0;
		if(d>m) swap(d,m);
		// d-p.div>=0 -> p<=d/div;
		ll p = d/div;
		ans = d*p - (p*(p+1)/2)*div;
		cout<<ans<<endl;

	}
}


#include "bits/stdc++.h"
#define lld long double
#define endl '\n'
#define int long long
#define MOD 1000000007
#define inf 1000000000000000000LL
#define pii pair<int,int>
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define all(v) v.begin(),v.end()
#define SpeedUp ios_base::sync_with_stdio(false);cout.tie(NULL);cin.tie(NULL);
using namespace std;
int power(int x,int y,int md=inf){int res=1;x%=md;while(y>0){if(y&1)res=(res*x)%md;x*=x;if(x>=md)x%=md;y>>=1;}return res;}

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

int32_t main() {
	SpeedUp;

	int t = 1;
	cin>>t;

	while(t--) {

		int m,d,w;
		cin>>m>>d>>w;

		int g = gcd(d-1,w);
		int x = w/g;

		int lim = min(m,d)/x;

		if(lim==0) cout<<0<<endl;

		else {
			// cout<<"#"<<x<<" "<<g<<" "<<lim<<" "<<m<<endl;
			int ans = -(lim*x*(lim+1))/2 + lim*min(d,m);
			cout<<ans<<endl;
		}

	}

}


#include "bits/stdc++.h"
#define lld long double
#define endl '\n'
#define int long long
#define MOD 1000000007
#define inf 1000000000000000000LL
#define pii pair<int,int>
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define all(v) v.begin(),v.end()
#define SpeedUp ios_base::sync_with_stdio(false);cout.tie(NULL);cin.tie(NULL);
using namespace std;
int power(int x,int y,int md=inf){int res=1;x%=md;while(y>0){if(y&1)res=(res*x)%md;x*=x;if(x>=md)x%=md;y>>=1;}return res;}

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

int32_t main() {
	SpeedUp;

	int t = 1;
	cin>>t;

	while(t--) {

		int m,d,w;
		cin>>m>>d>>w;

		int g = gcd(d-1,w);
		int x = w/g;

		int lim = min(m,d)/x;

		if(lim==0) cout<<0<<endl;

		else {
			// cout<<"#"<<x<<" "<<g<<" "<<lim<<" "<<m<<endl;
			int ans = lim*(-(x*(lim+1))/2 + min(d,m));
			cout<<ans<<endl;
		}

	}

}


#include "bits/stdc++.h"
#define lld long double
#define endl '\n'
#define int long long
#define MOD 1000000007
#define inf 1000000000000000000LL
#define pii pair<int,int>
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define all(v) v.begin(),v.end()
#define SpeedUp ios_base::sync_with_stdio(false);cout.tie(NULL);cin.tie(NULL);
using namespace std;
int power(int x,int y,int md=inf){int res=1;x%=md;while(y>0){if(y&1)res=(res*x)%md;x*=x;if(x>=md)x%=md;y>>=1;}return res;}

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

int32_t main() {
	SpeedUp;

	int t = 1;
	cin>>t;

	while(t--) {

		int m,d,w;
		cin>>m>>d>>w;

		int g = gcd(d-1,w);
		int x = w/g;

		int lim = min(m-1,d-1)/x;

		if(lim==0) cout<<0<<endl;

		else {
			// cout<<"#"<<x<<" "<<g<<" "<<lim<<" "<<m<<endl;
			int ans = -x*(lim*(lim+1))/2 + lim*min(d,m);
			cout<<ans<<endl;

		}

	}

}


#include <cstdio>

int main(){
    int T;
    scanf("%d", &T);
    while(T--){

    }
    return 0;
}


#include <cstdio>

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

int main(){
    int T;
    long long ans = 0;
    long long m, d, w;
    long long p, k, s;
    scanf("%d", &T);
    while(T--){
        scanf("%lld%lld%lld", &m, &d, &w);
        k = m > d ? d : m;
        p = gcd(d - 1, w);
        w /= p;
        s = 1;
        ans = 0;
        while(true){
            if(w * s >= k)
                break;
            ans += k - w * s;
            ++s;
        }
        printf("%lld\n", ans);
    }
    return 0;
}


#include <cstdio>

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

int main(){
    int T;
    long long ans = 0;
    long long m, d, w;
    long long p, k, s;
    scanf("%d", &T);
    while(T--){
        scanf("%lld%lld%lld", &m, &d, &w);
        k = m > d ? d : m;
        p = gcd(d - 1, w);
        w /= p;
        s = k / w;
        ans = s * k - w * s * (s + 1) / 2;
        printf("%lld\n", ans);
    }
    return 0;
}


#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<queue>
#include<map>
#include<set>
#include<cmath>
#include<vector>
typedef long long ll;
using namespace std;
#define pii pair<int,int>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#include<bits/stdc++.h>
using namespace std;
#define ll __int128
ll mod=998244353;
ll f(ll a,ll b,ll c,ll n){
	ll m=(a*n+b)/c;
	if(!m||!a)return 0;
	if(a>=c||b>=c)return (n*(n+1)/2%mod*(a/c)%mod+(b/c)*(n+1)%mod+f(a%c,b%c,c,n))%mod;
	else return (m*n%mod-f(c,c-b-1,a,m-1)+mod)%mod;
}
int main(){
	int i,t,m,d,w;
	scanf("%d",&t);
	while(t--){
		scanf("%d%d%d",&m,&d,&w);
		int limit=min(d,m);
		if(d==1){
			printf("0\n");
		}else{
			printf("%lld\n",(ll)f(1,0,w/__gcd(w,d-1),limit-1));
		}
	}
	return 0;
}


#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<queue>
#include<map>
#include<set>
#include<cmath>
#include<vector>
typedef long long ll;
using namespace std;
#define pii pair<int,int>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#include<bits/stdc++.h>
using namespace std;
ll mod=998244353;
ll f(ll a,ll b,ll c,ll n){
	ll m=(a*n+b)/c;
	if(!m||!a)return 0;
	if(a>=c||b>=c)return (n*(n+1)/2%mod*(a/c)%mod+(b/c)*(n+1)%mod+f(a%c,b%c,c,n))%mod;
	else return (m*n%mod-f(c,c-b-1,a,m-1)+mod)%mod;
}
int main(){
	int i,t,m,d,w;
	scanf("%d",&t);
	while(t--){
		scanf("%d%d%d",&m,&d,&w);
		int limit=min(d,m);
		if(d==1){
			printf("0\n");
		}else{
			printf("%lld\n",(ll)f(1,0,w/__gcd(w,d-1),limit-1));
		}
	}
	return 0;
}


#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<queue>
#include<map>
#include<set>
#include<cmath>
#include<vector>
typedef long long ll;
using namespace std;
#define pii pair<int,int>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
ll f(ll a,ll b,ll c,ll n){
	ll m=(a*n+b)/c;
	if(!m||!a)return 0;
	if(a>=c||b>=c)return n*(n+1)/2*(a/c)+(b/c)*(n+1)+f(a%c,b%c,c,n);
	else return m*n-f(c,c-b-1,a,m-1);
}
int main(){
	int i,t,m,d,w;
	scanf("%d",&t);
	while(t--){
		scanf("%d%d%d",&m,&d,&w);
		int limit=min(d,m);
		if(d==1){
			printf("0\n");
		}else{
			printf("%lld\n",(ll)f(1,0,w/__gcd(w,d-1),limit-1));
		}
	}
	return 0;
}


#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<queue>
#include<map>
#include<set>
#include<cmath>
#include<vector>
typedef long long ll;
using namespace std;
#define pii pair<int,int>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
ll mod=1991145149LL;
ll f(ll a,ll b,ll c,ll n){
	ll m=(a*n+b)/c;
	if(!m||!a)return 0;
	if(a>=c||b>=c)return (n*(n+1)/2%mod*(a/c)%mod+(b/c)*(n+1)%mod+f(a%c,b%c,c,n))%mod;
	else return (m%mod*n%mod-f(c,c-b-1,a,m-1)+mod)%mod;
}
int main(){
	int i,t,m,d,w;
	scanf("%d",&t);
	while(t--){
		scanf("%d%d%d",&m,&d,&w);
		int limit=min(d,m);
		if(d==1){
			printf("0\n");
		}else{
			printf("%lld\n",(ll)f(1,0,w/__gcd(w,d-1),limit-1));
		}
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#if DEBUG && !ONLINE_JUDGE
    #include "/home/chamanag/Desktop/cpp/debug.h"
#else
    #define debug(args...)
    #define DBG(x...)
#endif
typedef     long long int                        ll;
typedef     long double                          ld;
typedef     vector<ll>                           vi;
typedef     pair<ll,ll>                          pii;
typedef     vector<pii>                          vpi;
typedef     vector<vi>                           vvi;
typedef     map<ll,ll>                           mii;
typedef     map<char,ll>                         mci;
typedef     priority_queue<ll>                   pqi;
typedef     priority_queue<pii>                  pqii;
typedef     priority_queue<ll,vi,greater<ll>>    pqmini;
typedef     priority_queue<pii,vpi,greater<pii>> pqminpi;

#define     fi              first
#define     se              second
#define     sz(a)           (ll)((a).size())
#define     rep(i,n)        for(ll i=0;i<n;i++)
#define     repA(i,a,n)     for(auto i=a;i<=n;i++)
#define     repD(i, a, n)   for(auto i = a; i >= (n); --i)
#define     trav(a, x)      for(auto& a : x)
#define     all(x)          x.begin(), x.end()
#define     fill(a)         memset(a, 0, sizeof (a))
#define     pb              push_back
#define     MOD             1000000007
#define     PI              3.14159265359
#define     EULER           2.7182818284
#define     endl            '\n'

//////////////////////////////////////////////////////////////////////

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    // ifstream in("input.txt");
    // streambuf *cinbuf = std::cin.rdbuf(); //save old buf
    // cin.rdbuf(in.rdbuf()); //redirect std::cin to in.txt!
    // ofstream out("output.txt");
    // streambuf *coutbuf = std::cout.rdbuf(); //save old buf
    // cout.rdbuf(out.rdbuf()); //redirect std::cout to out.txt!
    cout.precision(20);
    ll tt; cin>>tt;
    while(tt--){
        ll m,d,w; cin>>m>>d>>w;

        ll diff = 1, ans = 0;
        ll range = min(m,d);
        if((d-1)%w == 0){
            cout<<(range*(range-1))/2<<endl;
            continue;
        }

        while(diff*(d-1)%w) diff++;
        DBG(diff);

        if(diff>=range){
            cout<<ans<<endl;
        }
        else{
            ll cnt = range/diff;
            DBG(cnt);
            ans += cnt*(range-diff);
            ans -= (cnt*(cnt-1)*diff)/2;
            cout<<ans<<endl;
        }

    }

    debug("\nRuntime = " + to_string((ld)clock()/(ld)CLOCKS_PER_SEC));
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#if DEBUG && !ONLINE_JUDGE
    #include "/home/chamanag/Desktop/cpp/debug.h"
#else
    #define debug(args...)
    #define DBG(x...)
#endif
typedef     long long int                        ll;
typedef     long double                          ld;
typedef     vector<ll>                           vi;
typedef     pair<ll,ll>                          pii;
typedef     vector<pii>                          vpi;
typedef     vector<vi>                           vvi;
typedef     map<ll,ll>                           mii;
typedef     map<char,ll>                         mci;
typedef     priority_queue<ll>                   pqi;
typedef     priority_queue<pii>                  pqii;
typedef     priority_queue<ll,vi,greater<ll>>    pqmini;
typedef     priority_queue<pii,vpi,greater<pii>> pqminpi;

#define     fi              first
#define     se              second
#define     sz(a)           (ll)((a).size())
#define     rep(i,n)        for(ll i=0;i<n;i++)
#define     repA(i,a,n)     for(auto i=a;i<=n;i++)
#define     repD(i, a, n)   for(auto i = a; i >= (n); --i)
#define     trav(a, x)      for(auto& a : x)
#define     all(x)          x.begin(), x.end()
#define     fill(a)         memset(a, 0, sizeof (a))
#define     pb              push_back
#define     MOD             1000000007
#define     PI              3.14159265359
#define     EULER           2.7182818284
#define     endl            '\n'

//////////////////////////////////////////////////////////////////////

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    // ifstream in("input.txt");
    // streambuf *cinbuf = std::cin.rdbuf(); //save old buf
    // cin.rdbuf(in.rdbuf()); //redirect std::cin to in.txt!
    // ofstream out("output.txt");
    // streambuf *coutbuf = std::cout.rdbuf(); //save old buf
    // cout.rdbuf(out.rdbuf()); //redirect std::cout to out.txt!
    cout.precision(20);
    ll tt; cin>>tt;
    while(tt--){
        ll m,d,w; cin>>m>>d>>w;

        ll diff = 1, ans = 0;
        ll range = min(m,d);
        if((d-1)%w == 0){
            cout<<(range*(range-1))/2<<endl;
            continue;
        }

        // while(diff*(d-1)%w) diff++;
        diff = (w*(d-1)/__gcd(w,d-1))/(d-1);
        DBG(diff);

        if(diff>=range){
            cout<<ans<<endl;
        }
        else{
            ll cnt = range/diff;
            DBG(cnt);
            ans += cnt*(range-diff);
            ans -= (cnt*(cnt-1)*diff)/2;
            cout<<ans<<endl;
        }

    }

    debug("\nRuntime = " + to_string((ld)clock()/(ld)CLOCKS_PER_SEC));
    return 0;
}


/*
K.D. Vinit
*/
#include <iostream>
#include <algorithm>
using namespace std;

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

void solve()
{
    long long int m,d,w,x,y;
    cin>>m>>d>>w;
    y=min(d,m);
    d--;
    x=gcd(d,w);
    w/=x;

    long long int ans=0;
    for(long long int i=1;i<=y;i++)
    {
        ans+=((y-i)/w);
    }

    cout<<ans<<endl;
}

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


/*
K.D. Vinit
*/
#include <iostream>
#include <algorithm>
using namespace std;

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

void solve()
{
    long long int m,d,w,x,y;
    cin>>m>>d>>w;
    y=min(d,m);
    m=y;
    d--;
    x=gcd(d,w);
    w/=x;

    y--;
    long long int ans=0;
    y/=w;
    x=y;
    y=((y*(y+1))/2);
    y*=w;
    ans+=y;

    m--;
    m%=w;
    m=(w-1-m);
    m*=x;
    ans-=m;

    cout<<ans<<endl;
}

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


/*
K.D. Vinit
*/
#include <iostream>
#include <algorithm>
using namespace std;

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

void solve()
{
    long long int m,d,w,x,y;
    cin>>m>>d>>w;
    y=min(d,m);
    m=y;
    d--;
    x=gcd(d,w);
    w/=x;

    long long int ans=0;
    for(long long int i=1;i<=m;i++)
    {
        y--;
        if((y+1)%w==0) break;
        ans+=(y/w);
    }
    y/=w;
    y=((y*(y+1))/2);
    y*=w;
    ans+=y;

    cout<<ans<<endl;
}

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


///Astalavista Code........~CodeSlayer007
#include <bits/stdc++.h>
using namespace std;
#define endl "\n"
#define Br cout<<endl
#define int long long
#define fe first
#define se second
#define double long double
#define all(v) v.begin(),v.end()
#define allr(v) v.rbegin(),v.rend()
#define PT(x) cout << (x) << endl
#define PTT(x) cout << (#x) << " is " << (x) << endl
#define lets_go ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
typedef vector<int> vi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
const int M = 998244353;
const int N = 2e5 + 10;
const int IM = 1e15 + 37;
const double PI = 3.1415926535897932384;
inline void PP(int n) { cout << n << " ";}
void PV(vi v) { for (int i = 0 ; i < v.size() ; i++) cout << v[i] << " " ; cout << endl;}
void PVV(vii v) {for (int i = 0 ; i < v.size() ; i++) cout << v[i].first << " " << v[i].second << endl;}
void PA(int v[], int n, int x = 0) { for (int i = x ; i < n + x; i++)cout << v[i] << ' ' ; cout << endl;}
void IN(int a[], int n, int x = 0) {for (int i = x; i < n + x; i++)cin >> a[i];}
inline void op() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
}
// <=================================================================================================================================
// <=================================================================================================================================

signed  main() {
    op();
    lets_go
    int t;
    cin >> t;
    while (t--) {
        int m, d, w;
        cin >> m >> d >> w;
        m = min(m, d);
        w = w / __gcd(w, d - 1);
        int k = m / w;
        int r = m % w;
        int x = w - r;
        int ans = (x * (k - 1) * (k) / 2) + (r * (k + 1) * k / 2);
        PT(ans);

    }
    return 0;

}


///Astalavista Code........~CodeSlayer007
#include <bits/stdc++.h>
using namespace std;
#define endl "\n"
#define Br cout<<endl
#define int long long
#define fe first
#define se second
#define double long double
#define all(v) v.begin(),v.end()
#define allr(v) v.rbegin(),v.rend()
#define PT(x) cout << (x) << endl
#define PTT(x) cout << (#x) << " is " << (x) << endl
#define lets_go ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
typedef vector<int> vi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
const int M = 998244353;
const int N = 2e5 + 10;
const int IM = 1e15 + 37;
const double PI = 3.1415926535897932384;
inline void PP(int n) { cout << n << " ";}
void PV(vi v) { for (int i = 0 ; i < v.size() ; i++) cout << v[i] << " " ; cout << endl;}
void PVV(vii v) {for (int i = 0 ; i < v.size() ; i++) cout << v[i].first << " " << v[i].second << endl;}
void PA(int v[], int n, int x = 0) { for (int i = x ; i < n + x; i++)cout << v[i] << ' ' ; cout << endl;}
void IN(int a[], int n, int x = 0) {for (int i = x; i < n + x; i++)cin >> a[i];}
inline void op() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
}
// <=================================================================================================================================
// <=================================================================================================================================

signed  main() {
    op();
    lets_go
    int t;
    cin >> t;
    while (t--) {
        int m, d, w;
        cin >> m >> d >> w;
        m = min(m, d);
        w = w / __gcd(w, d - 1);
        int k = m / w;
        int r = m % w;
        int ans = r * k + w * (k - 1) * k / 2;
        PT(ans);

    }

    return 0;

}


#include <bits/stdc++.h>
#define ull unsigned long long int
#define ll long long int
#define LL_MAX 9223372036854775807
#define pb push_back
#define pf push_front
#define mp make_pair
#define popb pop_back
#define vl vector<ll>
#define bs(v, x) binary_search(v.begin(), v.end(), x)
#define popf pop_front
#define p() cout << '\n'
#define p0(x) cout << x << " "
#define p1(x) cout << x << '\n'
#define p2(x, y) cout << x << " " << y << '\n'
#define p3(x, y, z) cout << x << " " << y << " " << z << '\n'
#define printv(v)                   \
  for (ll i = 0; i < v.size(); ++i) \
    cout << v[i] << " ";            \
  cout << '\n'
#define pr1(x) cout << fixed << setprecision(15) << x << '\n'
#define mod 1000000007
#define mod1 998244353
#define fio                         \
  ios_base::sync_with_stdio(false); \
  cin.tie(NULL)
#define get(n) \
  ll n;        \
  cin >> n
#define getvec(v, n)         \
  vector<ll> v(n);           \
  for (ll i = 0; i < n; i++) \
    cin >> v[i];
#define getstr(s) \
  string s;       \
  cin >> s
#define all(x) x.begin(), x.end()
#define countBits(x) __builtin_popcount(x)
using namespace std;

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

void mainSolve()
{
  get(m);
  get(d);
  get(w);
  ll n = min(m, d);
  --d;
  if (d == 0)
  {
    p1(0);
    return;
  }
  ll g = gcd(w, d);
  w = w / g;
  ll k = n / w;
  ll r = n % w;
  ll ans = (((k * (k - 1)) / 2ll) * w) + r * k;
  p1(ans);
}

int main()
{
  fio;
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  get(t);
  // ll t = 1;
  while (t--)
  {
    mainSolve();
  }
  return 0;
}


#include <bits/stdc++.h>
#define ull unsigned long long int
#define ll long long int
#define LL_MAX 9223372036854775807
#define pb push_back
#define pf push_front
#define mp make_pair
#define popb pop_back
#define vl vector<ll>
#define bs(v, x) binary_search(v.begin(), v.end(), x)
#define popf pop_front
#define p() cout << '\n'
#define p0(x) cout << x << " "
#define p1(x) cout << x << '\n'
#define p2(x, y) cout << x << " " << y << '\n'
#define p3(x, y, z) cout << x << " " << y << " " << z << '\n'
#define printv(v)                   \
  for (ll i = 0; i < v.size(); ++i) \
    cout << v[i] << " ";            \
  cout << '\n'
#define pr1(x) cout << fixed << setprecision(15) << x << '\n'
#define mod 1000000007
#define mod1 998244353
#define fio                         \
  ios_base::sync_with_stdio(false); \
  cin.tie(NULL)
#define get(n) \
  ll n;        \
  cin >> n
#define getvec(v, n)         \
  vector<ll> v(n);           \
  for (ll i = 0; i < n; i++) \
    cin >> v[i];
#define getstr(s) \
  string s;       \
  cin >> s
#define all(x) x.begin(), x.end()
#define countBits(x) __builtin_popcount(x)
using namespace std;

void mainSolve()
{
  get(m);
  get(d);
  get(w);
  ll n = min(m, d);
  d = (d - 1ll) % w;
  if (d == 0)
  {
    ll ans = ((n - 1ll) * n) / 2ll;
    p1(ans);
    return;
  }
  ll ans = 0;
  for (int i = 1; i <= n - 1ll; i++)
  {
    if ((i * d) % w == 0)
    {
      ans += (n - i);
    }
  }
  p1(ans);
}

int main()
{
  fio;
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  get(t);
  // ll t = 1;
  while (t--)
  {
    mainSolve();
  }
  return 0;
}


#include <bits/stdc++.h>
#define ull unsigned long long int
#define ll long long int
#define LL_MAX 9223372036854775807
#define pb push_back
#define pf push_front
#define mp make_pair
#define popb pop_back
#define vl vector<ll>
#define bs(v, x) binary_search(v.begin(), v.end(), x)
#define popf pop_front
#define p() cout << '\n'
#define p0(x) cout << x << " "
#define p1(x) cout << x << '\n'
#define p2(x, y) cout << x << " " << y << '\n'
#define p3(x, y, z) cout << x << " " << y << " " << z << '\n'
#define printv(v)                   \
  for (ll i = 0; i < v.size(); ++i) \
    cout << v[i] << " ";            \
  cout << '\n'
#define pr1(x) cout << fixed << setprecision(15) << x << '\n'
#define mod 1000000007
#define mod1 998244353
#define fio                         \
  ios_base::sync_with_stdio(false); \
  cin.tie(NULL)
#define get(n) \
  ll n;        \
  cin >> n
#define getvec(v, n)         \
  vector<ll> v(n);           \
  for (ll i = 0; i < n; i++) \
    cin >> v[i];
#define getstr(s) \
  string s;       \
  cin >> s
#define all(x) x.begin(), x.end()
#define countBits(x) __builtin_popcount(x)
using namespace std;

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

void mainSolve()
{
  get(m);
  get(d);
  get(w);
  ll n = min(m, d);
  --d;
  if (d == 0)
  {
    p1(0);
    return;
  }
  ll ans = 0;
  ll g = gcd(w, d);
  w = w / g;
  for (int i = w; i <= n - 1ll; i += w)
  {
    if ((i) % w == 0)
    {
      ans += (n - i);
    }
  }
  p1(ans);
}

int main()
{
  fio;
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  get(t);
  // ll t = 1;
  while (t--)
  {
    mainSolve();
  }
  return 0;
}


#include<bits/stdc++.h>
#define rep(i,x,y) for(auto i=(x);i<=(y);++i)
#define dep(i,x,y) for(auto i=(x);i>=(y);--i)
#define fr first
#define sc second
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,ll>pil;
typedef pair<int,int>pii;
typedef pair<ll,ll>pll;
const int N=1e6+10;
const int inf=1e9;
const int mo=998244353;
ll gcd(ll x,ll y){return x%y?gcd(y,x%y):y; }
int main(){

	//随手写的刚调过样例就给我过了这也太假了吧
	//又错过了什么

    //freopen("D:\\in.txt","r",stdin);
    //freopen("D:\\out.txt","w",stdout);

	int t; cin>>t;
	while(t--){
		ll m,d,w; cin>>m>>d>>w;
		ll base=w/gcd(d-1,w);
		ll lim=min(d,m);
		ll r=(lim-1)/base;
		ll ans=lim*r-base*(1+r)*r/2;
		//cout<<lim<<" "<<r<<" "<<base<<endl;
		cout<<ans<<endl;
	}
    return 0;
}


#include<bits/stdc++.h>
#define rep(i,x,y) for(auto i=(x);i<=(y);++i)
#define dep(i,x,y) for(auto i=(x);i>=(y);--i)
#define fr first
#define sc second
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,ll>pil;
typedef pair<int,int>pii;
typedef pair<ll,ll>pll;
const int N=1e6+10;
const int inf=1e9;
const int mo=998244353;
ll gcd(ll x,ll y){return x%y?gcd(y,x%y):y; }
int main(){
    //freopen("D:\\in.txt","r",stdin);
    //freopen("D:\\out.txt","w",stdout);
	int t; cin>>t;
	while(t--){
		ll m,d,w; cin>>m>>d>>w;
		ll base=w/gcd(d-1,w);
		ll lim=min(d,m);
		ll r=lim/base;
		ll ans=lim*r-base*(1+r)*r/2;
		//cout<<lim<<" "<<r<<" "<<base<<endl;
		cout<<ans<<endl;
	}
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define sd(x) scanf("%d",&x)
#define slld(x) scanf("%lld",&x)
#define ss(x) scanf("%s",x)
#define mod 1000000007
#define bitcount __builtin_popcountll
#define ll long long
#define pb push_back
#define pi pair<int,int>
#define pii pair<pi,int>
#define mp make_pair
int main () {
	ll t;
	slld(t);
	while (t--) {
		ll m, d, w;
		slld(m);
		slld(d);
		slld(w);
		if (d == 1 || m == 1) printf("0\n");
		else {
			w /= __gcd(d - 1, w);
			m = min(m, d);
			ll x = m % w;
			ll y = w - x;
			ll cnt1 = m / w + 1;
			ll cnt2 = m / w;
			ll val1 = ((cnt1 * (cnt1 - 1)) / 2) * x;
			ll val2 = ((cnt2 * (cnt2 - 1)) / 2) * y;
			printf("%lld\n", val1 + val2);
		}
	}
}


#include<bits/stdc++.h>
using namespace std;
#define sd(x) scanf("%d",&x)
#define slld(x) scanf("%lld",&x)
#define ss(x) scanf("%s",x)
#define mod 1000000007
#define bitcount __builtin_popcountll
#define ll long long
#define pb push_back
#define pi pair<int,int>
#define pii pair<pi,int>
#define mp make_pair
int main () {
	ll t;
	slld(t);
	while (t--) {
		ll m, d, w;
		slld(m);
		slld(d);
		slld(w);
		if (d == 1 || m == 1 || w == 1) printf("0\n");
		else {
			w /= __gcd(d - 1, w);
			m = min(m, d);
			ll x = m % w;
			ll y = w - x;
			ll cnt1 = m / w + 1;
			ll cnt2 = m / w;
			ll val1 = ((cnt1 * (cnt1 - 1)) / 2) * x;
			ll val2 = ((cnt2 * (cnt2 - 1)) / 2) * y;
			printf("%lld\n", val1 + val2);
		}
	}
}


#include <bits/stdc++.h>
#include<string.h>
#include<string>

using namespace std;

typedef long long int lli;

#define fio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define rep(i,a,b) for(lli i=a;i<b;i++)
#define rev(i,b,a) for(lli i=b;i>=a;i--)
#define FOREACH(i, x) for(__typeof((x).begin()) i = (x).begin(); i != (x).end(); i ++)
#define debug(x) cout << "\t[ " << #x  << " : " << x << " ]" << endl;
#define debugT(x) cout << "\t" << #x  << " : " << x << endl;
#define MOD (lli)(1000000000+7) //10^9+7
#define MAXN 1000000+1 //10^6+1
#define endl "\n"
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define TEST_CASES  lli t;cin >> t;while(t--)

lli max(lli a, lli b){ if(a>b) return a ; return b; }
lli min(lli a, lli b){ if(a<b) return a ; return b; }

string sing(char x)
{
	string s(1,x) ;
	return s;
}

bool cmpDec(lli a,lli b)
{
	if(a>=b)
	return true;
return false;
}

void print1D(lli a[], lli n)
{
	rep(i,0,n)cout<<a[i]<<" ";
	cout<<endl;
}

void print2D(lli **dp, lli n, lli m)
{
	rep(i,0,n)
	{
		rep(j,0,m)
		cout<<dp[i][j]<<" ";
		cout<<endl;
	}
}

void init1D(lli a[], lli n, lli k)
{
	rep(z,0,n)a[z]=k;
}

void init2D(lli **a, lli n, lli m, lli k)
{
	rep(i,0,n)
	rep(j,0,m)
	a[i][j]=k;
}

lli fastmod(lli base,lli exp, lli m)
{
	lli res=1;
	while(exp)
	{
		if (exp&1ll)
			res=(res*base)%m;
		base=(base*base)%m;
		exp/=2;
	}
	return res;
}

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

lli logBase(lli m, lli b)
{
	lli count=0;
	while(m>1)
	{
		++count;
		m=m/b;
	}
	return count;
}

// stores smallest prime factor for every number
lli spf[MAXN];

// Calculating SPF (Smallest Prime Factor) for every
// number till MAXN.
// Time Complexity : O(nloglogn)
void sieve()
{
	spf[1] = 1;
	for (lli i=2; i<MAXN; i++)

		// marking smallest prime factor for every
		// number to be itself.
		spf[i] = i;

	// separately marking spf for every even
	// number as 2
	for (lli i=4; i<MAXN; i+=2)
		spf[i] = 2;

	for (lli i=3; i*i<MAXN; i++)
	{
		// checking if i is prime
		if (spf[i] == i)
		{
			// marking SPF for all numbers divisible by i
			for (lli j=i*i; j<MAXN; j+=i)

				// marking spf[j] if it is not
				// previously marked
				if (spf[j]==j)
					spf[j] = i;
		}
	}
}

// A O(log n) function returning primefactorization
// by dividing by smallest prime factor at every step
vector<lli> getFactorization(lli x)
{
	vector<lli> ret;
	while (x != 1)
	{
		ret.push_back(spf[x]);
		x = x / spf[x];
	}
	return ret;
}

// Returns sum of all factors of n.
lli sumofFactors(lli n)
{
	// Traversing through all prime factors.
	lli res = 1;
	for (lli i = 2; i <= sqrt(n); i++)
	{

		lli curr_sum = 1;
		lli curr_term = 1;
		while (n % i == 0) {

			// THE BELOW STATEMENT MAKES
			// IT BETTER THAN ABOVE METHOD
			//  AS WE REDUCE VALUE OF n.
			n = n / i;

			curr_term *= i;
			curr_sum += curr_term;
		}

		res *= curr_sum;
	}

	// This condition is to handle
	// the case when n is a prime
	// number greater than 2.
	if (n >= 2)
		res *= (1 + n);

	return res;
}

void SUM(lli *a, lli n)
{
	lli sum=0;
	rep(i,0,n)sum+=a[i];
	cout<<sum<<endl;
}

void addEdge(vector<lli> adj[], lli u, lli v)
{
	adj[u].push_back(v);
	adj[v].push_back(u);
}

void printGraph(vector<lli> adj[], lli V)
{
	for (lli v = 1; v < V; ++v)
	{
		cout << "\n Adjacency list of vertex "
			 << v << "\n head ("<<v<<")";
		rep(x,0,adj[v].size())
		   cout << " -> " << adj[v][x];
		printf("\n");
	}
}

bool cycle(vector<lli> adj[], lli vis[], lli n, lli parent)
{
	vis[n]=1;
	if(adj[n].empty())return false;
	bool value=false;
	rep(i,0,adj[n].size())
	{
		if(adj[n][i]!=parent)
		{
			if(vis[adj[n][i]]==1)return true;
			else
				value|=cycle(adj,vis,adj[n][i],n);
		}
	}
	return value;
}

typedef struct num{
	lli index;
	lli value;
}num;

lli countFactors(lli n)
{
	lli count=0;
	lli ans=1;
	rep(i,2,sqrt(n)+1)
	{
		count=0;
		while(n%i==0)
		{
			n/=i;
			++count;
		}
		ans*=count;
	}
	if(n>1)ans*=2;
	return ans;
}

lli kthFactor(lli n, lli k)
{
	vector <lli> v;
	rep(i,2,(lli)sqrt(n)+1)
	{
		if(n%i==0)
		{
			v.pb(i);
			if(i<n/i) v.pb(n/i);
		}
	}
	v.pb(1);
	v.pb(n);
	sort(v.begin(),v.end());
	// print1D(v,v.size());
	if(v.size()<k)return -1;
	return v[k-1];

}

lli binSmaller(lli a[], lli n, lli k)
{
	lli l,r;
	l=0;
	r=n-1;
	lli mid=(l+r)/2;
	if(a[r]<=k)return r;
	if(a[l]>k)return -1;
	while(l<r)
	{
		if(a[mid]>k && a[mid-1]<=k)return mid-1;
		if(k>=a[mid] && a[mid+1]>k)return mid;
		if(k<a[mid])r=mid-1;
		else l=mid+1;
		mid=(l+r)/2;

	}
	return -1;
}

//returns count of numbers smaller than or equal to key
lli binarySearchCount(lli arr[], lli n, lli key)
{
	lli left = 0;
	lli right = n - 1;

	lli count = 0;
	lli mid=0;
	while (left <= right) {
		mid = (right + left) / 2;

		// Check if middle element is
		// less than or equal to key
		if (arr[mid] <= key) {

			// At least (mid + 1) elements are there
			// whose values are less than
			// or equal to key
			count = mid + 1;
			left = mid + 1;
		}

		// If key is smaller, ignore right half
		else
			right = mid - 1;
	}

	return count;
}

lli binomialCoeff(lli n, lli k)
{
	lli res = 1;

	// Since C(n, k) = C(n, n-k)
	if ( k > n - k )
		k = n - k;

	// Calculate value of
	// [n * (n-1) *---* (n-k+1)] / [k * (k-1) *----* 1]
	for (lli i = 0; i < k; ++i)
	{
		res *= (n - i);
		res /= (i + 1);
	}

	return res;
}

lli modInverse(lli a, lli m= MOD )
{
	lli m0 = m , y = 0, x = 1; if (m == 1) return 0;
	while (a > 1)
	{
		lli q = a / m;
		lli t = m;
		m = a % m, a = t;
		t = y;
		y = x - q * y;
		x = t;
	}
	if (x < 0)  x += m0; return x;
}

 // ncr in o(1)
const int N = 10e5+1;
lli factorialNumInverse[N + 1];  lli naturalNumInverse[N + 1]; lli fact[N + 1];

void InverseofNumber(lli p)
{
	naturalNumInverse[0] = naturalNumInverse[1] = 1;
	for (lli i = 2; i <= N; i++)
		naturalNumInverse[i] = naturalNumInverse[p % i] * (p - p / i) % p;
}

void InverseofFactorial(lli p)
{
	factorialNumInverse[0] = factorialNumInverse[1] = 1;
	for (lli i = 2; i <= N; i++)
		factorialNumInverse[i] = (naturalNumInverse[i] * factorialNumInverse[i - 1]) % p;
}

void factorial(lli p)
{
	fact[0] = 1;
	for (lli i = 1; i <= N; i++)
	{
		fact[i] = (fact[i - 1] * i) % p;
	}
}

lli nCr(lli N, lli R, lli p = MOD)
{
	if(R==0) return 1;
	lli ans = ((fact[N] * factorialNumInverse[R])  % p * factorialNumInverse[N - R]) % p;
	return ans;
}
//*/

lli numberOfPaths(lli m, lli n)
{
	// We have to calculate m+n-2 C n-1 here
	// which will be (m+n-2)! / (n-1)! (m-1)!
	lli path = 1;
	for (lli i = n; i < (m + n - 1); i++) {
		path *= i;
		path /= (i - n + 1);
	}
	return path;
}

int main()
{
	fio;
	// #ifndef ONLINE_JUDGE
	// 	freopen("alchemy_input.txt", "r", stdin);
	// 	freopen("alchemy_output.txt", "w", stdout);
	// #endif
	TEST_CASES
	{
		lli m,d,w;
		cin>>m>>d>>w;
		m=min(m,d);
		if((d-1)%w==0)cout<<(m*(m-1))/2<<endl;
		else
		{
			w/=gcd(d-1,w);
			// lli left=d;
			m=min(d,m);
			lli q=0/*m-m%w*/;
			q=(m)/w;
			lli ans=(m)*q-((q*(q+1))/2)*w;

			cout<<ans<<endl;

	}

}


#include <bits/stdc++.h>
#include<string.h>
#include<string>

using namespace std;

typedef long long int lli;

#define fio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define rep(i,a,b) for(lli i=a;i<b;i++)
#define rev(i,b,a) for(lli i=b;i>=a;i--)
#define FOREACH(i, x) for(__typeof((x).begin()) i = (x).begin(); i != (x).end(); i ++)
#define debug(x) cout << "\t[ " << #x  << " : " << x << " ]" << endl;
#define debugT(x) cout << "\t" << #x  << " : " << x << endl;
#define MOD (lli)(1000000000+7) //10^9+7
#define MAXN 1000000+1 //10^6+1
#define endl "\n"
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define TEST_CASES  lli t;cin >> t;while(t--)

lli max(lli a, lli b){ if(a>b) return a ; return b; }
lli min(lli a, lli b){ if(a<b) return a ; return b; }

string sing(char x)
{
	string s(1,x) ;
	return s;
}

bool cmpDec(lli a,lli b)
{
	if(a>=b)
	return true;
return false;
}

void print1D(lli a[], lli n)
{
	rep(i,0,n)cout<<a[i]<<" ";
	cout<<endl;
}

void print2D(lli **dp, lli n, lli m)
{
	rep(i,0,n)
	{
		rep(j,0,m)
		cout<<dp[i][j]<<" ";
		cout<<endl;
	}
}

void init1D(lli a[], lli n, lli k)
{
	rep(z,0,n)a[z]=k;
}

void init2D(lli **a, lli n, lli m, lli k)
{
	rep(i,0,n)
	rep(j,0,m)
	a[i][j]=k;
}

lli fastmod(lli base,lli exp, lli m)
{
	lli res=1;
	while(exp)
	{
		if (exp&1ll)
			res=(res*base)%m;
		base=(base*base)%m;
		exp/=2;
	}
	return res;
}

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

lli logBase(lli m, lli b)
{
	lli count=0;
	while(m>1)
	{
		++count;
		m=m/b;
	}
	return count;
}

// stores smallest prime factor for every number
lli spf[MAXN];

// Calculating SPF (Smallest Prime Factor) for every
// number till MAXN.
// Time Complexity : O(nloglogn)
void sieve()
{
	spf[1] = 1;
	for (lli i=2; i<MAXN; i++)

		// marking smallest prime factor for every
		// number to be itself.
		spf[i] = i;

	// separately marking spf for every even
	// number as 2
	for (lli i=4; i<MAXN; i+=2)
		spf[i] = 2;

	for (lli i=3; i*i<MAXN; i++)
	{
		// checking if i is prime
		if (spf[i] == i)
		{
			// marking SPF for all numbers divisible by i
			for (lli j=i*i; j<MAXN; j+=i)

				// marking spf[j] if it is not
				// previously marked
				if (spf[j]==j)
					spf[j] = i;
		}
	}
}

// A O(log n) function returning primefactorization
// by dividing by smallest prime factor at every step
vector<lli> getFactorization(lli x)
{
	vector<lli> ret;
	while (x != 1)
	{
		ret.push_back(spf[x]);
		x = x / spf[x];
	}
	return ret;
}

// Returns sum of all factors of n.
lli sumofFactors(lli n)
{
	// Traversing through all prime factors.
	lli res = 1;
	for (lli i = 2; i <= sqrt(n); i++)
	{

		lli curr_sum = 1;
		lli curr_term = 1;
		while (n % i == 0) {

			// THE BELOW STATEMENT MAKES
			// IT BETTER THAN ABOVE METHOD
			//  AS WE REDUCE VALUE OF n.
			n = n / i;

			curr_term *= i;
			curr_sum += curr_term;
		}

		res *= curr_sum;
	}

	// This condition is to handle
	// the case when n is a prime
	// number greater than 2.
	if (n >= 2)
		res *= (1 + n);

	return res;
}

void SUM(lli *a, lli n)
{
	lli sum=0;
	rep(i,0,n)sum+=a[i];
	cout<<sum<<endl;
}

void addEdge(vector<lli> adj[], lli u, lli v)
{
	adj[u].push_back(v);
	adj[v].push_back(u);
}

void printGraph(vector<lli> adj[], lli V)
{
	for (lli v = 1; v < V; ++v)
	{
		cout << "\n Adjacency list of vertex "
			 << v << "\n head ("<<v<<")";
		rep(x,0,adj[v].size())
		   cout << " -> " << adj[v][x];
		printf("\n");
	}
}

bool cycle(vector<lli> adj[], lli vis[], lli n, lli parent)
{
	vis[n]=1;
	if(adj[n].empty())return false;
	bool value=false;
	rep(i,0,adj[n].size())
	{
		if(adj[n][i]!=parent)
		{
			if(vis[adj[n][i]]==1)return true;
			else
				value|=cycle(adj,vis,adj[n][i],n);
		}
	}
	return value;
}

typedef struct num{
	lli index;
	lli value;
}num;

lli countFactors(lli n)
{
	lli count=0;
	lli ans=1;
	rep(i,2,sqrt(n)+1)
	{
		count=0;
		while(n%i==0)
		{
			n/=i;
			++count;
		}
		ans*=count;
	}
	if(n>1)ans*=2;
	return ans;
}

lli kthFactor(lli n, lli k)
{
	vector <lli> v;
	rep(i,2,(lli)sqrt(n)+1)
	{
		if(n%i==0)
		{
			v.pb(i);
			if(i<n/i) v.pb(n/i);
		}
	}
	v.pb(1);
	v.pb(n);
	sort(v.begin(),v.end());
	// print1D(v,v.size());
	if(v.size()<k)return -1;
	return v[k-1];

}

lli binSmaller(lli a[], lli n, lli k)
{
	lli l,r;
	l=0;
	r=n-1;
	lli mid=(l+r)/2;
	if(a[r]<=k)return r;
	if(a[l]>k)return -1;
	while(l<r)
	{
		if(a[mid]>k && a[mid-1]<=k)return mid-1;
		if(k>=a[mid] && a[mid+1]>k)return mid;
		if(k<a[mid])r=mid-1;
		else l=mid+1;
		mid=(l+r)/2;

	}
	return -1;
}

//returns count of numbers smaller than or equal to key
lli binarySearchCount(lli arr[], lli n, lli key)
{
	lli left = 0;
	lli right = n - 1;

	lli count = 0;
	lli mid=0;
	while (left <= right) {
		mid = (right + left) / 2;

		// Check if middle element is
		// less than or equal to key
		if (arr[mid] <= key) {

			// At least (mid + 1) elements are there
			// whose values are less than
			// or equal to key
			count = mid + 1;
			left = mid + 1;
		}

		// If key is smaller, ignore right half
		else
			right = mid - 1;
	}

	return count;
}

lli binomialCoeff(lli n, lli k)
{
	lli res = 1;

	// Since C(n, k) = C(n, n-k)
	if ( k > n - k )
		k = n - k;

	// Calculate value of
	// [n * (n-1) *---* (n-k+1)] / [k * (k-1) *----* 1]
	for (lli i = 0; i < k; ++i)
	{
		res *= (n - i);
		res /= (i + 1);
	}

	return res;
}

lli modInverse(lli a, lli m= MOD )
{
	lli m0 = m , y = 0, x = 1; if (m == 1) return 0;
	while (a > 1)
	{
		lli q = a / m;
		lli t = m;
		m = a % m, a = t;
		t = y;
		y = x - q * y;
		x = t;
	}
	if (x < 0)  x += m0; return x;
}

 // ncr in o(1)
const int N = 10e5+1;
lli factorialNumInverse[N + 1];  lli naturalNumInverse[N + 1]; lli fact[N + 1];

void InverseofNumber(lli p)
{
	naturalNumInverse[0] = naturalNumInverse[1] = 1;
	for (lli i = 2; i <= N; i++)
		naturalNumInverse[i] = naturalNumInverse[p % i] * (p - p / i) % p;
}

void InverseofFactorial(lli p)
{
	factorialNumInverse[0] = factorialNumInverse[1] = 1;
	for (lli i = 2; i <= N; i++)
		factorialNumInverse[i] = (naturalNumInverse[i] * factorialNumInverse[i - 1]) % p;
}

void factorial(lli p)
{
	fact[0] = 1;
	for (lli i = 1; i <= N; i++)
	{
		fact[i] = (fact[i - 1] * i) % p;
	}
}

lli nCr(lli N, lli R, lli p = MOD)
{
	if(R==0) return 1;
	lli ans = ((fact[N] * factorialNumInverse[R])  % p * factorialNumInverse[N - R]) % p;
	return ans;
}
//*/

lli numberOfPaths(lli m, lli n)
{
	// We have to calculate m+n-2 C n-1 here
	// which will be (m+n-2)! / (n-1)! (m-1)!
	lli path = 1;
	for (lli i = n; i < (m + n - 1); i++) {
		path *= i;
		path /= (i - n + 1);
	}
	return path;
}

int main()
{
	fio;
	// #ifndef ONLINE_JUDGE
	// 	freopen("alchemy_input.txt", "r", stdin);
	// 	freopen("alchemy_output.txt", "w", stdout);
	// #endif
	TEST_CASES
	{
		lli m,d,w;
		cin>>m>>d>>w;
		if((d-1)%w==0)cout<<(m*(m-1))/2<<endl;
		else
		{
			w/=gcd(d-1,w);
			m=min(d,m);
			lli q=(m)/w;
			lli ans=(m)*q-((q*(q+1))/2)*w;
			cout<<ans<<endl;
		}
	}

}


#include <bits/stdc++.h>
#include<string.h>
#include<string>

using namespace std;

typedef long long int lli;

#define fio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define rep(i,a,b) for(lli i=a;i<b;i++)
#define rev(i,b,a) for(lli i=b;i>=a;i--)
#define FOREACH(i, x) for(__typeof((x).begin()) i = (x).begin(); i != (x).end(); i ++)
#define debug(x) cout << "\t[ " << #x  << " : " << x << " ]" << endl;
#define debugT(x) cout << "\t" << #x  << " : " << x << endl;
#define MOD (lli)(1000000000+7) //10^9+7
#define MAXN 1000000+1 //10^6+1
#define endl "\n"
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define TEST_CASES  lli t;cin >> t;while(t--)

lli max(lli a, lli b){ if(a>b) return a ; return b; }
lli min(lli a, lli b){ if(a<b) return a ; return b; }

string sing(char x)
{
	string s(1,x) ;
	return s;
}

bool cmpDec(lli a,lli b)
{
	if(a>=b)
	return true;
return false;
}

void print1D(lli a[], lli n)
{
	rep(i,0,n)cout<<a[i]<<" ";
	cout<<endl;
}

void print2D(lli **dp, lli n, lli m)
{
	rep(i,0,n)
	{
		rep(j,0,m)
		cout<<dp[i][j]<<" ";
		cout<<endl;
	}
}

void init1D(lli a[], lli n, lli k)
{
	rep(z,0,n)a[z]=k;
}

void init2D(lli **a, lli n, lli m, lli k)
{
	rep(i,0,n)
	rep(j,0,m)
	a[i][j]=k;
}

lli fastmod(lli base,lli exp, lli m)
{
	lli res=1;
	while(exp)
	{
		if (exp&1ll)
			res=(res*base)%m;
		base=(base*base)%m;
		exp/=2;
	}
	return res;
}

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

lli logBase(lli m, lli b)
{
	lli count=0;
	while(m>1)
	{
		++count;
		m=m/b;
	}
	return count;
}

// stores smallest prime factor for every number
lli spf[MAXN];

// Calculating SPF (Smallest Prime Factor) for every
// number till MAXN.
// Time Complexity : O(nloglogn)
void sieve()
{
	spf[1] = 1;
	for (lli i=2; i<MAXN; i++)

		// marking smallest prime factor for every
		// number to be itself.
		spf[i] = i;

	// separately marking spf for every even
	// number as 2
	for (lli i=4; i<MAXN; i+=2)
		spf[i] = 2;

	for (lli i=3; i*i<MAXN; i++)
	{
		// checking if i is prime
		if (spf[i] == i)
		{
			// marking SPF for all numbers divisible by i
			for (lli j=i*i; j<MAXN; j+=i)

				// marking spf[j] if it is not
				// previously marked
				if (spf[j]==j)
					spf[j] = i;
		}
	}
}

// A O(log n) function returning primefactorization
// by dividing by smallest prime factor at every step
vector<lli> getFactorization(lli x)
{
	vector<lli> ret;
	while (x != 1)
	{
		ret.push_back(spf[x]);
		x = x / spf[x];
	}
	return ret;
}

// Returns sum of all factors of n.
lli sumofFactors(lli n)
{
	// Traversing through all prime factors.
	lli res = 1;
	for (lli i = 2; i <= sqrt(n); i++)
	{

		lli curr_sum = 1;
		lli curr_term = 1;
		while (n % i == 0) {

			// THE BELOW STATEMENT MAKES
			// IT BETTER THAN ABOVE METHOD
			//  AS WE REDUCE VALUE OF n.
			n = n / i;

			curr_term *= i;
			curr_sum += curr_term;
		}

		res *= curr_sum;
	}

	// This condition is to handle
	// the case when n is a prime
	// number greater than 2.
	if (n >= 2)
		res *= (1 + n);

	return res;
}

void SUM(lli *a, lli n)
{
	lli sum=0;
	rep(i,0,n)sum+=a[i];
	cout<<sum<<endl;
}

void addEdge(vector<lli> adj[], lli u, lli v)
{
	adj[u].push_back(v);
	adj[v].push_back(u);
}

void printGraph(vector<lli> adj[], lli V)
{
	for (lli v = 1; v < V; ++v)
	{
		cout << "\n Adjacency list of vertex "
			 << v << "\n head ("<<v<<")";
		rep(x,0,adj[v].size())
		   cout << " -> " << adj[v][x];
		printf("\n");
	}
}

bool cycle(vector<lli> adj[], lli vis[], lli n, lli parent)
{
	vis[n]=1;
	if(adj[n].empty())return false;
	bool value=false;
	rep(i,0,adj[n].size())
	{
		if(adj[n][i]!=parent)
		{
			if(vis[adj[n][i]]==1)return true;
			else
				value|=cycle(adj,vis,adj[n][i],n);
		}
	}
	return value;
}

typedef struct num{
	lli index;
	lli value;
}num;

lli countFactors(lli n)
{
	lli count=0;
	lli ans=1;
	rep(i,2,sqrt(n)+1)
	{
		count=0;
		while(n%i==0)
		{
			n/=i;
			++count;
		}
		ans*=count;
	}
	if(n>1)ans*=2;
	return ans;
}

lli kthFactor(lli n, lli k)
{
	vector <lli> v;
	rep(i,2,(lli)sqrt(n)+1)
	{
		if(n%i==0)
		{
			v.pb(i);
			if(i<n/i) v.pb(n/i);
		}
	}
	v.pb(1);
	v.pb(n);
	sort(v.begin(),v.end());
	// print1D(v,v.size());
	if(v.size()<k)return -1;
	return v[k-1];

}

lli binSmaller(lli a[], lli n, lli k)
{
	lli l,r;
	l=0;
	r=n-1;
	lli mid=(l+r)/2;
	if(a[r]<=k)return r;
	if(a[l]>k)return -1;
	while(l<r)
	{
		if(a[mid]>k && a[mid-1]<=k)return mid-1;
		if(k>=a[mid] && a[mid+1]>k)return mid;
		if(k<a[mid])r=mid-1;
		else l=mid+1;
		mid=(l+r)/2;

	}
	return -1;
}

//returns count of numbers smaller than or equal to key
lli binarySearchCount(lli arr[], lli n, lli key)
{
	lli left = 0;
	lli right = n - 1;

	lli count = 0;
	lli mid=0;
	while (left <= right) {
		mid = (right + left) / 2;

		// Check if middle element is
		// less than or equal to key
		if (arr[mid] <= key) {

			// At least (mid + 1) elements are there
			// whose values are less than
			// or equal to key
			count = mid + 1;
			left = mid + 1;
		}

		// If key is smaller, ignore right half
		else
			right = mid - 1;
	}

	return count;
}

lli binomialCoeff(lli n, lli k)
{
	lli res = 1;

	// Since C(n, k) = C(n, n-k)
	if ( k > n - k )
		k = n - k;

	// Calculate value of
	// [n * (n-1) *---* (n-k+1)] / [k * (k-1) *----* 1]
	for (lli i = 0; i < k; ++i)
	{
		res *= (n - i);
		res /= (i + 1);
	}

	return res;
}

lli modInverse(lli a, lli m= MOD )
{
	lli m0 = m , y = 0, x = 1; if (m == 1) return 0;
	while (a > 1)
	{
		lli q = a / m;
		lli t = m;
		m = a % m, a = t;
		t = y;
		y = x - q * y;
		x = t;
	}
	if (x < 0)  x += m0; return x;
}

 // ncr in o(1)
const int N = 10e5+1;
lli factorialNumInverse[N + 1];  lli naturalNumInverse[N + 1]; lli fact[N + 1];

void InverseofNumber(lli p)
{
	naturalNumInverse[0] = naturalNumInverse[1] = 1;
	for (lli i = 2; i <= N; i++)
		naturalNumInverse[i] = naturalNumInverse[p % i] * (p - p / i) % p;
}

void InverseofFactorial(lli p)
{
	factorialNumInverse[0] = factorialNumInverse[1] = 1;
	for (lli i = 2; i <= N; i++)
		factorialNumInverse[i] = (naturalNumInverse[i] * factorialNumInverse[i - 1]) % p;
}

void factorial(lli p)
{
	fact[0] = 1;
	for (lli i = 1; i <= N; i++)
	{
		fact[i] = (fact[i - 1] * i) % p;
	}
}

lli nCr(lli N, lli R, lli p = MOD)
{
	if(R==0) return 1;
	lli ans = ((fact[N] * factorialNumInverse[R])  % p * factorialNumInverse[N - R]) % p;
	return ans;
}
//*/

lli numberOfPaths(lli m, lli n)
{
	// We have to calculate m+n-2 C n-1 here
	// which will be (m+n-2)! / (n-1)! (m-1)!
	lli path = 1;
	for (lli i = n; i < (m + n - 1); i++) {
		path *= i;
		path /= (i - n + 1);
	}
	return path;
}

int main()
{
	fio;
	// #ifndef ONLINE_JUDGE
	// 	freopen("alchemy_input.txt", "r", stdin);
	// 	freopen("alchemy_output.txt", "w", stdout);
	// #endif
	TEST_CASES
	{
		lli m,d,w;
		cin>>m>>d>>w;
		m=min(m,d);
		if((d-1)%w==0)cout<<(m*(m-1))/2<<endl;
		else
		{
			w/=gcd(d-1,w);
			// lli left=d;
			m=min(d,m);
			lli q=0/*m-m%w*/;
			q=(m)/w;
			lli ans=(m)*q-((q*(q+1))/2)*w;

			cout<<ans<<endl;
		}
	}

}


#include <bits/stdc++.h>
#include<string.h>
#include<string>

using namespace std;

typedef long long int lli;

#define fio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define rep(i,a,b) for(lli i=a;i<b;i++)
#define rev(i,b,a) for(lli i=b;i>=a;i--)
#define FOREACH(i, x) for(__typeof((x).begin()) i = (x).begin(); i != (x).end(); i ++)
#define debug(x) cout << "\t[ " << #x  << " : " << x << " ]" << endl;
#define debugT(x) cout << "\t" << #x  << " : " << x << endl;
#define MOD (lli)(1000000000+7) //10^9+7
#define MAXN 1000000+1 //10^6+1
#define endl "\n"
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define TEST_CASES  lli t;cin >> t;while(t--)

lli max(lli a, lli b){ if(a>b) return a ; return b; }
lli min(lli a, lli b){ if(a<b) return a ; return b; }

string sing(char x)
{
	string s(1,x) ;
	return s;
}

bool cmpDec(lli a,lli b)
{
	if(a>=b)
	return true;
return false;
}

void print1D(lli a[], lli n)
{
	rep(i,0,n)cout<<a[i]<<" ";
	cout<<endl;
}

void print2D(lli **dp, lli n, lli m)
{
	rep(i,0,n)
	{
		rep(j,0,m)
		cout<<dp[i][j]<<" ";
		cout<<endl;
	}
}

void init1D(lli a[], lli n, lli k)
{
	rep(z,0,n)a[z]=k;
}

void init2D(lli **a, lli n, lli m, lli k)
{
	rep(i,0,n)
	rep(j,0,m)
	a[i][j]=k;
}

lli fastmod(lli base,lli exp, lli m)
{
	lli res=1;
	while(exp)
	{
		if (exp&1ll)
			res=(res*base)%m;
		base=(base*base)%m;
		exp/=2;
	}
	return res;
}

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

lli logBase(lli m, lli b)
{
	lli count=0;
	while(m>1)
	{
		++count;
		m=m/b;
	}
	return count;
}

// stores smallest prime factor for every number
lli spf[MAXN];

// Calculating SPF (Smallest Prime Factor) for every
// number till MAXN.
// Time Complexity : O(nloglogn)
void sieve()
{
	spf[1] = 1;
	for (lli i=2; i<MAXN; i++)

		// marking smallest prime factor for every
		// number to be itself.
		spf[i] = i;

	// separately marking spf for every even
	// number as 2
	for (lli i=4; i<MAXN; i+=2)
		spf[i] = 2;

	for (lli i=3; i*i<MAXN; i++)
	{
		// checking if i is prime
		if (spf[i] == i)
		{
			// marking SPF for all numbers divisible by i
			for (lli j=i*i; j<MAXN; j+=i)

				// marking spf[j] if it is not
				// previously marked
				if (spf[j]==j)
					spf[j] = i;
		}
	}
}

// A O(log n) function returning primefactorization
// by dividing by smallest prime factor at every step
vector<lli> getFactorization(lli x)
{
	vector<lli> ret;
	while (x != 1)
	{
		ret.push_back(spf[x]);
		x = x / spf[x];
	}
	return ret;
}

// Returns sum of all factors of n.
lli sumofFactors(lli n)
{
	// Traversing through all prime factors.
	lli res = 1;
	for (lli i = 2; i <= sqrt(n); i++)
	{

		lli curr_sum = 1;
		lli curr_term = 1;
		while (n % i == 0) {

			// THE BELOW STATEMENT MAKES
			// IT BETTER THAN ABOVE METHOD
			//  AS WE REDUCE VALUE OF n.
			n = n / i;

			curr_term *= i;
			curr_sum += curr_term;
		}

		res *= curr_sum;
	}

	// This condition is to handle
	// the case when n is a prime
	// number greater than 2.
	if (n >= 2)
		res *= (1 + n);

	return res;
}

void SUM(lli *a, lli n)
{
	lli sum=0;
	rep(i,0,n)sum+=a[i];
	cout<<sum<<endl;
}

void addEdge(vector<lli> adj[], lli u, lli v)
{
	adj[u].push_back(v);
	adj[v].push_back(u);
}

void printGraph(vector<lli> adj[], lli V)
{
	for (lli v = 1; v < V; ++v)
	{
		cout << "\n Adjacency list of vertex "
			 << v << "\n head ("<<v<<")";
		rep(x,0,adj[v].size())
		   cout << " -> " << adj[v][x];
		printf("\n");
	}
}

bool cycle(vector<lli> adj[], lli vis[], lli n, lli parent)
{
	vis[n]=1;
	if(adj[n].empty())return false;
	bool value=false;
	rep(i,0,adj[n].size())
	{
		if(adj[n][i]!=parent)
		{
			if(vis[adj[n][i]]==1)return true;
			else
				value|=cycle(adj,vis,adj[n][i],n);
		}
	}
	return value;
}

typedef struct num{
	lli index;
	lli value;
}num;

lli countFactors(lli n)
{
	lli count=0;
	lli ans=1;
	rep(i,2,sqrt(n)+1)
	{
		count=0;
		while(n%i==0)
		{
			n/=i;
			++count;
		}
		ans*=count;
	}
	if(n>1)ans*=2;
	return ans;
}

lli kthFactor(lli n, lli k)
{
	vector <lli> v;
	rep(i,2,(lli)sqrt(n)+1)
	{
		if(n%i==0)
		{
			v.pb(i);
			if(i<n/i) v.pb(n/i);
		}
	}
	v.pb(1);
	v.pb(n);
	sort(v.begin(),v.end());
	// print1D(v,v.size());
	if(v.size()<k)return -1;
	return v[k-1];

}

lli binSmaller(lli a[], lli n, lli k)
{
	lli l,r;
	l=0;
	r=n-1;
	lli mid=(l+r)/2;
	if(a[r]<=k)return r;
	if(a[l]>k)return -1;
	while(l<r)
	{
		if(a[mid]>k && a[mid-1]<=k)return mid-1;
		if(k>=a[mid] && a[mid+1]>k)return mid;
		if(k<a[mid])r=mid-1;
		else l=mid+1;
		mid=(l+r)/2;

	}
	return -1;
}

//returns count of numbers smaller than or equal to key
lli binarySearchCount(lli arr[], lli n, lli key)
{
	lli left = 0;
	lli right = n - 1;

	lli count = 0;
	lli mid=0;
	while (left <= right) {
		mid = (right + left) / 2;

		// Check if middle element is
		// less than or equal to key
		if (arr[mid] <= key) {

			// At least (mid + 1) elements are there
			// whose values are less than
			// or equal to key
			count = mid + 1;
			left = mid + 1;
		}

		// If key is smaller, ignore right half
		else
			right = mid - 1;
	}

	return count;
}

lli binomialCoeff(lli n, lli k)
{
	lli res = 1;

	// Since C(n, k) = C(n, n-k)
	if ( k > n - k )
		k = n - k;

	// Calculate value of
	// [n * (n-1) *---* (n-k+1)] / [k * (k-1) *----* 1]
	for (lli i = 0; i < k; ++i)
	{
		res *= (n - i);
		res /= (i + 1);
	}

	return res;
}

lli modInverse(lli a, lli m= MOD )
{
	lli m0 = m , y = 0, x = 1; if (m == 1) return 0;
	while (a > 1)
	{
		lli q = a / m;
		lli t = m;
		m = a % m, a = t;
		t = y;
		y = x - q * y;
		x = t;
	}
	if (x < 0)  x += m0; return x;
}

 // ncr in o(1)
const int N = 10e5+1;
lli factorialNumInverse[N + 1];  lli naturalNumInverse[N + 1]; lli fact[N + 1];

void InverseofNumber(lli p)
{
	naturalNumInverse[0] = naturalNumInverse[1] = 1;
	for (lli i = 2; i <= N; i++)
		naturalNumInverse[i] = naturalNumInverse[p % i] * (p - p / i) % p;
}

void InverseofFactorial(lli p)
{
	factorialNumInverse[0] = factorialNumInverse[1] = 1;
	for (lli i = 2; i <= N; i++)
		factorialNumInverse[i] = (naturalNumInverse[i] * factorialNumInverse[i - 1]) % p;
}

void factorial(lli p)
{
	fact[0] = 1;
	for (lli i = 1; i <= N; i++)
	{
		fact[i] = (fact[i - 1] * i) % p;
	}
}

lli nCr(lli N, lli R, lli p = MOD)
{
	if(R==0) return 1;
	lli ans = ((fact[N] * factorialNumInverse[R])  % p * factorialNumInverse[N - R]) % p;
	return ans;
}
//*/

lli numberOfPaths(lli m, lli n)
{
	// We have to calculate m+n-2 C n-1 here
	// which will be (m+n-2)! / (n-1)! (m-1)!
	lli path = 1;
	for (lli i = n; i < (m + n - 1); i++) {
		path *= i;
		path /= (i - n + 1);
	}
	return path;
}

int main()
{
	fio;
	// #ifndef ONLINE_JUDGE
	// 	freopen("alchemy_input.txt", "r", stdin);
	// 	freopen("alchemy_output.txt", "w", stdout);
	// #endif
	TEST_CASES
	{
		lli m,d,w;
		cin>>m>>d>>w;

			w/=gcd(d-1,w);
			// lli left=d;
			m=min(d,m);
			lli q=0;
			q=(m)/w;
			lli ans=(m)*q-((q*(q+1))/2)*w;

			cout<<ans<<endl;

	}

}


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

void dfs(int s,int* cnt,vector<vector<pair<int,int> >>& g,int p,int* par)
{
    par[s]=p;
    bool leaf=true;
    for(auto i:g[s]){
        if(i.first==par[s])
        leaf=false;
        dfs(i.first,cnt,g,s,par);
        cnt[s]+=cnt[i.first];
    }
    if(leaf)cnt[s]=1;

}
signed main(){

    int t;
     cin>>t;
     while(t--)
     {
         int m,d,w;
         cin>>m>>d>>w;
         int u=min(m,d);
         int ans=0;
         for(int i=1;i<=u;i++)
         {
             if(i*(d-1)%w==0)
                ans+=u-i;
             /*for(int j=1;j<=u;j++)
             {
                 if(i!=j && ((i-1)*d+j)%w==((j-1)*d+i)%w)
                    ans++;
                 cout<<ans<<endl;
             }*/
         }
         cout<<ans<<endl;
     }

return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
int gcd(int a,int b)
{
    if(a>b)swap(a,b);
    if(a==0)return b;
    return gcd(b%a,a);
}

void dfs(int s,int* cnt,vector<vector<pair<int,int> >>& g,int p,int* par)
{
    par[s]=p;
    bool leaf=true;
    for(auto i:g[s]){
        if(i.first==par[s])
        leaf=false;
        dfs(i.first,cnt,g,s,par);
        cnt[s]+=cnt[i.first];
    }
    if(leaf)cnt[s]=1;

}
signed main(){

    int t;
     cin>>t;
     while(t--)
     {
         int m,d,w;
         cin>>m>>d>>w;
         int u=min(m,d);
         int w1=w/gcd(d-1,w);
         int k=u/w1;
         cout<<(k*u)-((k*(k+1))/2)*w1<<endl;
     }

return 0;
}


#include <bits/stdc++.h>

#define endl '\n'

using namespace std;
typedef long long ll;
const int N = 1e5 + 7, M = 2 * N;

int main() {
    ios_base::sync_with_stdio(0), cin.tie(0);
#ifdef CLION
    freopen("in", "rt", stdin);
#endif

    int nTests;
    scanf("%d", &nTests);
    for (int tid = 1; tid <= nTests; ++tid) {
        int m, d, w;
        scanf("%d%d%d", &m, &d, &w);
        int g = __gcd(d - 1, w);
        int must = w / g;
        int lim = min(m, d);
        ll tmp = (lim / must);
        ll ans = tmp * (tmp - 1) * must / 2;
        tmp = lim - tmp*must;
        ans += tmp * (lim / must);
//        for (int i = 0; i < lim; ++i) {
//            ans += (lim - i) / must;
//        }
        printf("%lld\n", ans);
    }
}


#include <bits/stdc++.h>

#define endl '\n'

using namespace std;
typedef long long ll;
const int N = 1e5 + 7, M = 2 * N;

int main() {
    ios_base::sync_with_stdio(0), cin.tie(0);
#ifdef CLION
    freopen("in", "rt", stdin);
#endif

    int nTests;
    scanf("%d", &nTests);
    for (int tid = 1; tid <= nTests; ++tid) {
        int m, d, w;
        scanf("%d%d%d", &m, &d, &w);
        int g = __gcd(d - 1, w);
        int must = w / g;
        int lim = min(m, d);
        ll tmp = (lim / must);
        ll ans = tmp * lim - tmp * (tmp + 1) * must / 2;
        printf("%lld\n", ans);
    }
}


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

#define db(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) { cout << name << " : " << arg1 << '\n'; }
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
	const char* comma = strchr(names + 1, ',');
	cout.write(names, comma - names) << " : " << arg1 << " |"; __f(comma + 1, args...);
}
#define rng(x) x.begin(), x.end()
#define pb push_back
#define F first
#define S second
#define el '\n'

typedef long long ll;
typedef pair<int,int> pi;
typedef pair<ll,ll> pl;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef vector<bool> vb;

inline ll n2(ll n){
	return (n*(n+1))/2;
}

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

void solve(){
	ll m, d, w; cin>>m>>d>>w; // m = 10 d = 7 w = 12
	w = w/gcd(d-1, w); // w = 2;
	if(d < m) m = d;
	ll t = (m - 1)/w; // m = 7, t = 3;
	ll c1 = m - w*t; // c1 = 1;
	ll ans = c1*n2(t) + (w-c1)*n2(t-1);
	cout<<ans<<el;
}

int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	#ifndef ONLINE_JUDGE
		freopen("input.txt", "r", stdin);
		freopen("output.txt", "w", stdout);
	#endif
	int T=1, tc = 1;
	cin>>T;
	while(T--){
		solve();
	}
	return 0;
}


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

#define db(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) { cout << name << " : " << arg1 << '\n'; }
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
	const char* comma = strchr(names + 1, ',');
	cout.write(names, comma - names) << " : " << arg1 << " |"; __f(comma + 1, args...);
}
#define rng(x) x.begin(), x.end()
#define pb push_back
#define F first
#define S second
#define el '\n'

typedef long long ll;
typedef pair<int,int> pi;
typedef pair<ll,ll> pl;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef vector<bool> vb;

inline ll n2(ll n){
	return (n*(n-1))/2;
}

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

void solve(){
	ll m, d, w; cin>>m>>d>>w;
	w = w/gcd(d-1, w);
	if(d < m) m = d;
	ll ans = (m%w)*n2(m/w + 1) + (w - m%w)*n2(m/w);
	cout<<ans<<el;
}

int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	#ifndef ONLINE_JUDGE
		freopen("input.txt", "r", stdin);
		freopen("output.txt", "w", stdout);
	#endif
	int T=1, tc = 1;
	cin>>T;
	while(T--){
		solve();
	}
	return 0;
}


#include<bits/stdc++.h>

using namespace std;

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

int main()
{
  int t;
  cin>>t;
  while(t--)
  {
    int m,d,w;
    cin>>m>>d>>w;

    int t=w/gcd(w,d-1);
    //cout<<"t:"<<t<<endl;

    int x, y;
    int u=t;
    long long rez=0;
    while(u<=min(m,d))
    {
      rez+=min(m,d)-u;
      u=u+t;
    }
    cout<<rez<<endl;
  }
}


#include<bits/stdc++.h>

using namespace std;

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

int main()
{
  int t;
  cin>>t;
  while(t--)
  {
    int m,d,w;
    cin>>m>>d>>w;

    int t=w/gcd(w,d-1);
    //cout<<"t:"<<t<<endl;

    int x, y;
    int u=t;
    long long rez=0;
    int n=min(m,d)/t;

    rez+=(long long)n * min(m,d) - (long long)n*(n+1)/2*t;
   /* while(u<=min(m,d))
    {
      rez+=min(m,d)-u;
      u=u+t;
    }*/
    cout<<rez<<endl;
  }
}


#include<bits/stdc++.h>

using namespace std;

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

int main()
{
  int t;
  cin>>t;
  while(t--)
  {
    int m,d,w;
    cin>>m>>d>>w;

    int t=w/gcd(w,d-1);
    //cout<<"t:"<<t<<endl;

    int x, y;
    int u=t;
    long long rez=0;
    int n=min(m,d)/t;

    rez+=n * min(m,d) - n*(n+1)/2*t;
   /* while(u<=min(m,d))
    {
      rez+=min(m,d)-u;
      u=u+t;
    }*/
    cout<<rez<<endl;
  }
}


#include <bits/stdc++.h>
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
using namespace std;

typedef long long ll;
typedef unsigned long long llu;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef pair<int, pii> piii;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<int, ll> pil;
typedef pair<string, int> psi;
typedef pair<char, int> pci;
typedef pair<int, char> pic;
const ll MOD = (ll)1e9 + 7;
const long double PI = 3.141592653589793238462643383279502884197;

ll fac[1] = {1}, inv[1] = {1};
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a*b/gcd(a,b);}
ll mp(ll a,ll b){ll ret=1;while(b){if(b&1)ret=ret*a%MOD;a=a*a%MOD;b>>=1;}return ret;}

ll cmb(ll r) {
    if (r<2) return 0;
    return r*(r-1)/2;
}

int main() {
    int tc;
    scanf("%d", &tc);

    while (tc--) {
        ll m,d,w;
        scanf("%lld %lld %lld", &m, &d, &w);

        ll mm = min(m,d)-1; // 이것보다 엄격히 작은 것 중에..
        ll ck=d-1;
        ll gc = gcd(ck, w);
        // printf("%lld //", gc);

        gc = w/gc;  // 매 이것마다.

        ll ret = 0;
        // printf("%lld //", gc);

        for (int i = 0; i < gc; i++) {
            if (i>=mm) break;

            ll t = (mm-i)/gc;
            ret += cmb(t+1);

            // printf("%d : %lld\n", i, t);
        }

        printf("%lld\n" ,ret);
    }
}


#include <bits/stdc++.h>
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
using namespace std;

typedef long long ll;
typedef unsigned long long llu;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef pair<int, pii> piii;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<int, ll> pil;
typedef pair<string, int> psi;
typedef pair<char, int> pci;
typedef pair<int, char> pic;
const ll MOD = (ll)1e9 + 7;
const long double PI = 3.141592653589793238462643383279502884197;

ll fac[1] = {1}, inv[1] = {1};
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a*b/gcd(a,b);}
ll mp(ll a,ll b){ll ret=1;while(b){if(b&1)ret=ret*a%MOD;a=a*a%MOD;b>>=1;}return ret;}

ll cmb(ll r) {
    if (r<2) return 0;
    return r*(r-1)/2;
}

int main() {
    int tc;
    scanf("%d", &tc);

    while (tc--) {
        ll m,d,w;
        scanf("%lld %lld %lld", &m, &d, &w);

        ll mm = min(m,d)-1;
        ll ck=d-1;
        ll gc = gcd(ck, w);

        gc = w/gc;
        ll ret = 0;

        ll tt = mm/gc;
        tt = cmb(tt+1);
        ret += tt * (mm%gc+1);
        tt = cmb(mm/gc);
        ret += tt * (gc - (mm%gc+1));

        /*
        for (int i = 0; i < gc; i++) {
            if (i>=mm) break;

            ll t = (mm-i)/gc;
            ret += cmb(t+1);
        }*/

        printf("%lld\n" ,ret);
    }
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define endl '\n'
#define Turnoff std::ios::sync_with_stdio(false)
const ll Max=2e3+5;
const double Pi=acos(-1);

/*
*/

int main(){
    Turnoff;
    int t;cin>>t;
    while(t--){
        ll m,d,w;
        cin>>m>>d>>w;
        ///(x-y)(d-1)mod w=0
        w/=__gcd(d-1,w);
        ll up=min(d,m);
        ll num=up/w;
        cout<<(num*up-num*(1+num)/2*w);
    }
}


#include <iostream>
#include <vector>

using namespace std;

int main(){

    int n, m;
    cin >> n >> m;
    vector <vector<int>> g (n + 1);

    int loop = 0;
    vector<bool> loop_v (n + 1);
    for (int i = 0; i < m; ++i){
        int a, b;
        ios_base::sync_with_stdio(false);
        cin >> a >> b;
        if (a != b){
            g[a].push_back(b);
            g[b].push_back(a);
        }else{
            g[a].push_back(b);
            loop_v[a] = true;
            ++loop;
        }
    }
    vector<int> color;
    for (int i = 0; i <= n; ++i){
            color.push_back(0);
    }

    int number_of_anj_comp = 0;
    if (m > n + (n-1)*(n-2)/2){
        number_of_anj_comp = 1;
    }else{
        vector<int> stack;
        for (int i = 1; i < n + 1; ++i){
            if (color[i] == 0){
                stack.push_back(i);
                int edges = 0;
                while (stack.size() != 0){
                    int v = stack.back();
                    stack.pop_back();
                    if (color[v] == 0){
                        color[v] = 1;
                        for (auto& u : g[v]){
                            edges += 1;
                            stack.push_back(u);
                        }
                        color[v] = 2;
                    }
                }
                if (edges > 0){
                    number_of_anj_comp += 1;
                }
            }
        }
    }

    if (number_of_anj_comp > 1){
        cout << 0;
    }else{
        long long ans = 0;
        for (int i = 1; i < n + 1; ++i){
            int k = g[i].size();
            if (loop_v[i]){
                k -= 1;
            }
            ans += k * (k - 1) / 2;
        }
        ans += (m - loop) * loop + loop * (loop - 1) / 2;
        cout << ans;
    }
    return 0;
}


#include <iostream>
#include <vector>

using namespace std;

int main(){
    ios::sync_with_stdio(false);
    //cin.tie(NULL);
    int n, m;
    cin >> n >> m;
    vector <vector<int>> g (n + 1);

    int loop = 0;
    vector<bool> loop_v (n + 1);
    for (int i = 0; i < m; ++i){
        int a, b;
        cin >> a >> b;
        if (a != b){
            g[a].push_back(b);
            g[b].push_back(a);
        }else{
            g[a].push_back(b);
            loop_v[a] = true;
            ++loop;
        }
    }

    /*if (n == 1000000){
            cout << "oups";
            return 0;
    }*/

    vector<int> color;
    for (int i = 0; i <= n; ++i){
            color.push_back(0);
    }

    int number_of_anj_comp = 0;
    if (m > n + (n-1)*(n-2)/2){
        number_of_anj_comp = 1;
    }else{
        vector<int> stack;
        for (int i = 1; i < n + 1; ++i){
            if (color[i] == 0){
                stack.push_back(i);
                int edges = 0;
                while (stack.size() != 0){
                    int v = stack.back();
                    stack.pop_back();
                    if (color[v] == 0){
                        color[v] = 1;
                        for (auto& u : g[v]){
                            edges += 1;
                            stack.push_back(u);
                        }
                        color[v] = 2;
                    }
                }
                if (edges > 0){
                    number_of_anj_comp += 1;
                }
            }
        }
    }

    if (number_of_anj_comp > 1){
        cout << 0;
    }else{
        long long ans = 0;
        for (int i = 1; i < n + 1; ++i){
            int k = g[i].size();
            if (loop_v[i]){
                k -= 1;
            }
            ans += k * (k - 1) / 2;
        }
        ans += (m - loop) * loop + loop * (loop - 1) / 2;
        cout << ans;
    }
    return 0;
}


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

typedef long long ll;
typedef unsigned long long ull;
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define P pair<int, int>
#define debug(x) cout << (#x) << ": " << (x) << " "
#define fastio ios::sync_with_stdio(false), cin.tie(0)
#define mod 998244353
const int M = 1000000 + 10;
const int N = 1000000 + 10;

#define int long long

int n, m, u, v;
vector<int> G[N];
int du[N], vis[N], sl, ans;

void dfs(int now, int f)
{
    vis[now] = 1;
    for(int i = 0; i < G[now].size(); ++ i) {
        if(vis[G[now][i]]) continue;
        dfs(G[now][i], now);
    }
}

signed main()
{
    scanf("%lld %lld", &n, &m);
    for(int i = 1; i <= m; ++ i) {
        scanf("%lld %lld", &u, &v);
        if(u == v) sl ++;
        else du[u] ++, du[v] ++, G[u].push_back(v), G[v].push_back(u);;
    }
    for(int i = 1; i <= n; ++ i) vis[i] = !du[i];
    for(int i = 1; i <= n; ++ i) {
        if(!vis[i]) {
            dfs(i, -1);
            break;
        }
    }
    for(int i = 1; i <= n; ++ i) {
        if(!vis[i]) {
            printf("0\n");
            return 0;
        }
    }
    for(int i = 1; i <= n; ++ i) {
        ans += du[i] * (du[i] - 1) / 2;
    }
    ans += sl * (sl - 1) / 2 + sl * (m - sl);
    printf("%lld\n", ans);

    return 0;
}

/*

  Rejoicing in hope, patient in tribulation .

*/


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

typedef long long ll;
typedef unsigned long long ull;
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define P pair<int, int>
#define debug(x) cout << (#x) << ": " << (x) << " "
#define fastio ios::sync_with_stdio(false), cin.tie(0)
#define mod 998244353
const int M = 1000000 + 10;
const int N = 1000000 + 10;

#define int long long

int n, m, u, v;
vector<int> G[N];
int du[N], vis[N], sl, ans;

void dfs(int now, int f)
{
    vis[now] = 1;
    for(int i = 0; i < G[now].size(); ++ i) {
        if(G[now][i] == f) continue;
        dfs(G[now][i], now);
    }
}

signed main()
{
    scanf("%lld %lld", &n, &m);
    for(int i = 1; i <= m; ++ i) {
        scanf("%lld %lld", &u, &v);
        G[u].push_back(v), G[v].push_back(u);
        if(u == v) sl ++;
        else du[u] ++, du[v] ++;
    }
    for(int i = 1; i <= n; ++ i) vis[i] = !du[i];
    for(int i = 1; i <= n; ++ i) {
        if(!vis[i]) {
            dfs(i, -1);
            break;
        }
    }
    for(int i = 1; i <= n; ++ i) {
        if(!vis[i]) {
            printf("0\n");
            return 0;
        }
    }
    for(int i = 1; i <= n; ++ i) {
        ans += du[i] * (du[i] - 1) / 2;
    }
    ans += sl * (sl - 1) / 2 + sl * (m - sl);
    printf("%lld\n", ans);

    return 0;
}

/*

  Rejoicing in hope, patient in tribulation .

*/


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

typedef long long ll;
typedef unsigned long long ull;
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define P pair<int, int>
#define debug(x) cout << (#x) << ": " << (x) << " "
#define fastio ios::sync_with_stdio(false), cin.tie(0)
#define mod 998244353
const int M = 1000000 + 10;
const int N = 1000000 + 10;

#define int long long

int n, m, u, v;
vector<int> G[N];
int du[N], tdu[N], vis[N], sl, ans;

void dfs(int now, int f)
{
    vis[now] = 1;
    for(int i = 0; i < G[now].size(); ++ i) {
        if(vis[G[now][i]]) continue;
        dfs(G[now][i], now);
    }
}

signed main()
{
    scanf("%lld %lld", &n, &m);
    for(int i = 1; i <= m; ++ i) {
        scanf("%lld %lld", &u, &v);
        if(u == v) sl ++;
        else du[u] ++, du[v] ++, G[u].push_back(v), G[v].push_back(u);
        tdu[u] ++, tdu[v] ++;
    }
    for(int i = 1; i <= n; ++ i) vis[i] = !tdu[i];
    for(int i = 1; i <= n; ++ i) {
        if(!vis[i]) {
            dfs(i, -1);
            break;
        }
    }
    for(int i = 1; i <= n; ++ i) {
        if(!vis[i]) {
            printf("0\n");
            return 0;
        }
    }
    for(int i = 1; i <= n; ++ i) {
        ans += du[i] * (du[i] - 1) / 2;
    }
    ans += sl * (sl - 1) / 2 + sl * (m - sl);
    printf("%lld\n", ans);

    return 0;
}

/*

  Rejoicing in hope, patient in tribulation .

*/


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

#define ff first
#define ss second
#define endl '\n'

const int MAX = 1e6 + 5;
vector<int> adj[MAX];
bool visited[MAX];

void dfs(int root) {
	visited[root] = 1;

	for (const int child : adj[root]) {
		if (!visited[child]) {
			dfs(child);
		}
	}
}

inline ll nC2(ll a) {
	return ((a * (a-1))/2);
}

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

	int n, m;
	cin >> n >> m;

	ll ans = 0;
	ll temp = 0;
	for (int i = 0, u, v; i < m; ++i) {
		cin >> u >> v; --u; --v;
		adj[u].push_back(v);
		++temp;

		if (u != v) {
			adj[v].push_back(u);
			++temp;
		}
	}

	dfs(0);
	for (int i = 0; i < n; ++i) {
		if (visited[i]) {
			temp -= adj[i].size();
		}
	}

	if (temp != 0) {
		cout << 0 << endl;
		return 0;
	}

	for (int i = 0; i < n; ++i) {
		ans += nC2(adj[i].size());
	}

	cout << ans << endl;
	return 0;
}


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

#define ff first
#define ss second
#define endl '\n'

const int MAX = 1e6 + 5;
vector<int> adj[MAX];
bool visited[MAX];
bool self_loop[MAX];

void dfs(int root) {
	visited[root] = 1;

	for (const int child : adj[root]) {
		if (!visited[child]) {
			dfs(child);
		}
	}
}

inline ll nC2(ll a) {
	return ((a * (a-1))/2);
}

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

	int n, m;
	cin >> n >> m;

	ll ans = 0;
	ll temp = 0;
	ll self_loops = 0;
	for (int i = 0, u, v; i < m; ++i) {
		cin >> u >> v; --u; --v;

		if (u != v) {
			adj[u].push_back(v);
			adj[v].push_back(u);
			temp += 2;
		}
		else {
			++self_loops;
			self_loop[u] = 1;
		}
	}

	for (int i = 0; i < n; ++i) {
		if (!adj[i].empty()) {
			dfs(i);
			break;
		}
	}

	for (int i = 0; i < n; ++i) {
		if (visited[i]) {
			temp -= adj[i].size();
		}
		else if (self_loop[i]) {
			cout << 0 << endl;
			return 0;
		}
	}

	if (temp != 0) {
		cout << 0 << endl;
		return 0;
	}

	ans += nC2(self_loops) + ((m - self_loops) * self_loops);
	for (int i = 0; i < n; ++i) {
		ans += nC2(adj[i].size());
	}

	cout << ans << endl;
	return 0;
}


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

#define ff first
#define ss second
#define endl '\n'

const int MAX = 1e6 + 5;
vector<int> adj[MAX];
bool visited[MAX];

void dfs(int root) {
	visited[root] = 1;

	for (const int child : adj[root]) {
		if (!visited[child]) {
			dfs(child);
		}
	}
}

inline ll nC2(ll a) {
	return ((a * (a-1))/2);
}

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

	int n, m;
	cin >> n >> m;

	ll ans = 0;
	ll temp = 0;
	ll self_loops = 0;
	for (int i = 0, u, v; i < m; ++i) {
		cin >> u >> v; --u; --v;

		if (u != v) {
			adj[u].push_back(v);
			adj[v].push_back(u);
			temp += 2;
		}
		else {
			++self_loops;
		}
	}

	for (int i = 0; i < n; ++i) {
		if (!adj[i].empty()) {
			dfs(i);
			break;
		}
	}

	for (int i = 0; i < n; ++i) {
		if (visited[i]) {
			temp -= adj[i].size();
		}
	}

	if (temp != 0) {
		cout << 0 << endl;
		return 0;
	}

	ans += nC2(self_loops) + ((m - self_loops) * self_loops);
	for (int i = 0; i < n; ++i) {
		ans += nC2(adj[i].size());
	}

	cout << ans << endl;
	return 0;
}


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

#define ff first
#define ss second
#define endl '\n'

const int MAX = 1e6 + 5;
vector<int> adj[MAX];
bool visited[MAX];

void dfs(int root) {
	visited[root] = 1;

	for (const int child : adj[root]) {
		if (!visited[child]) {
			dfs(child);
		}
	}
}

inline ll nC2(ll a) {
	return ((a * (a-1))/2);
}

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

	int n, m;
	cin >> n >> m;

	ll ans = 0;
	ll temp = 0;
	ll self_loops = 0;
	for (int i = 0, u, v; i < m; ++i) {
		cin >> u >> v; --u; --v;

		if (u != v) {
			adj[u].push_back(v);
			adj[v].push_back(u);
			temp += 2;
		}
		else {
			++self_loops;
		}
	}

	dfs(0);
	for (int i = 0; i < n; ++i) {
		if (visited[i]) {
			temp -= adj[i].size();
		}
	}

	if (temp != 0) {
		cout << 0 << endl;
		return 0;
	}

	ans += nC2(self_loops) + ((m - self_loops) * self_loops);
	for (int i = 0; i < n; ++i) {
		ans += nC2(adj[i].size());
	}

	cout << ans << endl;
	return 0;
}


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

#define ff first
#define ss second
#define endl '\n'

const int MAX = 1e6 + 5;
vector<int> adj[MAX];
bool visited[MAX];
int numVisited = 0;

void dfs(int root)
{
	visited[root] = 1;
	++numVisited;

	for (const int child : adj[root]) {
		if (!visited[child]) {
			dfs(child);
		}
	}
}

inline ll nC2(ll a) {
	return ((a * (a-1))/2);
}

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

	int n, m;
	cin >> n >> m;

	ll ans = 0;
	for (int i = 0, u, v; i < m; ++i) {
		cin >> u >> v; --u; --v;
		adj[u].push_back(v);

		if (u != v) {
			adj[v].push_back(u);
		}
	}

	dfs(0);

	if (numVisited < n) {
		cout << ans << endl;
		return 0;
	}

	for (int i = 0; i < n; ++i) {
		ans += nC2(adj[i].size());
	}

	cout << ans << endl;
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <queue>
#include <stack>
#include <algorithm>
#include <map>
#include <set>
#include <vector>
#include <cmath>
using namespace std;

#define INIT(x) memset(x,0,sizeof(x))
#define eps 1e-8
#define next next_
typedef long long ll;
typedef unsigned long long ull;
const int INF = 0x7fffffff;
const int inf = 0x3f3f3f3f;
const int maxn = 1000005;
const int N = 105;

inline void read(ll &x) {
    ll f=1;x=0;char s=getchar();
    while(s<'0'||s>'9'){if(s=='-')f=-1;s=getchar();}
    while(s>='0'&&s<='9'){x=x*10+s-'0';s=getchar();}
    x*=f;
}

ll n,m,ans,cnt1,cnt2; //cnt1ά����ͨ�ߣ�cnt2ά���Ի���
ll in[maxn],fa[maxn];
bool vis[maxn]; //�жϹµ�

ll get(ll x) {
	if(x==fa[x]) return x;
	return fa[x] = get(fa[x]);
}

void join(ll x,ll y) {
	fa[get(x)] = get(y);
}

int main() {
	read(n), read(m);
	for(int i=1;i<=n;i++) fa[i] = i;
	for(int i=0;i<m;i++) {
		ll x,y;
		read(x), read(y);
		join(x,y);
		vis[x] = vis[y] = 1;
		if(x!=y) in[x]++, in[y]++;
		if(x!=y) cnt1++;
		else cnt2++;
	}
//	for(int i=1;i<=n;i++) cout<<i<<' '<<fa[i]<<endl;
	set<int> s;
	for(int i=1;i<=n;i++) {
		if(vis[i]) s.insert(get(i));
		if(s.size()>2) {
			cout<<0<<endl;
			return 0;
		}
	}
	for(int i=1;i<=n;i++) { //�����й��������ͨ��
		ans += in[i] * (in[i]-1) / 2;
	}
	ans += cnt1 * cnt2; //һ����ͨ�ߣ�һ���Ի�
	ans += cnt2 * (cnt2-1) / 2; //�����Ի�
	cout<<ans<<endl;
	return 0;
}