import sys
input=sys.stdin.readline
t=int(input())
for ii in range(t):
    n,m=map(int,input().split())
    ans=n*(n-1)//2+n
    if m>=n//2:
        print(ans-(n-m))
    else:
        z=n-m
        if z%(m+1)==0:
            p=z//(m+1)
            k=m+1
            print(ans-k*(p+p*(p-1)//2))
        else:
            k1=z%(m+1)
            k2=m+1-k1
            p1=z//(m+1)+1
            p2=p1-1
            print(ans-k1*(p1*(p1-1)//2+p1)-k2*(p2*(p2-1)//2+p2))
       