import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Problem619C {
    public static void main(String[] args) throws Exception {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        int testCases = Integer.parseInt(reader.readLine());
        for (int t = 0; t < testCases; t++) {
            StringTokenizer tokenizer = new StringTokenizer(reader.readLine());
            long n = Long.parseLong(tokenizer.nextToken());
            long m = Long.parseLong(tokenizer.nextToken());
            long numOnes = m;
            long numZeroes = n - m;
            if (numZeroes <= numOnes + 1) {
                System.out.println(f(n) - numZeroes);
            } else {
                long numGroups = numOnes + 1;
                if (numZeroes % numGroups == 0) {
                    // divides evenly!
                    System.out.println(f(n) - numGroups * f(numZeroes / numGroups));
                } else {
                    // doesn't divide evenly
                    long numZeroesInSmallGroups = numZeroes / numGroups;
                    long numZeroesInBigGroups = numZeroesInSmallGroups + 1;
                    long numBigGroups = numZeroes % numGroups;
                    long numSmallGroups = numGroups - numBigGroups;
                    long answer = f(n) - (numSmallGroups * f(numZeroesInSmallGroups) + numBigGroups * f(numZeroesInBigGroups));
                    System.out.println(answer);
                }
            }
        }
    }
    public static long f(long x) {
        return x * (x + 1) / 2;
    }
}
