        import java.util.*;
    import java.io.*;
    public class Test
    {
        
   

        public static void main(String[] args) throws Exception {   
            BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
             int t = Integer.parseInt(in.readLine());
            while(t-- > 0){
                String s[] = in.readLine().split(" ");
                long n = Long.parseLong(s[0]);
                long m = Long.parseLong(s[1]);
                long total = (n*(n+1))/2;
                long zeros = n-m;
                long ones = m;
                if(zeros <= ones){
                    System.out.println(total-zeros);
                    continue;
                }
                // 000101010 000000
                // total -= (ones+1);
                zeros -= (ones+1);
                long each = zeros/(ones+1)+1;
                total -= ((each*(each+1))/2)*(ones+1);
                long remain = zeros%(ones+1);
                total+= ((each*(each+1))/2)*(remain);
                each++;
                total -= ((each*(each+1))/2)*(remain);

                System.out.println(total);
            }
        } 
    }