'''
Hey  why peeping here -_'_- ?
I believe on myself and I will achieve
this->author = Fuad Ashraful Mehmet, CSE ,University of Asia Pacific
Todo:
'''
import sys
input=sys.stdin.readline
from itertools import groupby
R=lambda:map(int,input().split())
I=lambda:int(input())
S=lambda:input().rstrip('\n')
L=lambda:list(R())

def fun(x):
    if x<=0:
        return 0
    return x*(x+1)>>1
def HalfDead():
    n,m=R()
    n+=1
    m+=1
    x,rem=divmod(n,m) 
    
    zero=(m-rem)*fun(x-1)+rem*fun(x)
    
    print(fun(n-1)-zero)
if __name__=='__main__':
    for _ in range(I()):
        HalfDead()