// No sorceries shall prevail. //           
import java.util.*;
import java.io.*;
	
public class InVoker {	
	
	static long mod = 1000000007;
	public static void main(String args[]) {
			
		Scanner inp=new Scanner(System.in);
		PrintWriter out=new PrintWriter(System.out);
		
		int t=inp.nextInt();
		while(t-->0) {
			long n=inp.nextLong();
			long m=inp.nextLong();
			long gg=(n*(n+1))/2;
			long x=(n-m)/(m+1);
			//out.println(x+" "+gg);
			if(m>=(n)/2) {
				out.println(gg-(n-m));
				continue;
			}
			if((n-m)%(m+1)==0) {
				gg-=(m+1)*(x*(x+1)/2);
			}
			else {
				long rem=(n-m)%(m+1);
				gg-=(m+1-rem)*(x*(x+1)/2);
				gg-=rem*((x+1)*(x+2)/2);
			}
			out.println(gg);
			
		}
		
		out.close();
		inp.close();
	}
}
