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

#define mod 1000000007
#define MAXX  1e18
#define MINN -1e18

#define f first
#define s second
#define ll long long
#define ull unsigned long long int
#define ld long double
#define pb push_back
#define pi pair<ll, ll>
#define pl pair<ll, ll>
#define all(a) a.begin(), a.end()
#define lb(a, key) lower_bound(all(a), key) - a.begin();
#define ub(a, key) upper_bound(all(a), key) - a.begin();
// #define fastio  ios_base::sync_with_stdio(false); cin.tie(NULL);
#define add(a, b) (((a)%mod + (b)%mod + mod)%mod)
#define mul(a, b) (((a)%mod * (b)%mod) % mod)
#define endl cout<<"\n";
#define debug(vari) cerr<< "["<< #vari << " " << (vari) << "]";endl;
#define read(a) for(auto &i: a) cin >> i;
#define out(a)  for(auto i: a) cout << i << " ";endl
#define ordered_set tree<ll, null_type, std::less<ll>, rb_tree_tag, tree_order_statistics_node_update>

using namespace __gnu_pbds;
using namespace std;

int main() {
    // fastio
    int t; 
    cin >> t;
    while(t--) {
       ll n, m; cin >> n >> m;
       if(m == 0) {
           cout << 0 << "\n";
           continue;
       }
       ll x = (n-m)/(m+1);
       ll total = n*(n+1)/2;
       ll ans = (x*(x+1)/2)*(m+1);
       ll tmp = (n-m)%(m+1);
       ans = ans + tmp;
    //    debug(ans);
       cout << total-ans << "\n";

    }
}