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

public class Sample
{

	static int[][][] dp;
	static int[] are;

	public static void main(String[] args) throws InterruptedException {
		Thread t=new Thread(null, null,"", 1<<28) {
			public void run() {
				m();
			}
		};
		t.start();
		t.join();
	}

	public static void m()
	{
		FastScanner in=new FastScanner();
		int t = in.nextInt();
		while(t-->0)
		{
			int n = in.nextInt();
			int m = in.nextInt();
			//
			if(m==0)
			{
				System.out.println(0);
				continue;
			}
			//
			mumbaiGay(n, m);
		}
	}

	static void mumbaiGay(int n, int m)
	{

		long total = n*(long)(n+1)/2;
		long zeroes = n-m;
		long groups = m+1;
		long small = zeroes/groups;
		long big = small+1;
		long big_count = zeroes%groups;
		long small_count = groups-big_count;
		//
		long waysS = small_count*small*(long)(small+1)/2;
		long waysB = big_count*big*(long)(big+1)/2;
		//
		System.out.println(total-waysS-waysB);
	}

	static void longestsub(char[] str1, char[] str2)
	{
		int n = str1.length;
		int m = str2.length;
		//
		int[][] dp = new int[n+1][m+1];
		//
		for(int i=0; i<=n; i++)
		{
			for(int j=0; j<=m; j++)
			{
				if(i==0 || j==0) {dp[i][j]=0; continue;}
				//
				if(str1[i-1]==str2[j-1]) dp[i][j]=dp[i-1][j-1]+1;
				else dp[i][j] = Math.max(dp[i-1][j], dp[i][j-1]);
			}
		}
		//
		int length = dp[n][m];
		//
		String ans = "";
		//
		int i=n, j=m;
		while(true)
		{
			if(i==0 || j==0) break;
			//
			if(dp[i][j]==Math.max(dp[i-1][j], dp[i][j-1]))
			{
				if(dp[i-1][j]>dp[i][j-1]) i--;
				else j--;
			}
			else
			{
				ans = str1[i-1] + ans;
				i--; j--;
			}
		}
		//
		System.out.println(length);
		System.out.println(ans);
	}

	static void print(String[] are)
	{
		for(int i=0; i<are.length; i++)
			System.out.print(are[i]);
		System.out.println();
	}

	static void sort(int[] a)
	{
		ArrayList<Integer> list=new ArrayList<>();
		for (int i:a) list.add(i);
		Collections.sort(list);
		for (int i=0; i<a.length; i++) a[i]=list.get(i);
	}

	static class FastScanner
	{
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st=new StringTokenizer("");

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

		}

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

		public int[] readArray(int n) {
			int[] a=new int[n];
			for (int i=0; i<n; i++) a[i]=nextInt();
			return a;
		}

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

		public long[] readLongArray(int n) {
			long[] a=new long[n];
			for (int i=0; i<n; i++) a[i]=nextLong();
			return a;
		}
	}

}
