import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

public class C1301 {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
        StringTokenizer st;
        for (int t = Integer.parseInt(br.readLine()); t-- > 0; ) {
            st = new StringTokenizer(br.readLine());
            long n = Long.parseLong(st.nextToken());
            long m = Long.parseLong(st.nextToken());
            long total = (n * (n + 1)) / 2;
            if (m == 0) {
                out.println(0);
            } else if (m == 1) {
                long subtract = ((n / 2) * ((n / 2) + 1));
                if (n % 2 == 0) {
                    subtract -= (n / 2);
                }
                out.println(total - subtract);
            } else {
                long len0 = (n - m) / m;
                long lastLen0 = (n - m) % m;
                if (len0 == 0) {
                    out.println(total - Math.max(0, n - m));
                } else {
                    long subtract = m * ((len0 * (len0 + 1)) / 2) + ((lastLen0 * (lastLen0 + 1)) / 2);
                    out.println(total - subtract);
                }
            }
        }
        out.close();
    }
}
