import sys


T = input()


def summ(n):
	return (n*(n + 1)/2)





for case in range(T):

	n, m = map(int, sys.stdin.readline().split())

	if m >= float(n)/2:
		A = summ(n) - (n - m)
	else:
		# the number of compartments
		c = m + 1
		# splitting the zeros into compartments
		z = n - m
		f_l = z/c
		f_u = f_l + 1

		# calculate how many of f_l and f_u there need 
		n_f_u = z % f_l
		n_f_l = c - n_f_u

		# calculating A
		A = n*(n + 1)/2 - ((f_l + 1)*(f_l + 2)/2)*n_f_u + (f_l*(f_l + 1)/2)*n_f_l

	print (int(A))




	
