//package com.kraken.upsolve;

import java.util.Scanner;

public class C {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int t = sc.nextInt();
		while (t-- > 0) {
			long n = sc.nextLong(), m = sc.nextLong();
			long z = n - m;
			long k = z / (m + 1);
			long res;
			if (m == 0) res = 0;
			else if (z <= m) res = ((n * (n + 1)) / 2) - z;
			else res = ((n * (n + 1)) / 2) - (((k * (k + 1)) / 2) * (m + 1)) + ((z % (m + 1)) * (k + 1));
			System.out.println(res);
		}
	}

}
