"""
    Author : thekushalghosh
    Team   : CodeDiggers
"""
import sys,math
input = sys.stdin.readline
############ ---- USER DEFINED INPUT FUNCTIONS ---- ############
def inp():
    return(int(input()))
def inlt():
    return(list(map(int,input().split())))
def insr():
    s = input()
    return(s[:len(s) - 1])
def invr():
    return(map(int,input().split()))
################################################################
############ ---- THE ACTUAL CODE STARTS BELOW ---- ############
t = 1
t = inp()
def qw(n):
    return(n * (n + 1) // 2)
for tt in range(t):
    n,m = invr()
    q = n - m
    if m == 0:
        print(0)
        continue
    ww = (n * (n + 1) // 2)
    w = q % (m + 1)
    qq = q // (m + 1)
    ww = ww - (w * qw(qq + 1)) - ((m + 1 - w) * qw(qq))
    print(ww)