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

public class c {
  static boolean debug = false;
  public static void main(String[] args) {
    FS in = new FS(System.in);

    int numT = in.nextInt();
    for(int t=0; t<numT; t++) {
      long n = in.nextLong();
      long m = in.nextLong();

      
      long numSubstrings = choose(n, 2);
      numSubstrings += n; // have to add len 1 substrings XD
      if(debug) System.out.printf("Num substrings = %d\n", numSubstrings);

      long numBad = 0;
      long numZeroes = n-m;
      long numRanges = m + 1;
      long rangeSize = numZeroes/numRanges;

      long numLongerRanges = numZeroes%numRanges;
      long numShorterRanges = numRanges - numLongerRanges;

      numBad = numShorterRanges * choose(rangeSize, 2);
      numBad += numLongerRanges * choose(rangeSize+1, 2);
      numBad += numZeroes; // len 1 ranges
      // if(m != 0) {
      // } else {
      //   // if no 1s, then they're all bad.
      //   numBad = numSubstrings;
      // }

      long ans = numSubstrings - numBad;
      System.out.println(ans);



    }

  }

  static long choose(long n, long k) {
    long prev = 1;
    for(long c = 1; c<=k; c++) {
      prev = (prev*(n-c+1))/c;
    }
    return prev;
  }

  static class FS {
    BufferedReader in;
    StringTokenizer token;
    public FS(InputStream stream) {
      in = new BufferedReader(new InputStreamReader(stream));
    }
    public String next() {
      if(token == null || !token.hasMoreElements()) {
        try {
          token = new StringTokenizer(in.readLine());
        } catch (Exception e) {}
        return next();
      }
      return token.nextToken();
    }
    public int nextInt() {
      return Integer.parseInt(next());
    }
    public long nextLong() {
      return Long.parseLong(next());
    }
  }
}

/* 

5
3 1
3 2
3 3
4 0
5 2

4
5
6
0
12



nC2 - numberOfOnly0Substrings

So we basically do

*/