// package Quarantine;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class AyoubsFunc {
    public static void main(String[] args)throws IOException {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        int test=Integer.parseInt(br.readLine());
        StringBuilder print=new StringBuilder();
        while(test--!=0){
            String line[]=br.readLine().split(" ");
            long n=Long.parseLong(line[0]);
            long m=Long.parseLong(line[1]);
            long z=n-m;
            if(m==0){
                print.append("0\n");
                continue;
            }
            else{
                long t=z/(m+1);
                long r=z%(m+1);
                long total=(n*(n+1))/2;
                long tc=m+1-r;
                long t2=t+1;
                long f=(t*(t+1))/2*tc+(t2*(t2+1))/2*r;
                long ans=total-f;
                print.append(ans+"\n");
            }
        }
        System.out.println(print.toString());
    }
}
