import java.io.*;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.*;


public class Equal {
    static class pair {
        int v;
        long w;

        public pair(int v, long w) {
            this.v = v;
            this.w = w;
        }


    }

    static class tri implements Comparable<tri> {
        int v, i, j;

        public tri(int v, int i, int j) {
            this.v = v;
            this.i = i;
            this.j = j;
        }

        @Override
        public int compareTo(tri o) {

            return v - o.v;
        }
    }

    static class Node {
        int i;
        long cost;
        int[] child;

        public Node(int ii, long c) {
            i = ii;
            cost = c;
            child = new int[66];
        }
    }

    static ArrayList<pair>[] tree;
    static Node[] nodes;
    static boolean vis[];
    static long sum = 0;

    static void dfs(int u, int p) {
        for (int i = 0; i < tree[u].size(); i++) {
            pair v = tree[u].get(i);
            if (v.v == p)
                continue;

            dfs(v.v, u);
            nodes[u].child[0]++;
            long c = v.w;

            for (int j = 1; j < 66 && c > 0; j++) {
                if (c % 2 == 1)
                    nodes[u].child[j]++;
                c /= 2;
            }
            for (int j = 0; j < 66; j++) {
                nodes[u].child[j] += nodes[v.v].child[j];
            }
            c = v.w;
            for (int j = 1; j < 66; j++) {
                if (c % 2 == 0) {
                    sum += pow(2, j - 1) * nodes[v.v].child[j];
                } else {
                    sum += pow(2, j - 1) * (nodes[v.v].child[0] - nodes[v.v].child[j]);
                }
                c /= 2;
            }
        }
    }

    static long pow(long i, long e) {
        long res = 1;
        while (e-- > 0) {
            res *= i;
        }
        return res;
    }

    public static void main(String[] args) throws IOException {
        //BufferedReader br = new BufferedReader(new FileReader("name.in"));
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st;
        PrintWriter out = new PrintWriter(System.out);
        int T=Integer.parseInt(br.readLine());
        while(T-->0){
            st=new StringTokenizer(br.readLine());
            int n=Integer.parseInt(st.nextToken());
            int m=Integer.parseInt(st.nextToken());
            int mod=(n-m)%(m+1);
            long x=(n-m)/(m+1);
            long delta=-1-x+(x+2)*mod;
            long ans=(x+2)*(x+2)*((1l*(mod)*(mod-1))/2);
            long min=Math.min(m,(m+1)-mod);
            ans+=(x+1)*(x+1)*(min*(min+1))/2;
            if(mod!=0)
            ans+=delta*(x+1)*(min);
            out.println(ans);
        }
        out.flush();
        out.close();

    }


}