import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author prem_cse
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        FastReader in = new FastReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        CAyoubsFunction solver = new CAyoubsFunction();
        solver.solve(1, in, out);
        out.close();
    }

    static class CAyoubsFunction {
        public void solve(int testNumber, FastReader sc, PrintWriter out) {
            int t = sc.nextInt();
            while (t-- > 0) {
                long n = sc.nextLong();
                long m = sc.nextLong();
                long diff = n - m;
                long ans = ((n + 1) * n) / 2;
                if (m == 0) {
                    out.println(0);
                } else {
                    long q = diff / (m + 1);
                    long rem = diff % (m + 1);

                    ans -= rem * ((q + 1) * (q + 2)) / 2;
                    ans -= (m + 1 - rem) * ((q + 1) * (q)) / 2;
                    out.println(ans);

                }
            }
        }

    }

    static class FastReader {
        BufferedReader br;
        StringTokenizer st;

        public FastReader(InputStream stream) {
            br = new BufferedReader(new InputStreamReader(stream), 32768);
            st = null;
        }

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

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

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

    }
}

