def getCnts(l): #number of substrings within string of length l
    return ((l+1)*l)//2

def getAns(n,m):
    #distribute n-m into m+1 groups
    smaller=(n-m)//(m+1)
    smallerCnts=smaller*m+smaller+2*m+1-n
    larger=smaller+1
    largerCnts=m+1-smallerCnts
    ans=getCnts(n)-smallerCnts*getCnts(smaller)-largerCnts*getCnts(larger)
#    print('s:{} sc:{} l:{} lc:{}'.format(smaller,smallerCnts,larger,largerCnts))
    return ans
    


import sys
input=sys.stdin.buffer.readline #FOR READING PURE INTEGER INPUTS (space separation ok)

t=int(input())
for _ in range(t):
    n,m=[int(x) for x in input().split()]
    print(getAns(n,m))