import sys

def input():
    return sys.stdin.readline().strip()

t = int(input())

for _ in range(t):
    n,m = list(map(int,input().split()))
    ans = (n*(n+1)//2)
    if m == 0:
        print(0)
    elif (n-m)<=(n+1)//2:
        print(ans-(n-m))
    else:
        zeros = n-m
        blockcnt = m+1
        blocksize = zeros//blockcnt
        rest = zeros%blockcnt
        smallblocks = blockcnt-rest
        bigblocks = blockcnt-smallblocks
        ans -= smallblocks*((blocksize*(blocksize+1))//2)
        ans -= bigblocks*(((blocksize+1)*(blocksize+2))//2)
        print(ans)

