import sys
input=sys.stdin.readline
for _ in range(int(input())):
	N,M=map(int,input().split())
	if M==0:
		print(0)
	else:
		if N==M:
			print(N*(N+1)//2)
		else:
			Partitions=M+1
			Extra=0
			if (N-M)%(M+1)==0:
				SizeOfPartitions=(N-M)//(M+1)
			else:
				SizeOfPartitions=(N-M)//(M+1)
				Extra=(N-M)%(M+1)
			print(N*(N+1)//2-Extra*((SizeOfPartitions+1)*(SizeOfPartitions+2)//2)-(Partitions-Extra)*(SizeOfPartitions)*(SizeOfPartitions+1)//2)

