import java.util.*;
import java.io.*;
public class C {
	public static void main(String[] args) throws NumberFormatException, IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		PrintWriter pw = new PrintWriter(System.out);
		int t = Integer.parseInt(br.readLine());
		while(t-->0) {
			String[] inp = br.readLine().split(" ");
			long n = Integer.parseInt(inp[0]);
			long m = Integer.parseInt(inp[1]);
			long q = m+1;
			long zeros = (n-m)/q;
			long rem = (n-m)%q;
			long ans = n*(n+1)/2;
			ans-=(zeros*(zeros+1)/2)*(q-rem);
			ans-=((zeros+1)*(zeros+2)/2)*rem;
			pw.println(ans);
			
		}
		pw.flush();
	}
}
