from sys import stdin
input=stdin.readline
import math
#import bisect
#i = bisect.bisect_left(a, k)
#list=input().split(maxsplit=1)
for xoxo in range(1):
    def s(x):
        return ((x+1)*x)//2
    for _ in range (int(input())):
        b=[]
        n,m=map(int, input().split())
        if m>=math.ceil(n/2) and m!=0:
            print(s(n)-(n-m))
        elif (m==0):
            print('0')
        else:
            k=(n-m)//(m+1)
            p=(n-m)%(m+1)
            kk=(m+1)-p
            ans=s(n)
            ans-=(s(k)*kk)
            ans-=(s(k+1)*p)
            print(ans)