//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();
		StringBuilder s = new StringBuilder();
		while (t-- > 0) {
			long n = sc.nextLong(), m = sc.nextLong();
			long z = n - m;
			long k = z / (m + 1);
			long tot = (n * (n + 1)) / 2;
			long res;
			if (m == 0) res = 0;
			else if (z <= m) res = tot - z;
			else res = tot - (((k * (k + 1)) / 2) * (m + 1 - (z % (m + 1)))) - ((((k + 1) * (k + 2)) / 2) * (z % (m + 1)));
			s.append(res + "\n");
		}
		System.out.println(s.toString());
	}
}
