from sys import stdin
input=stdin.readline
t=int(input())
for i in range(t):
    n,m=list(map(int,input().split()))
    nd=n//2
    mi=m+1
    diff=n-m
    if(m==0):
        print(0)
        continue
    if(m>=nd):
        k=(n**2-n)/2+m
        print(int(k))
    else:
        a,b=diff//mi,diff%mi
        print(int(((n*(n+1))/2-b*((a+1)*(a+2)//2)-(mi-b)*(a*(a+1)//2))))