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


public class  Main{
	static long getSum(long i) {
		return (i*1l*(i+1))/2;
	}
	public static void main(String[] args) throws Exception{
		MScanner sc=new MScanner(System.in);
		PrintWriter pw=new PrintWriter(System.out);
		int tc=sc.nextInt();
		while(tc-->0) {
			long n=sc.nextInt(),m=sc.nextInt();
			if(m==0) {
				pw.println(0);
				continue;
			}
			if(m<n/2) {
				long zeros=n-m;
				long cuts=m+1;
				long groups=(zeros+cuts-1)/cuts;
				long ans=(getSum(m))*(groups+1)*(groups+1);
				pw.println(ans);
				continue;
			}
			long mid=Math.min(m, n/2);
			long ans=4l*getSum(mid);
			ans-=2l*mid;
			
			long rem=Math.max(0, m-mid);
			ans+=rem;
			long portion=mid*2l;
			long remPortion=Math.max(0, n-portion);
			ans+=(2l*mid)*remPortion;
			
			pw.println(ans);
		}
		pw.flush();
	}
	static class MScanner {
		StringTokenizer st;
		BufferedReader br;
		public MScanner(InputStream system) {
			br = new BufferedReader(new InputStreamReader(system));
		}
 
		public MScanner(String file) throws Exception {
			br = new BufferedReader(new FileReader(file));
		}
 
		public String next() throws IOException {
			while (st == null || !st.hasMoreTokens())
				st = new StringTokenizer(br.readLine());
			return st.nextToken();
		}
		public int[] takearr(int n) throws IOException {
	        int[]in=new int[n];for(int i=0;i<n;i++)in[i]=nextInt();
	        return in;
		}
		public long[] takearrl(int n) throws IOException {
	        long[]in=new long[n];for(int i=0;i<n;i++)in[i]=nextLong();
	        return in;
		}
		public Integer[] takearrobj(int n) throws IOException {
	        Integer[]in=new Integer[n];for(int i=0;i<n;i++)in[i]=nextInt();
	        return in;
		}
		public Long[] takearrlobj(int n) throws IOException {
	        Long[]in=new Long[n];for(int i=0;i<n;i++)in[i]=nextLong();
	        return in;
		}
		public String nextLine() throws IOException {
			return br.readLine();
		}
 
		public int nextInt() throws IOException {
			return Integer.parseInt(next());
		}
 
		public double nextDouble() throws IOException {
			return Double.parseDouble(next());
		}
 
		public char nextChar() throws IOException {
			return next().charAt(0);
		}
 
		public Long nextLong() throws IOException {
			return Long.parseLong(next());
		}
 
		public boolean ready() throws IOException {
			return br.ready();
		}
 
		public void waitForInput() throws InterruptedException {
			Thread.sleep(3000);
		}
	}
}