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

public class Solution{
    static long c;
    public static void main(String[] args){
        try {
            PrintWriter out=new PrintWriter(System.out,true);
            BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
            int t = Integer.parseInt(br.readLine());
            for(int i=0;i<t;i++) {
                String[] ss = br.readLine().split(" ");

                int x = Integer.parseInt(ss[0]);
                int y = Integer.parseInt(ss[1]);
                if(y==0){
                    out.println(0);
                    continue;
                }
                if(x==y){
                    out.println(x*(x-1));
                    continue;
                }
                int p=x/y;
                int q=p-1;
                if(x%y==0){
                    q-=1;
                }
                char[] c=new char[x];
                Arrays.fill(c,'0');

                while(q<x && y>0){
                    c[q]='1';
                    q+=p;
                    y--;
                }
                int prev=-1;
                long ans=0;
                for(int j=0;j<x;j++){
                    if(c[j]=='1'){
                        ans+=1;
                        if(prev!=-1){
                            ans+=(j-prev-1);
                            ans+=(x-j-1);
                            ans+=((long)(j-prev-1))*(x-j-1);
                        }else{
                            ans+=j;
                            ans+=(x-j-1);
                            ans+=((long)j)*(x-j-1);
                        }
                        prev=j;
                    }
                }
                out.println(ans);

            }






            out.close();
        } catch (Exception e) {
            System.out.println("kkkk "+ e.getMessage());
        }

    }

    static void trav(List<List<Integer>> list,int i,int j){
        Stack<Integer> stack=new Stack<>();
        boolean[] vis=new boolean[list.size()];
        stack.push(i);
        vis[i]=true;
        boolean bb=false;
        while(stack.size()>0){
            int p =stack.peek();
            int s =stack.size();
            for(int k=0;k<list.get(p).size();k++){
                if(!vis[list.get(p).get(k)]){

                    stack.push(list.get(p).get(k));
                    vis[list.get(p).get(k)]=true;
                    if(list.get(p).get(k)==j){
                        bb=true;
                    }
                    break;
                }
            }
            if(bb){
                break;
            }
            if(stack.size()!=s+1){
                stack.pop();
            }
        }
        int a=stack.pop();
        while(stack.size()>0){
            int b=stack.pop();
            list.get(a).remove(new Integer(b));
            list.get(b).remove(new Integer(a));
            a=b;
        }
    }


    static void shuffle(int[] a) {
        Random get = new Random();
        for (int i = 0; i < a.length; i++) {
            int r = get.nextInt(a.length);
            int temp = a[i];
            a[i] = a[r];
            a[r] = temp;
        }
    }
    static int lcm(int a,int b){
        long c=a*b;
        return (int)(c/hcf(a,b));
    }
    static int hcf(int a,int b){
        if(a==0){
            return b;
        }
        if(b==0){
            return a;
        }
        if(a>b)
            return hcf(a%b,b);

        return hcf(a,b%a);
    }
    static void dfs(List<List<Integer>> list,boolean[] vis,int i){
        vis[i]=true;

        for(int j=0;j<list.get(i).size();j++){
            if(!vis[list.get(i).get(j)]){
                dfs(list,vis,list.get(i).get(j));
                c++;
            }
        }
    }
    static int modInverse(int x,int m){
        return power(x,m-2,m);
    }
    static int power(int x, int y, int m)
    {
        if (y == 0)
            return 1;

        long p = power(x, y / 2, m) % m;
        p = (p * p) % m;

        return (int)((y%2==0)? p : (x*p)%m);
    }
    static class pair{
        int a,b;
        public pair(int a,int b){
            this.a=a;
            this.b=b;
        }
    }
}