from sys import *
from collections import *
from math import *

def ii(): return int(stdin.readline())
def fi(): return float(stdin.readline())
def mi(): return map(int, stdin.readline().split())
def fmi(): return map(float, stdin.readline().split())
def li(): return list(mi())
def lsi():
	x = li()
	x.pop()
	return x
def si(): return stdin.readline()


def get(a, d, n):
	return n * (2 * a + (n - 1) * d) // 2
	
tc = ii()
for tcase in range(tc):
	n, m = mi()
	if m is 0:
		print(0)
		continue
	p = n - m
	cnt = p // (m + 1)
	p -= cnt
	sum1 = get(m, 1, p)
	sum2 = get(m * 2, m, cnt)
	sum3 = get(1, 1, m)
	ans = sum1 + sum2 + sum3
	print(ans)