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

public class Main {


    public static void main(String[] args) throws IOException {
//        br = new BufferedReader(new FileReader(new File("cutting.in")));
//        out = new PrintWriter(new File("cutting.out"));
        br = new BufferedReader(new InputStreamReader(System.in));
        out = new PrintWriter(System.out);
        int PUTEN = nextInt();
        for (int POMOGI = 0; POMOGI < PUTEN; POMOGI++) {
            long n = nextInt();
            long m = nextInt();
            long sum = (n + 1) * (n / 2);
            if (n % 2 == 1) sum += n / 2 + 1;
            long k = n / (m + 1);
            long ans = 0;
            long sum_for_k = (k + 1) * (k / 2);
            if (k % 2 == 1) sum_for_k += k / 2 + 1;
            if (k == 1) sum_for_k = 1;
            if(k == 0)sum_for_k = 0;
            ans += sum_for_k * m;
            ans -= (sum - sum_for_k);
            ans += (n - k) * n;
            ans += (n - k);
            out.println(ans);
        }
        out.close();
    }

    static BufferedReader br;
    static PrintWriter out;
    static StringTokenizer in = new StringTokenizer("");

    static String next() throws IOException {
        while (!in.hasMoreTokens()) {
            in = new StringTokenizer(br.readLine());
        }
        return in.nextToken();
    }

    static int nextInt() throws IOException {
        return Integer.parseInt(next());
    }

    static long nextLong() throws IOException {
        return Long.parseLong(next());
    }

    static double nextDouble() throws IOException {
        return Double.parseDouble(next());
    }

    static int gcd(int a, int b) {
        if (b == 0) return a;
        return gcd(b, a % b);
    }
}
