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

public class Function {
    public static void main(String[] args) {
        MyScanner s = new MyScanner();
        int cases = s.nextInt();
        for (int i = 0; i < cases; i++) {
            int n = s.nextInt();
            int m = s.nextInt();
            long total = n * (n+1) / 2L;
            long x = (n-m) / (m+1L);
            long y = (n-m) - (m+1L) * x;
            long p1 = (m - y + 1) * (x + 1) * x / 2L;
            long p2 = y * (x + 2) * (x + 1) / 2L;
            System.out.println(total - (p1 + p2));
        }

    }
    public static class MyScanner {
        BufferedReader br;
        StringTokenizer st;

        public MyScanner() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

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

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

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

        String nextLine(){
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }

    }
}
