import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Scanner;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        Scanner in = new Scanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskC solver = new TaskC();
        int testCount = Integer.parseInt(in.next());
        for (int i = 1; i <= testCount; i++)
            solver.solve(i, in, out);
        out.close();
    }

    static class TaskC {
        public void solve(int testNumber, Scanner in, PrintWriter out) {
            out.println(solve(in, out));

        }

        private long solve(Scanner in, PrintWriter out) {
            long n = in.nextInt();
            long m = in.nextInt();
            if (m >= n / 2) {
                return n * (n + 1) / 2 - (n - m);
            }
            long r = n - m;
            long k = r / (m + 1);
            long z = r - k * (m + 1);
            long res = n * (n + 1) / 2;
            return res - k * (k + 1) / 2 * (m + 1 - z) - (k + 1) * (k + 2) / 2 * z;
        }

    }
}

