import java.io.*;
import java.util.*;

public class ayoubFunc {
	public static int next(StringTokenizer st) {
		return Integer.parseInt(st.nextToken());
	}

	static int T, N, M;
	public static void main(String[] args) throws IOException{
		// TODO Auto-generated method stub
		BufferedReader sc = new BufferedReader(new InputStreamReader(System.in));
		
		T = Integer.parseInt(sc.readLine());
		for(int i = 0; i < T; i++) {
			StringTokenizer st = new StringTokenizer(sc.readLine());
			N = next(st);
			M = next(st);
			N -= M;
			int n = N / (M+1);
			//N = 3, M = 2, n = 1
			int left = N % (M+1);
			long ans = C2(N+M) - ((left) * C2(n+1) + (M+1-left) * C2(n));
			System.out.println(ans);
		}

	}
	
	public static long C2(int n) {
		return (long) n * (n + 1) / 2;
	}

}