
import sys
def fastio():
    from io import StringIO
    from atexit import register
    global input
    sys.stdin = StringIO(sys.stdin.read())
    input = lambda : sys.stdin.readline().rstrip('\r\n')
    sys.stdout = StringIO()
    register(lambda : sys.__stdout__.write(sys.stdout.getvalue()))
fastio()
for _ in range(int(input())):
    n,m=list(map(int,input().split()))
    l=n if m==0 else (n-2*m if m<=n//2 else 0)
    s1=n-m-l
    if l==n:
        print(0)
    else:
        p=(n-m)//(m+1);r=(n-m)%(m+1);k=(n*(n+1))//2-m*(p*(p+1)//2)-(p+r)*(p+r+1)//2
        print(k)