import math,sys
input=sys.stdin.readline
t=int(input())
def meth(x):
    return (x*(x+1))//2
for _ in range(t):
    n,m=list(map(int,input().split()))
    val=(n-m)//(m+1)
    pp=(n-m)%(m+1)
    # val+1 is (n-m)%(m+1) times
    # val is (m+1)-(n-m)%(m+1) times
    #print(val+1,(n-m)%(m+1))
    #print(val,(m+1)-(n-m)%(m+1))
    res=(n*(n+1))//2
    res-=pp*meth(val+1)
    res-=(m+1-pp)*meth(val)
    print(res)
