import sys

lines = list(sys.stdin.readlines())
t = int(lines[0])

def count(x):
    return x * (x+1)//2

for i in range(t):
    n, ones = map(int, lines[i+1].split())
    zeros = n - ones
    
    if zeros==0:
        print(count(n))
        continue
    used_ones = min(ones, zeros-1)
    groups = used_ones+1
    
    smal_size = zeros//groups
    larg_size = smal_size + 1
    larg_count = zeros%groups
    
    total = count(n)
    total -= count(smal_size) * (groups-larg_count)
    total -= count(larg_size) * larg_count
    
    print(total)
    
