import java.util.*;

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        for (int i = 0; i < t; i++) {
            long n, m;
            n = sc.nextLong();
            m = sc.nextLong();
            if (m == 0) {
                System.out.println(0);
                continue;
            }
            long c = n / m;
            if (m == 1) {
                c = n / 2 + n % 2;
                long res = sum(n  - 1) - sum(n - c - 1) - sum(c - 2) + m;
                System.out.println(res);
            } else {
                long pairWithoutOne = m * sum(c - 2) + sum(n - m * c - 1);
                long pairAll = sum(n - 1);
                System.out.println(pairAll - pairWithoutOne + m);
            }
        }
    }

    public static long sum(long n) {
        if (n <= 1) {
            return 0;
        }
        return ((n + 1) * 1l * n) / 2;
    }
}
