

import java.util.Scanner;

public class C4 {

	public static void main(String[] args) {
		Scanner nik = new Scanner(System.in);
		int t = nik.nextInt();
		StringBuilder st = new StringBuilder();
		while (t-- > 0) {
			long n = nik.nextLong(), m = nik.nextLong();
			long z0s = n - m, gt = (n * (n + 1)) / 2;
			if (m == 0)
				st.append(0 + "\n");
			else if (z0s <= m)
				st.append((gt - z0s) + "\n");
			else {
				long sz = z0s / (m + 1), rem = z0s % (m + 1);
				long ans = (((sz + 1) * (sz + 2)) / 2) * rem + (((sz) * (sz + 1)) / 2) * (m + 1 - rem);
				st.append((gt - ans) + "\n");
			}
		}
		System.out.print(st);
	}

}
