import sys
for _ in range(int(sys.stdin.readline())):
    n, m = map(int,sys.stdin.readline().split())
    r=n-m
    if m==0:
        print(0)
    else:
        v=r%(m+1)
        k=r//(m+1)
        c=v*((k+1)*k//2 +k+1) +(m+1-v)*(k*(k-1)//2+k)
        print(n*(n-1)//2+n-c)
        