import java.util.*;
import java.io.*;

public class Solution{
	public static void main (String[] args) throws IOException{
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		PrintWriter out=new PrintWriter(System.out);
		String[] temp=br.readLine().trim().split(" ");
		
		int numTestCases=Integer.parseInt(temp[0]);
		while(numTestCases-->0){
		    temp=br.readLine().trim().split(" ");
		    int n=Integer.parseInt(temp[0]);
		    int m=Integer.parseInt(temp[1]);
		    
		    out.println(maxValue(n,m));
		}
		
		out.flush();
		out.close();
	}
	public static long maxValue(int n,int m)
	{
	    long total=(1L*n*(n+1))/2;
	    if(m==0){
	        return 0;
	    }
	    if(n==m){
	        return total;
	    }
	    
	    if((n-m)%(m+1)==0)
	    {
	        int l=(n-m)/(m+1);
	        return total-(1L*(m+1)*l*(l+1))/2;
	    }
	    else{
	        int l=(n-m)/(m+1);
	        int remainder=(n-m)%(m+1);
	        long ans=total-(1L*(m-remainder)*l*(l+1))/2;
	        l++;
	        return ans-(1L*remainder*l*(l+1))/2;
	    }
	}
}