"""
#If FastIO not needed, used this and don't forget to strip
#import sys, math
#input = sys.stdin.readline
"""

import os
import sys
from io import BytesIO, IOBase
import heapq as h 
from bisect import bisect_left, bisect_right

from types import GeneratorType
BUFSIZE = 8192
class FastIO(IOBase):
    newlines = 0
 
    def __init__(self, file):
        import os
        self.os = os
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None
 
    def read(self):
        while True:
            b = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()
 
    def readline(self):
        while self.newlines == 0:
            b = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()
 
    def flush(self):
        if self.writable:
            self.os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)
 
 
class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")
 
 
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")

import collections as col
import math, string

def getInts():
    return [int(s) for s in input().split()]

def getInt():
    return int(input())

def getStrs():
    return [s for s in input().split()]

def getStr():
    return input()

def listStr():
    return list(input())

MOD = 10**9+7


"""
Strings of length 1 is always M
Strings of length 2 can be maximised by having the 1s at least 1 zero apart where necessary
If M >= floor(N//2), then every substring except some of the 1s works

Otherwise, M 1s, N-M 0s. We want to split the 0s as evenly as possible

00100100

Partition the 0s into M+1 groups

000100100
f(1) = 2
f(2) = 4
f(3) = 6
f(4) = 6
f(5) = 5
f(6) = 4
f(7) = 3
f(8) = 2
f(9) = 1

001000100
f(1) = 2
f(2) = 4
f(3) = 6
f(4) = 6

00010000

max gap = ceil((N-M)/(M+1))
for x < max gap, contribution is M*x
for x >= max gap, contribution is N-x+1
so we want sum x = 1 to max_gap (M*x) + sum x = max_gap+1 to N (N-x+1)

Obviously M = 0 => 0
M = 1 =>
"""

def solve():
    N, M = getInts()
    if M == 0:
        return 0
    gap = math.ceil((N-M)/(M+1))
    ans = (M+1)*gap*(gap+1)//2 + (N-gap)*(N+1) - N*(N+1)//2
    return ans
    

    
for _ in range(getInt()):
        print(solve())