# from debug import debug
import sys
from math import log2, ceil
input = sys.stdin.readline

t = int(input())
while t:
	t-=1
	n, m = map(int, input().split())
	if m == 0:
		print(0)
	elif m >= n//2:
		print(n*(n+1)//2 - (n-m))
	else:
		q = (n-m)//(m+1)
		r = (n-m)%(m+1)
		print(n*(n+1)//2 - r*((q+1)*(q+2)//2) - (m+1-r)*(q*(q+1)//2))