import java.util.*;

public class JavaApplication78 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)  {
        // TODO code application logic here
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
      
        
       
        while(t--!=0){
           int n=sc.nextInt();
           int mid=(n+1)/2;
         
         long sum=0;
           int m=sc.nextInt();
           for(int i=0;i<m;i++)
           {
               if(mid>1)
               {
                //System.out.println("if"+mid+" "+n);   
                sum=sum+2*(n-1); 
                n=n-2;
                mid--;
               }
               else
               {//System.out.println("esle"+mid+" "+n);   
                   sum=sum+1;
               }   
           }
           System.out.println(sum);
           
           
    }
    }

}