import sys
for _ in range(int(sys.stdin.readline())):
    n, m = map(int,sys.stdin.readline().split())
    ans=n*(n+1)//2
    x=(n-m)//(m+1)
    y=(n-m)%(m+1)
    ans-=(y*(x+2)*(x+1)//2+(m+1-y)*(x+1)*(x)//2)
    print(ans)