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

#define ll long long int
#define mod 1000000007

#define 	endl 		'\n'
#define 	max(a,b) 	((a>b)?(a):(b))
#define 	min(a,b) 	((a<b)?(a):(b))
//#define		mp 			make_pair
#define 	pb 			push_back
#define 	fast  		ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);

///use __gcd


ll powe( ll a, ll b) { ll ans = 1;  while(b){if(b&1){ans = ans*a;}a = a*a;b = b>>1;} return ans; }
ll lcm(ll a,ll b){
    ll ans=a*b;
    ans/=__gcd(a,b);
    return ans;
}
bool isprime(ll a)
{
    for(ll i=2;i<=sqrt(a);i++)
    {
        if(a%i==0)return false;
    }
    return true;
}

bool ispalin(string str)
{
    for(int i=0;i<str.length()/2;i++)
    {
        if(str[i]!=str[str.length()-i-1])return false;
    }
    return true;
}
ll kadane(vector<int> &v ,bool temp)
{
    ll sum=0;ll ans=0;ll mx=INT_MIN;
    int os,oe;int cs=0,ce=-1;int n=v.size();
    for(int i=0;i<v.size();i++)
    {
        sum+=v[i];ce++;mx=max(mx,v[i]);
        if(sum>ans&&!(cs==0 && ce==n-1))
        {ans=sum;os=cs;oe=ce;
        }
        if(sum<=0){sum=0;cs=i+1;ce=i;}
    }
    //cout<<ans<<endl;
    ///cout<<os<<" "<<oe<<endl;
    if(temp==true)return mx;
    return ans;

}
vector<int> sieve()
{
    bool isprime[100001];
    for(int i=2;i<=100000;i++)isprime[i]=true;
    isprime[0]=false;isprime[1]=false;
    for(int p=2;p*p<=100000;p++)
    {
        if(isprime[p]==true)
        {
            for(int i=p*p;i<=100000;i+=p)
                isprime[i]=false;
        }
    }
    vector<int> primes;
    for(int i=0;i<=100000;i++)
    {
        if(isprime[i])primes.push_back(i);
    }
    return primes;

}
bool mycomp(int a,int b)
{
    return a>b;
}
void segmented(ll l, ll r, vector<int> &primes)
{
    bool isprimes[100001];
    for(int i=2;i<=100000;i++)isprimes[i]=true;
    for(int i=0;i<primes.size();i++)
    {
        int cur=primes[i];
        int base=ceil(1.0*l/cur);base*=cur;

        for(int j=base;j<=r;j+=cur)
        {
            isprimes[j-l]=false;
        }
        if(base==cur)isprimes[base-l]=true;
    }
    for(int i=l;i<=r;i++)
    {
        if(isprimes[i-l]==true)cout<<i<<endl;
    }
}

int main() {
    fast;
    ///#ifndef ONLINE_JUDGE
    ///  freopen("input.txt",  "r",  stdin);
    ///freopen("output.txt", "w", stdout);
    ///#endif
    int t=1;
    cin>>t;
    while(t--)
    {
        long int n,ones;
      cin>>n>>ones;

      long int totalSubstrings = (n * (n + 1)) / 2;
      long int zeroes = n - ones;

      if(zeroes <= ones + 1){
         cout<<totalSubstrings - zeroes<<endl;
         continue;
      }

    long int zeroesPerOne = zeroes / (ones + 1);
    long int remaingZeroes = zeroes % (ones+1);

    //No extra
    long int badSubstrings =((ones + 1) - remaingZeroes) * ( zeroesPerOne * (zeroesPerOne + 1)/2);
    //Extra zero
    badSubstrings +=  (remaingZeroes *(zeroesPerOne + 1 ) * (zeroesPerOne + 2 )) / 2;

    cout<<totalSubstrings - badSubstrings<<endl;
        label:
            int x;
    }


    return 0;
}
