from sys import stdin, stdout
raw_input = stdin.readline
pr = stdout.write
for t in range(input()):
    n,m=map(int,raw_input().split())
    n0=n-m
    n1=m
    ans=(n*(n+1))/2
    if n1>=n0-1:
        pr(str(ans-n0)+'\n')
        continue
    qu=(n0)/(n1+1)
    rem=(n0)%(n1+1)
    x=n1+1
    x1=((qu+1)*(qu))/2
    x2=(qu*(qu-1))/2
    ans-=((rem)*x1)+((x-rem)*x2)
    ans-=n0
    pr(str(ans)+'\n')
    
