Ayoub thinks that he is a very smart person, so he created a function $f(s)$, where $s$ is a binary string (a string which contains only symbols "0" and "1"). The function $f(s)$ is equal to the number of substrings in the string $s$ that contains at least one symbol, that is equal to "1".

More formally, $f(s)$ is equal to the number of pairs of integers $(l, r)$, such that $1 \leq l \leq r \leq |s|$ (where $|s|$ is equal to the length of string $s$), such that at least one of the symbols $s_l, s_{l+1}, \ldots, s_r$ is equal to "1". 

For example, if $s = $"01010" then $f(s) = 12$, because there are $12$ such pairs $(l, r)$: $(1, 2), (1, 3), (1, 4), (1, 5), (2, 2), (2, 3), (2, 4), (2, 5), (3, 4), (3, 5), (4, 4), (4, 5)$.

Ayoub also thinks that he is smarter than Mahmoud so he gave him two integers $n$ and $m$ and asked him this problem. For all binary strings $s$ of length $n$ which contains exactly $m$ symbols equal to "1", find the maximum value of $f(s)$.

Mahmoud couldn't solve the problem so he asked you for help. Can you help him? 


-----Input-----

The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^5$)  — the number of test cases. The description of the test cases follows.

The only line for each test case contains two integers $n$, $m$ ($1 \leq n \leq 10^{9}$, $0 \leq m \leq n$) — the length of the string and the number of symbols equal to "1" in it.


-----Output-----

For every test case print one integer number — the maximum value of $f(s)$ over all strings $s$ of length $n$, which has exactly $m$ symbols, equal to "1".


-----Example-----
Input
5
3 1
3 2
3 3
4 0
5 2

Output
4
5
6
0
12



-----Note-----

In the first test case, there exists only $3$ strings of length $3$, which has exactly $1$ symbol, equal to "1". These strings are: $s_1 = $"100", $s_2 = $"010", $s_3 = $"001". The values of $f$ for them are: $f(s_1) = 3, f(s_2) = 4, f(s_3) = 3$, so the maximum value is $4$ and the answer is $4$.

In the second test case, the string $s$ with the maximum value is "101".

In the third test case, the string $s$ with the maximum value is "111".

In the fourth test case, the only string $s$ of length $4$, which has exactly $0$ symbols, equal to "1" is "0000" and the value of $f$ for that string is $0$, so the answer is $0$.

In the fifth test case, the string $s$ with the maximum value is "01010" and it is described as an example in the problem statement.

Difficulty: MEDIUM_HARD

Solutions:
import sys
import os
import time
import collections
from collections import Counter, deque
import itertools
import math
import timeit
import random

def sieve(n):
	if n < 2:
		return list()
	prime = [True for _ in range(n + 1)]
	p = 3
	while p * p <= n:
		if prime[p]:
			for i in range(p * 2, n + 1, p):
				prime[i] = False
		p += 2
	r = [2]
	for p in range(3, n + 1, 2):
		if prime[p]:
			r.append(p)
	return r

def divs(n, start=1):
	divisors = []
	for i in range(start, int(math.sqrt(n) + 1)):
		if n % i == 0:
			if n / i == i:
				divisors.append(i)
			else:
				divisors.extend([i, n // i])
	return divisors

def divn(n, primes):
	divs_number = 1
	for i in primes:
		if n == 1:
			return divs_number
		t = 1
		while n % i == 0:
			t += 1
			n //= i
		divs_number *= t

def flin(d, x, default=-1):
	left = right = -1
	for i in range(len(d)):
		if d[i] == x:
			if left == -1:
				left = i
			right = i
	if left == -1:
		return (default, default)
	else:
		return (left, right)

def ceil(n, k):
	return n // k + (n % k != 0)

def ii():
	return int(input())

def mi():
	return map(int, input().split())

def li():
	return list(map(int, input().split()))

def lcm(a, b):
	return abs(a * b) // math.gcd(a, b)

def prr(a, sep=' '):
	print(sep.join(map(str, a)))

def dd():
	return collections.defaultdict(int)

def ddl():
	return collections.defaultdict(list)
input = sys.stdin.readline

def c(n):
	return n * (n + 1) // 2
res = []
for _ in range(ii()):
	(n, m) = mi()
	z = n - m
	count = m + 1
	vmin = z // count
	max_count = z - vmin * count
	min_count = count - max_count
	res.append(c(n) - c(vmin) * min_count - c(vmin + 1) * max_count)
prr(res, '\n')


import sys
n = int(input())
for _ in range(n):
	(n, m) = map(int, sys.stdin.readline().split())
	grp = (n - m) // (m + 1)
	ans = n * (n + 1) // 2 - grp * (grp + 1) // 2 * (m + 1) - (grp + 1) * ((n - m) % (m + 1))
	print(ans)


T = int(input())
arr = []
for i in range(T):
	(n, m) = map(int, input().split())
	k = n - m
	m += 1
	t = k // m
	kol = k - t * m
	ans = n * (n + 1) // 2 - (kol * (t + 1) ** 2 + (m - kol) * t ** 2 + k) // 2
	arr.append(ans)
print('\n'.join(map(str, arr)))


import math
import sys
tc = int(input())
while tc:
	(n, m) = map(int, sys.stdin.readline().split())
	if m == 0:
		total = 0
	else:
		total = n * (n + 1) // 2
		if m >= n // 2:
			total -= n - m
		else:
			t = n - m
			d = m + 1
			r = t % d
			x = t // d
			s = d - r
			total -= r * (x + 1) * (x + 2) // 2 + s * x * (x + 1) // 2
	print(total)
	tc -= 1


import os
import sys
from io import BytesIO, IOBase

def main():
	t = int(input())
	while t:
		t -= 1
		(n, m) = map(int, input().split())
		cnt1 = int(n - m)
		cnt2 = int(cnt1 // (m + 1))
		cnt3 = int((cnt1 + m) // (m + 1))
		left = int(cnt1 % (m + 1))
		a = int(m + 1 - left)
		print(int(n * (n + 1) // 2 - left * (cnt3 * (cnt3 + 1)) // 2 - a * (cnt2 * (cnt2 + 1)) // 2))
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	(n, m) = map(int, input().split())
	grps = m + 1
	zeach = (n - m) // grps
	extras = (n - m) % grps
	print(n * (n + 1) // 2 - zeach * (zeach + 1) // 2 * grps - extras * (zeach + 1))


from __future__ import division, print_function
import os, sys
from io import BytesIO, IOBase
if sys.version_info[0] < 3:
	from __builtin__ import xrange as range
	from future_builtins import ascii, filter, hex, map, oct, zip

def ii():
	return int(input())

def si():
	return input()

def mi():
	return map(int, input().split())

def li():
	return list(mi())

def gcd(x, y):
	while y:
		(x, y) = (y, x % y)
	return x

def read():
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
import math
mod = 1000000007

def day(d, m, y):
	t = [0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4]
	y -= m < 3
	return (y + int(y / 4) - int(y / 100) + int(y / 400) + t[m - 1] + d) % 7

def isl(y):
	if y % 400 == 0:
		return True
	elif y % 4 == 0 and y % 100 != 0:
		return True
	return False

def main():
	for i in range(ii()):
		(n, m) = mi()
		if m == 0:
			ans = 0
		else:
			tot = n * (n + 1) // 2
			s = (n - m) // (m + 1)
			b = (m + 1 - (n - m) % (m + 1)) * s * (s + 1) // 2 + (n - m) % (m + 1) * (s + 1) * (s + 2) // 2
			ans = tot - b
		print(ans)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


from sys import stdin, stdout
import math, sys, heapq
from itertools import permutations, combinations
from collections import defaultdict, deque, OrderedDict
from os import path
import random
import bisect as bi

def yes():
	print('YES')

def no():
	print('NO')
if path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')

	def I():
		return int(input())

	def In():
		return map(int, input().split())
else:

	def I():
		return int(stdin.readline())

	def In():
		return map(int, stdin.readline().split())

def dict(a):
	d = {}
	for x in a:
		if d.get(x, -1) != -1:
			d[x] += 1
		else:
			d[x] = 1
	return d

def find_gt(a, x):
	i = bi.bisect_left(a, x)
	if i != len(a):
		return i
	else:
		return -1

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

def gcd(a, b):
	if b == 0:
		return a
	return gcd(b, a % b)

def main():
	try:
		(n, m) = In()
		t = su(n)
		z = n - m
		g = m + 1
		k = z // g
		ans = su(n) - su(k) * g - (k + 1) * (z % g)
		print(ans)
	except:
		pass
M = 998244353
P = 1000000007
for _ in range(I()):
	main()


import os
import sys
from io import BytesIO, IOBase
import heapq as h
from bisect import bisect_left, bisect_right
from types import GeneratorType
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		import os
		self.os = os
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			self.os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
import collections as col
import math, string

def getInts():
	return [int(s) for s in input().split()]

def getInt():
	return int(input())

def getStrs():
	return [s for s in input().split()]

def getStr():
	return input()

def listStr():
	return list(input())
MOD = 10 ** 9 + 7

def solve():
	(N, M) = getInts()
	if M == 0:
		return 0
	gap = math.ceil((N - M) / (M + 1))
	ans = (M + 1) * gap * (gap + 1) // 2 + (N - gap) * (N + 1) - N * (N + 1) // 2
	return ans
for _ in range(getInt()):
	print(solve())


import os
import sys
from io import BytesIO, IOBase
from collections import Counter
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))

def gcd(a, b):
	if a == 0:
		return b
	return gcd(b % a, a)

def lcm(a, b):
	return a * b / gcd(a, b)

def main():
	for _ in range(int(input())):
		(n, m) = map(int, input().split())
		ans = n * (n + 1) // 2
		k = (n - m) // (m + 1)
		f = k
		k = (n - m) % (m + 1)
		ans -= (m + 1 - k) * (f * (f + 1) // 2)
		f += 1
		ans -= k * (f * (f + 1) // 2)
		print(ans)
	return
main()


import sys
for _ in range(int(sys.stdin.readline())):
	(n, m) = map(int, sys.stdin.readline().split())
	ans = n * (n + 1) // 2
	x = (n - m) // (m + 1)
	y = (n - m) % (m + 1)
	ans -= y * (x + 2) * (x + 1) // 2 + (m + 1 - y) * (x + 1) * x // 2
	print(ans)


n = int(input())
l1 = []
l2 = []
for x in range(n):
	s = str(input())
	l = s.split(' ')
	l1.append(int(l[0]))
	l2.append(int(l[1]))
l3 = []
i = 0
while i < n:
	k = l2[i] + 1
	z = l1[i] - l2[i]
	q = z // k
	r = z % k
	tz = r * ((q + 2) * (q + 1) // 2) + (k - r) * ((q + 1) * q // 2)
	l3.append(l1[i] * (l1[i] + 1) // 2 - tz)
	i = i + 1
for x in l3:
	print(x)


from sys import stdin

def iinput():
	return int(stdin.readline())

def sinput():
	return input()

def minput():
	return map(int, stdin.readline().split())

def linput():
	return list(map(int, stdin.readline().split()))
t = iinput()
while t:
	t -= 1
	(n, m) = minput()
	total = n * (n + 1) // 2
	z = n - m
	g = m + 1
	k = z // g
	ans = total - k * (k + 1) // 2 * g - (k + 1) * (z % g)
	print(ans)


from math import factorial
from collections import Counter
from heapq import heapify, heappop, heappush
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def RL():
	return map(int, sys.stdin.readline().rstrip().split())

def comb(n, m):
	return factorial(n) / (factorial(m) * factorial(n - m)) if n >= m else 0

def perm(n, m):
	return factorial(n) // factorial(n - m) if n >= m else 0

def mdis(x1, y1, x2, y2):
	return abs(x1 - x2) + abs(y1 - y2)
mod = 1000000007
INF = float('inf')
from math import ceil

def main():
	N = int(input())
	for _ in range(N):
		(n, m) = RL()
		total = n * (n + 1) // 2
		zero = n - m
		pos = m + 1
		times = zero // pos
		sy = zero % pos
		res = times * (times + 1) // 2 * pos + ceil(zero / pos) * sy
		print(int(total) - int(res))
main()


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
from math import ceil

def t(n, k):
	if k == 0:
		n += 2
		k += 2
		return int((k - 1) * n ** 2 / (2 * k))
	return (n ** 2 - n % k * ceil(n / k) ** 2 - (k - n % k) * int(n / k) ** 2) // 2
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	if k == 0:
		print(0)
		continue
	print(t(n + 1, k + 1))


import sys
input = sys.stdin.readline
t = int(input())
for i in range(t):
	(n, m) = map(int, input().split())
	z = n - m
	k = z // (m + 1)
	totalk = k * (k + 1) / 2
	g = m + 1
	output = n * (n + 1) // 2 - k * (k + 1) // 2 * g - (k + 1) * (z % g)
	print(int(output))


from sys import stdin
input = stdin.readline
for _ in range(int(input())):
	(a, b) = list(map(int, input().split(' ')))
	extra = 0
	total = a * (a + 1) // 2
	if b >= a // 2 + a % 2:
		print(total - (a - b))
	else:
		x = a - b
		v1 = (a - b) // (b + 1)
		v2 = (a - b) % (b + 1)
		total -= v2 * (v1 + 1) * (v1 + 2) // 2 + (b + 1 - v2) * (v1 + 1) * v1 // 2
		print(total)


from sys import stdin, stdout

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

def bs(be, en):
	while be < en:
		mid = (be + en) // 2
		val = m * mid
		if val >= n - (mid - 1):
			en = mid
		else:
			be = mid + 1
	return en

def arr_inp(n):
	return [int(x) for x in stdin.readline().split()]
t = int(input())
(ans, arr) = ([], [arr_inp(1) for i in range(t)])
for (n, m) in arr:
	if m == 0:
		ans.append('0')
	else:
		tem = bs(1, n)
		val = sum_range(n - tem + 1) + m * sum_range(tem - 1)
		ans.append(str(val))
print('\n'.join(ans))


import sys
get = lambda x: x * (x + 1) // 2
t = int(sys.stdin.readline())
for _ in range(t):
	(n, m) = map(int, sys.stdin.readline().split(' '))
	all = get(n)
	zeros = n - m
	len_complete_intervals = zeros // (m + 1)
	len_remained_intervals = zeros % (m + 1)
	print(all - get(len_complete_intervals) * (m + 1) - (len_complete_intervals + 1) * len_remained_intervals)


import sys
t = int(sys.stdin.readline())
for _ in range(t):
	(n, m) = map(int, sys.stdin.readline().split(' '))
	p = (n - m) // (m + 1)
	q = (n - m) % (m + 1)
	x = q * (p + 1) * (p + 2) // 2
	y = (m + 1 - q) * p * (p + 1) // 2
	res = n * (n + 1) // 2 - (x + y)
	print(res)


import sys
input = sys.stdin.readline
for l in range(int(input())):
	(n, m) = map(int, input().split(' '))
	zerosPer = (n - m) // (m + 1)
	numUpper = (n - m) % (m + 1)
	x = (m + 1 - numUpper) * zerosPer * (zerosPer + 1) // 2
	x = x + numUpper * (zerosPer + 1) * (zerosPer + 2) // 2
	print(n * (n + 1) // 2 - x)


import sys
for test in range(int(sys.stdin.readline())):
	(n, m) = [int(i) for i in sys.stdin.readline().split()]
	q = n - m
	w = m + 1
	print(-((q // w + 1) * (q % w) * (q // w + 2) // 2 + (w - q % w) * (q // w) * (q // w + 1) // 2) + n * (n + 1) // 2)


def main():
	from sys import stdin, stdout
	for _ in range(int(stdin.readline())):
		(n, m) = map(int, stdin.readline().split())
		n += 1
		m += 1
		(div, mod) = divmod(n, m)
		stdout.write(f'{(n ** 2 - div ** 2 * (m - mod) - (div + 1) ** 2 * mod) // 2}\n')
main()


from sys import stdin, stdout
import math
q = int(stdin.readline())
for x in range(q):
	(n, m) = [int(x) for x in stdin.readline().split()]
	if m == 0:
		print('0')
	else:
		temp = (n - m) / (m + 1)
		if int(math.ceil(temp)) == int(temp) and temp != 0:
			temp = int(temp)
			temp2 = temp * (temp + 1) // 2 * (m + 1)
		else:
			temp = math.ceil(temp)
			temp1 = (n - m) % (m + 1)
			temp2 = temp * (temp + 1) // 2 * temp1 + temp * (temp - 1) // 2 * (m + 1 - temp1)
		sum1 = (n + 1) * n // 2
		sum1 = sum1 - temp2
		print(int(sum1))


import sys, math
input = sys.stdin.readline

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())
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)


T = int(input())
s = ''
for i in range(T):
	(n, m) = map(int, input().split())
	k = n - m
	m += 1
	t = k // m
	kol = k - t * m
	ans = n * (n + 1) // 2 - (kol * (t + 1) ** 2 + (m - kol) * t ** 2 + k) // 2
	s += str(ans) + '\n'
print(s)


from sys import stdin, stdout, setrecursionlimit

def fn(n):
	return n * (n + 1) // 2
for _ in range(int(stdin.readline())):
	(n, k) = list(map(int, stdin.readline().split()))
	zeros = n - k
	partitions = k + 1
	zeros_per_partition = zeros // partitions
	rem = zeros % partitions
	xx = rem * fn(zeros_per_partition + 1)
	normal = (partitions - rem) * fn(zeros_per_partition)
	print(fn(n) - (xx + normal))


t = int(input())
for test in range(t):
	(n, m) = map(int, input().split())
	r = (n - m) % (m + 1)
	d = (n - m) // (m + 1)
	ans = (n - m + r) * (d + 1)
	print((n * (n + 1) - ans) // 2)


import sys

def fastio():
	from io import StringIO
	from atexit import register
	global input
	sys.stdin = StringIO(sys.stdin.read())
	input = lambda : sys.stdin.readline().rstrip('\r\n')
	sys.stdout = StringIO()
	register(lambda : sys.__stdout__.write(sys.stdout.getvalue()))
fastio()

def debug(*var, sep=' ', end='\n'):
	print(*var, file=sys.stderr, end=end, sep=sep)
INF = 10 ** 20
MOD = 10 ** 9 + 7
I = lambda : list(map(int, input().split()))
from math import gcd
from math import ceil
from collections import defaultdict as dd, Counter
from bisect import bisect_left as bl, bisect_right as br
(t,) = I()
while t:
	t -= 1
	(n, m) = I()
	total = n * (n + 1) // 2
	zero = n - m
	if m == 0:
		print(0)
		continue
	ok = zero // (m + 1)
	spread = zero % (m + 1)
	(even, odd) = (ok, ok + 1)
	print(total - odd * (odd + 1) // 2 * spread - even * (even + 1) // 2 * (m + 1 - spread))


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))


import sys
import bisect
import heapq
from math import *
from collections import Counter as counter
from itertools import combinations as comb
from itertools import permutations as permutate
from bisect import bisect_left as bl
from bisect import bisect_right as br
from bisect import bisect
mod = pow(10, 9) + 7

def inp():
	return sys.stdin.readline().strip()

def out(var):
	sys.stdout.write(str(var))

def lis():
	return list(map(int, inp().split()))

def stringlis():
	return list(map(str, inp().split()))

def sep():
	return map(int, inp().split())

def strsep():
	return map(str, inp().split())

def graph(vertex):
	return [[] for i in range(0, vertex + 1)]

def zerolist(n):
	return [0] * n

def nl():
	out('\n')

def testcase(t):
	for p in range(t):
		solve()

def printlist(a):
	for p in range(0, len(a)):
		out(str(a[p]) + ' ')

def lcm(a, b):
	return a * b // gcd(a, b)

def solve():
	(n, m) = sep()
	zeros = n - m
	if zeros == 0:
		print(n * (n + 1) // 2)
	else:
		div = zeros // (m + 1)
		rem = zeros % (m + 1)
		print(n * (n + 1) // 2 - rem * (div + 1) * (div + 2) // 2 - (m + 1 - rem) * ((div + 1) * div) // 2)
testcase(int(inp()))


import sys
input = sys.stdin.readline
Q = int(input())
Query = [list(map(int, input().split())) for _ in range(Q)]
for (N, M) in Query:
	Z = N - M
	s = M + 1
	m1 = Z // s
	m2 = m1 + 1
	ans = N * (N + 1) // 2 - m1 * (m1 + 1) // 2 * (s - Z % s) - m2 * (m2 + 1) // 2 * (Z % s)
	print(ans)


import sys
input = sys.stdin.readline
T = int(input())
for _ in range(T):
	(n, m) = list(map(int, input().split()))
	S = 0
	x = (n - m) // (m + 1)
	z = (n - m) % (m + 1)
	S += (m + 1 - z) * (x * (x + 1)) // 2
	S += z * (x + 1) * (x + 2) // 2
	print(n * (n + 1) // 2 - S)


import sys

def fastio():
	from io import StringIO
	from atexit import register
	global input
	sys.stdin = StringIO(sys.stdin.read())
	input = lambda : sys.stdin.readline().rstrip('\r\n')
	sys.stdout = StringIO()
	register(lambda : sys.__stdout__.write(sys.stdout.getvalue()))
fastio()
for _ in range(int(input())):
	(n, m) = list(map(int, input().split()))
	l = n if m == 0 else n - 2 * m if m <= n // 2 else 0
	s1 = n - m - l
	if l == n:
		print(0)
	else:
		p = (n - m) // (m + 1)
		r = (n - m) % (m + 1)
		k = n * (n + 1) // 2 - (m + 1 - r) * (p * (p + 1) // 2) - r * (p + 1) * (p + 2) // 2
		print(k)


import sys, os, io

def rs():
	return sys.stdin.readline().rstrip()

def ri():
	return int(sys.stdin.readline())

def ria():
	return list(map(int, sys.stdin.readline().split()))

def ws(s):
	sys.stdout.write(s + '\n')

def wi(n):
	sys.stdout.write(str(n) + '\n')

def wia(a):
	sys.stdout.write(' '.join([str(x) for x in a]) + '\n')
import math, datetime, functools, itertools, operator, bisect, fractions, statistics
from collections import deque, defaultdict, OrderedDict, Counter
from fractions import Fraction
from decimal import Decimal
from sys import stdout
from heapq import heappush, heappop, heapify, _heapify_max, _heappop_max, nsmallest, nlargest

def main():
	starttime = datetime.datetime.now()
	if os.path.exists('input.txt'):
		sys.stdin = open('input.txt', 'r')
		sys.stdout = open('output.txt', 'w')
	tc = ri()
	for _ in range(tc):
		(n, m) = ria()
		z = n - m
		g = m + 1
		k = z // g
		total = n * (n + 1) // 2
		part1 = z % g
		part2 = g - part1
		wrongzeroes = part1 * ((k + 1) * (k + 2)) // 2 + part2 * (k * (k + 1)) // 2
		print(total - wrongzeroes)
	endtime = datetime.datetime.now()
	time = (endtime - starttime).total_seconds() * 1000
	if os.path.exists('input.txt'):
		print('Time:', time, 'ms')

class FastReader(io.IOBase):
	newlines = 0

	def __init__(self, fd, chunk_size=1024 * 8):
		self._fd = fd
		self._chunk_size = chunk_size
		self.buffer = io.BytesIO()

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, self._chunk_size))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self, size=-1):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, self._chunk_size if size == -1 else size))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

class FastWriter(io.IOBase):

	def __init__(self, fd):
		self._fd = fd
		self.buffer = io.BytesIO()
		self.write = self.buffer.write

	def flush(self):
		os.write(self._fd, self.buffer.getvalue())
		(self.buffer.truncate(0), self.buffer.seek(0))

class FastStdin(io.IOBase):

	def __init__(self, fd=0):
		self.buffer = FastReader(fd)
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

class FastStdout(io.IOBase):

	def __init__(self, fd=1):
		self.buffer = FastWriter(fd)
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.flush = self.buffer.flush
sys.stdin = FastStdin()
sys.stdout = FastStdout()
main()


import sys
t = int(input())
for i in range(t):
	line = list(map(int, sys.stdin.readline().split()))
	n = line[0]
	m = line[1]
	rem = (n - m) % (m + 1)
	q = (n - m) // (m + 1)
	ans = n * (n - 1) // 2 + m - rem * ((q + 1) * q) // 2 - (m + 1 - rem) * (q * (q - 1) // 2)
	print(ans)


import sys

def main():
	(n, m) = map(int, sys.stdin.readline().split())
	ans = n * (n + 1)
	ans = ans // 2
	if n == m:
		print(ans)
	elif m == 0:
		print(0)
	else:
		temp = n - m
		k = temp // (m + 1)
		sub = k * (k + 1) // 2 * (m + 1) + (k + 1) * (temp % (m + 1))
		print(ans - sub)
	return

def test():
	t = int(input())
	while t:
		main()
		t -= 1
test()


import sys
read = lambda : list(map(int, sys.stdin.readline().strip().split()))
sigma = lambda x: x * (x + 1) // 2
for _ in range(int(input())):
	(n, m) = read()
	k = n - m
	total = sigma(n)
	if k > m:
		(e, f) = divmod(k, m + 1)
		total -= (m + 1 - f) * sigma(e) + f * sigma(e + 1)
	else:
		total -= k
	print(total)


import sys
lines = list(sys.stdin.readlines())
t = int(lines[0])

def count(x):
	return x * (x + 1) // 2
for i in range(t):
	(n, ones) = map(int, lines[i + 1].split())
	zeros = n - ones
	if zeros == 0:
		print(count(n))
		continue
	used_ones = min(ones, zeros - 1)
	groups = used_ones + 1
	smal_size = zeros // groups
	larg_size = smal_size + 1
	larg_count = zeros % groups
	total = count(n)
	total -= count(smal_size) * (groups - larg_count)
	total -= count(larg_size) * larg_count
	print(total)


from sys import stdin
input = stdin.readline
import math
t = int(input())

def g(x):
	return x * (x - 1) // 2 + x
for _ in range(t):
	(n, m) = map(int, input().split())
	c = n - m
	d = m + 1
	c_d = math.ceil(c / d)
	a = c_d * d - c
	b = d - a
	ans = g(n) - g(c_d - 1) * a - g(c_d) * b
	print(ans)


import sys
import os
import time
import collections
from collections import Counter, deque
import itertools
import math
import timeit
import random
from io import BytesIO, IOBase

def sieve(n):
	if n < 2:
		return list()
	prime = [True for _ in range(n + 1)]
	p = 3
	while p * p <= n:
		if prime[p]:
			for i in range(p * 2, n + 1, p):
				prime[i] = False
		p += 2
	r = [2]
	for p in range(3, n + 1, 2):
		if prime[p]:
			r.append(p)
	return r

def divs(n, start=1):
	divisors = []
	for i in range(start, int(math.sqrt(n) + 1)):
		if n % i == 0:
			if n / i == i:
				divisors.append(i)
			else:
				divisors.extend([i, n // i])
	return divisors

def divn(n, primes):
	divs_number = 1
	for i in primes:
		if n == 1:
			return divs_number
		t = 1
		while n % i == 0:
			t += 1
			n //= i
		divs_number *= t

def flin(d, x, default=-1):
	left = right = -1
	for i in range(len(d)):
		if d[i] == x:
			if left == -1:
				left = i
			right = i
	if left == -1:
		return (default, default)
	else:
		return (left, right)

def ceil(n, k):
	return n // k + (n % k != 0)

def ii():
	return int(input())

def mi():
	return map(int, input().split())

def li():
	return list(map(int, input().split()))

def lcm(a, b):
	return abs(a * b) // math.gcd(a, b)

def prr(a, sep=' '):
	print(sep.join(map(str, a)))

def dd():
	return collections.defaultdict(int)

def ddl():
	return collections.defaultdict(list)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
for _ in range(ii()):
	(n, m) = mi()
	z = n - m
	k = z // (m + 1)
	r = z % (m + 1)
	res = n * (n + 1) // 2 - k * (k + 1) * (m + 1 - r) // 2 - r * (k + 1) * (k + 2) // 2
	print(res)


import sys

def input():
	return sys.stdin.readline().strip()
t = int(input())
for _ in range(t):
	(n, m) = list(map(int, input().split()))
	ans = n * (n + 1) // 2
	if m == 0:
		print(0)
	elif n - m <= (n + 1) // 2:
		print(ans - (n - m))
	else:
		zeros = n - m
		blockcnt = m + 1
		blocksize = zeros // blockcnt
		rest = zeros % blockcnt
		smallblocks = blockcnt - rest
		bigblocks = blockcnt - smallblocks
		ans -= smallblocks * (blocksize * (blocksize + 1) // 2)
		ans -= bigblocks * ((blocksize + 1) * (blocksize + 2) // 2)
		print(ans)


from sys import stdin
input = stdin.readline
q = int(input())
for _ in range(q):
	(n, j) = map(int, input().split())
	z = n - j
	przedjed = z // (j + 1)
	cyk = z - (j + 1) * przedjed
	chuj = cyk * (przedjed + 2) * (przedjed + 1) // 2 + (j + 1 - cyk) * przedjed * (przedjed + 1) // 2
	print(n * (n + 1) // 2 - chuj)


import sys
import math

def input():
	return sys.stdin.readline().strip()

def iinput():
	return [int(x) for x in sys.stdin.readline().split()]

def vxod(q):
	return q * (q + 1) // 2

def main():
	(n, m) = iinput()
	if n - m >= m + 1:
		grupps = (n - m) // (m + 1)
		grupps_1 = (n - m) % (m + 1)
		grupps_2 = m + 1 - grupps_1
		summ = grupps_1 * vxod(grupps + 1) + grupps_2 * vxod(grupps)
		print(vxod(n) - summ)
	else:
		print(vxod(n) + m - n)
	return
for qwerty in range(int(input())):
	main()


import sys
for _ in range(int(sys.stdin.readline())):
	(n, m) = map(int, sys.stdin.readline().split())
	r = n - m
	if m == 0:
		print(0)
	else:
		v = r % (m + 1)
		k = r // (m + 1)
		c = v * ((k + 1) * k // 2 + k + 1) + (m + 1 - v) * (k * (k - 1) // 2 + k)
		print(n * (n - 1) // 2 + n - c)


def solve():
	import sys
	t = int(input())
	while t > 0:
		(n, m) = [int(x) for x in sys.stdin.readline().split()]
		g = m + 1
		z = n - m
		k = z // g
		print(n * (n + 1) // 2 - k * (k + 1) * g // 2 - (k + 1) * (z % g))
		t -= 1
solve()


import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	(N, M) = map(int, input().split())
	if M < N // 2:
		(l, k) = divmod(N - M, M + 1)
		print(N * (N + 1) // 2 - (l * (l + 1) // 2 * (M + 1 - k) + (l + 1) * (l + 2) // 2 * k))
	else:
		print(N * (N + 1) // 2 - (N - M))


import sys
input = lambda : sys.stdin.readline().rstrip()
for _ in range(int(input())):
	(n, m) = map(int, input().split())
	z = n - m
	div = z // (m + 1)
	rem = z % (m + 1)
	ans = n * (n + 1) // 2 - div * (div + 1) * (m + 1) // 2 - (div + 1) * rem
	print(ans)


import sys
input = sys.stdin.readline

def main():
	t = int(input())
	for _ in range(t):
		(n, m) = map(int, input().split())
		if m == 0:
			print(0)
		elif m == n:
			print(n * (n - 1) // 2 + n)
		else:
			p = n - m
			(r, q) = (p % (m + 1), p // (m + 1))
			res = n * (n - 1) // 2 + n
			if r != 0:
				res -= (q + 1 + (q + 1) * q // 2) * r
			if q != 0:
				res -= (q + q * (q - 1) // 2) * (m + 1 - r)
			print(res)
main()


import sys
i = list(sys.stdin.readlines())
t = int(i[0])
for __ in range(t):
	(n, m) = map(int, i[__ + 1].split())
	if m == 0:
		print(0)
		continue
	buckets = m + 1
	fl = (n - m) // buckets
	big_buckets = (n + 1) % buckets
	comp = n * (n + 1) // 2
	comp -= big_buckets * (fl + 2) * (fl + 1) // 2
	comp -= (buckets - big_buckets) * fl * (fl + 1) // 2
	print(comp)


import sys
input = sys.stdin.readline

def print(x):
	sys.stdout.write(str(x) + '\n')
for _ in range(int(input())):
	(n, m) = map(int, input().split())
	grps = m + 1
	zeach = (n - m) // grps
	extras = (n - m) % grps
	print(n * (n + 1) // 2 - zeach * (zeach + 1) // 2 * grps - extras * (zeach + 1))


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)


import sys
input = sys.stdin.readline
t = int(input())

def calc(x):
	return x * (x + 1) // 2
for test in range(t):
	(n, m) = map(int, input().split())
	ANS = calc(n)
	k = n - m
	(q, mod) = divmod(k, m + 1)
	ANS -= calc(q + 1) * mod + calc(q) * (m + 1 - mod)
	print(ANS)


import sys
input = sys.stdin.readline
t = int(input())
C = [list(map(int, input().split())) for i in range(t)]

def c(x, y):
	if x % 2 == 0:
		return x // 2 * y
	else:
		return y // 2 * x
for i in range(t):
	n = C[i][0]
	o = C[i][1]
	z = n - o
	al = c(n, n - 1) + n
	if z - 1 <= o:
		print(al - z)
	else:
		a = z // (o + 1)
		b = z % (o + 1)
		mi = c(a + 1, a + 2) * b + c(a, a + 1) * (o + 1 - b)
		print(al - mi)


import sys

def input():
	return sys.stdin.readline().strip()

def list2d(a, b, c):
	return [[c] * b for i in range(a)]

def list3d(a, b, c, d):
	return [[[d] * c for j in range(b)] for i in range(a)]

def list4d(a, b, c, d, e):
	return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]

def ceil(x, y=1):
	return int(-(-x // y))

def INT():
	return int(input())

def MAP():
	return map(int, input().split())

def LIST(N=None):
	return list(MAP()) if N is None else [INT() for i in range(N)]

def Yes():
	print('Yes')

def No():
	print('No')

def YES():
	print('YES')

def NO():
	print('NO')
INF = 10 ** 18
MOD = 10 ** 9 + 7

def nH2(n):
	return (n + 1) * n // 2
for _ in range(INT()):
	(N, M) = MAP()
	cnt1 = M
	cnt0 = N - M
	total = nH2(N)
	if cnt1 >= N // 2:
		print(total - cnt0)
		continue
	(di, mo) = divmod(cnt0, cnt1 + 1)
	dicnt = cnt1 + 1 - mo
	ans = total - (dicnt * nH2(di) + mo * nH2(di + 1))
	print(ans)


def getCnts(l):
	return (l + 1) * l // 2

def getAns(n, m):
	smaller = (n - m) // (m + 1)
	smallerCnts = smaller * m + smaller + 2 * m + 1 - n
	larger = smaller + 1
	largerCnts = m + 1 - smallerCnts
	ans = getCnts(n) - smallerCnts * getCnts(smaller) - largerCnts * getCnts(larger)
	return ans
import sys
input = sys.stdin.buffer.readline
t = int(input())
for _ in range(t):
	(n, m) = [int(x) for x in input().split()]
	print(getAns(n, m))


import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	(n, m) = list(map(int, input().split()))
	z = n - m
	x = z // (m + 1)
	print(n * (n + 1) // 2 - (m + 1 - z % (m + 1)) * (x * (x + 1) // 2) - z % (m + 1) * ((x + 1) * (x + 2) // 2))


from sys import stdin, stdout
for I in range(int(stdin.readline())):
	stdout.write((lambda n, m: str(n * (n + 1) // 2 - (m + 1 - (n - m) % (m + 1)) * ((n - m) // (m + 1) * ((n - m) // (m + 1) + 1) // 2) - (n - m) % (m + 1) * (((n - m) // (m + 1) + 1) * ((n - m) // (m + 1) + 2) // 2)) + '\n')(*map(int, stdin.readline().split())))


t = int(input())
raw = []
for _ in range(t):
	raw.append(list(map(int, input().split())))
for i in range(t):
	(n, m) = raw[i]
	p = (n - m) // (m + 1)
	q = (n - m) % (m + 1)
	ans = n * (n + 1) // 2
	ans -= (p + 1) * (p + 2) * q // 2
	ans -= p * (p + 1) * (m + 1 - q) // 2
	print(ans)


import sys
t = int(input())
while t > 0:
	(n, m) = map(int, sys.stdin.readline().split())
	p = (n - m) // (m + 1)
	ans = n * (n + 1) // 2 - (m + 1) * p * (p + 1) // 2 - (p + 1) * ((n - m) % (m + 1))
	print(ans)
	t = t - 1


import sys

def fun(x):
	return x * (x + 1) // 2
for _ in range(int(input())):
	(n, m) = map(int, sys.stdin.readline().split())
	emp = n - m
	if emp >= m + 1:
		xx = emp // (m + 1)
		if emp % (m + 1) == 0:
			times = m + 1
			print(fun(n) - fun(xx) * times)
		else:
			times = m + 1
			rem = emp % (m + 1)
			print(fun(n) - fun(xx + 1) * rem - fun(xx) * (times - rem))
	else:
		print(fun(n) - emp)


import io, os
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
t = int(input())
for i in range(t):
	(n, m) = list(map(int, input().split()))
	a = (n - m) // (m + 1)
	b = (n - m) % (m + 1)
	c = m + 1 - b
	d = a + 1
	ans = n * (n + 1) // 2
	ans -= c * (a * (a + 1) // 2)
	ans -= b * (d * (d + 1) // 2)
	print(ans)


import math
import collections
import sys

def inpu():
	return input().split(' ')

def inti(a):
	for i in range(len(a)):
		a[i] = int(a[i])
	return a

def inp():
	a = inpu()
	a = inti(a)
	return a

def solution():
	(n, m) = [int(x) for x in sys.stdin.readline().split()]
	ans = n * (n + 1) // 2
	val = (n - m) // (m + 1)
	ans -= val * (val + 1) * (m + 1) // 2
	rem = (n - m) % (m + 1)
	ans -= (val + 1) * rem
	print(ans)
for _ in range(int(input())):
	solution()


import sys
reader = (line.rstrip() for line in sys.stdin)
input = reader.__next__

def nSubarr(n):
	return n * (n + 1) >> 1
t = int(input())
for _ in range(t):
	(n, m) = map(int, input().split())
	n += 1
	m += 1
	(x, rem) = divmod(n, m)
	ctr_zero = (m - rem) * nSubarr(x - 1) + rem * nSubarr(x)
	ans = nSubarr(n - 1) - ctr_zero
	print(ans)


from sys import stdin
input = stdin.readline
t = int(input())
for i in range(t):
	(n, m) = list(map(int, input().split()))
	nd = n // 2
	mi = m + 1
	diff = n - m
	if m == 0:
		print(0)
		continue
	if m >= nd:
		k = n * (n + 1) // 2 + m - n
		print(int(k))
	else:
		(a, b) = (diff // mi, diff % mi)
		print(int(n * (n + 1) // 2 - b * ((a + 1) * (a + 2) // 2) - (mi - b) * (a * (a + 1) // 2)))


t = int(input())
zxc = []
for i in range(t):
	(n, m) = [int(k) for k in input().split()]
	res = (1 + n) * n // 2
	eta = (n - m) // (m + 1)
	zeta = (n - m) % (m + 1)

	def f(x):
		return (1 + x) * x // 2
	res -= zeta * f(eta + 1)
	res -= (m + 1 - zeta) * f(eta)
	zxc.append(res)
print('\n'.join([str(k) for k in zxc]))


import sys
input = sys.stdin.readline
for nt in range(int(input())):
	(n, m) = map(int, input().split())
	z = n - m
	s = n * (n + 1) // 2
	k1 = z // (m + 1)
	k2 = z % (m + 1)
	sub = k1 * (k1 + 1) // 2 * (m + 1 - k2)
	sub += (k1 + 1) * (k1 + 2) // 2 * k2
	print(s - sub)


import sys
for _ in range(int(sys.stdin.readline())):
	(n, m) = map(int, sys.stdin.readline().split())
	ze = n - m
	allp = n * (n + 1) // 2
	mean = ze // (m + 1)
	more = ze % (m + 1)
	allp -= (m + 1 - more) * (mean * (mean + 1) // 2) + more * ((mean + 1) * (mean + 2) // 2)
	print(allp)


from sys import stdin, stdout
t = int(stdin.readline())
while t:
	t -= 1
	(n, m) = map(int, stdin.readline().split())
	(mul, rem) = divmod(n - m, m + 1)
	ans = n * (n + 1) // 2 - (m + 1 - rem) * mul * (mul + 1) // 2 - rem * (mul + 1) * (mul + 2) // 2
	stdout.write(str(ans) + '\n')


import sys
input = sys.stdin.readline
out = sys.stdout
for _ in range(int(input())):
	(n, m) = map(int, input().split())
	a = (n - m) // (m + 1)
	b = (n - m) % (m + 1)
	a1 = (m + 1 - b) * (a * (a + 1)) // 2
	b1 = b * ((a + 1) * (a + 2)) // 2
	ans = n * (n + 1) // 2 - a1 - b1
	out.write(str(ans) + '\n')


import sys

def input():
	return sys.stdin.readline().rstrip()
t = int(input())
for _ in range(t):
	(n, m) = map(int, input().split())
	per = n * (n + 1) // 2
	count0 = n - m
	nG = m + 1
	k = count0 // nG
	rem = count0 // nG
	val = per - rem * (rem + 1) // 2 * nG - (k + 1) * (count0 % nG)
	print(val)


import sys

def calc(a0, diff, n):
	return n * (2 * a0 + (n - 1) * diff) // 2

def go(n, m):
	if n // 2 <= m:
		ans = n * (n + 1) // 2
		ans -= n - m
		return ans
	elif m == 0:
		return 0
	else:
		k = (n - m) // (m + 1)
		r = (n - m) % (m + 1)
		begin = (k + 1) * calc(n - k, -(k + 1), m + 1 - r)
		end = (k + 2) * calc((k + 2) * (r - 1), -(k + 2), r - 1)
		return begin + end
t = int(sys.stdin.readline())
for _ in range(t):
	(n, m) = map(int, sys.stdin.readline().split())
	print(go(n, m))


from sys import stdin, stdout
for _ in range(int(stdin.readline())):
	(n, m) = map(int, stdin.readline().split())
	z = n - m
	c = z // (m + 1)
	o = z % (m + 1)
	stdout.write(str(n * (n + 1) // 2 - o * (c + 1) * (c + 2) // 2 - (m + 1 - o) * c * (c + 1) // 2) + '\n')


import sys
import math as mt
input = sys.stdin.buffer.readline
t = int(input())
for __ in range(t):
	(n, m) = map(int, input().split())
	k = (n - m) // (m + 1)
	ans = n * (n + 1) // 2 - k * (k + 1) // 2 * (m + 1)
	ans -= (n - m) % (m + 1) * (k + 1)
	print(ans)


import sys
input = sys.stdin.readline
import math
import copy
import collections
from collections import deque
for _ in range(int(input())):
	(n, m) = map(int, input().split())
	z = n - m
	o = m
	val = z // (m + 1)
	num = z % (m + 1)
	ans = math.comb(n + 1, 2) - math.comb(val + 2, 2) * num - math.comb(val + 1, 2) * (m + 1 - num)
	print(ans)


def Sol(n, m):
	cant = n * (n + 1) // 2
	z = n - m
	k = z // (m + 1)
	cant -= (m + 1) * k * (k + 1) // 2
	cant -= z % (m + 1) * (k + 1)
	return int(cant)
t = int(input())
while t > 0:
	aux = input()
	(n, m) = aux.split(' ')
	print(Sol(int(n), int(m)))
	t -= 1


import sys
input = sys.stdin.readline

def calc(x):
	return x * (x + 1) // 2
t = int(input())
for _ in range(t):
	(n, m) = map(int, input().split())
	zeros = n - m
	x = zeros // (m + 1)
	r = zeros % (m + 1)
	print(calc(n) - calc(x) * (m + 1 - r) - calc(x + 1) * r)


import sys
import bisect
from collections import deque

def no():
	print('NO')

def dist(z, c):
	return (z[0] - c[0]) * (z[0] - c[0]) + (z[1] - c[1]) * (z[1] - c[1])

def vec(z, c):
	return z[0] * c[1] - z[1] * c[0]

def pro(z, c):
	return [z[0] - c[0], z[1] - c[1]]

def main():
	(n, m) = [int(i) for i in sys.stdin.readline().split()]
	print(n * (n + 1) // 2 - ((n - m) % (m + 1) * ((n - m) // (m + 1) + 1) * ((n - m) // (m + 1) + 2) // 2 + (m + 1 - (n - m) % (m + 1)) * ((n - m) // (m + 1)) * ((n - m) // (m + 1) + 1) // 2))
for i in range(int(sys.stdin.readline())):
	main()


import sys
import math
from collections import defaultdict, Counter
input = sys.stdin.readline

def print(x):
	sys.stdout.write(str(x) + '\n')
t = int(input())
for i in range(t):
	(n, m) = map(int, input().split())
	if m == 0:
		print(0)
	elif n - m <= m:
		print(n * (n + 1) // 2 - (n - m))
	else:
		d = n - m
		k = d // (m + 1)
		d1 = d % (m + 1)
		print(n * (n + 1) // 2 - (m + 1) * (k * (k + 1) // 2) - (k + 1) * d1)


import sys
input = sys.stdin.readline
from itertools import groupby
R = lambda : map(int, input().split())
I = lambda : int(input())
S = lambda : input().rstrip('\n')
L = lambda : list(R())

def fun(x):
	if x <= 0:
		return 0
	return x * (x + 1) >> 1

def HalfDead():
	(n, m) = R()
	n += 1
	m += 1
	(x, rem) = divmod(n, m)
	zero = (m - rem) * fun(x - 1) + rem * fun(x)
	print(fun(n - 1) - zero)
for _ in range(I()):
	HalfDead()


from sys import stdin, stdout
T = int(input())

def summ(n):
	return n * (n + 1) // 2
for case in range(T):
	(n, m) = map(int, stdin.readline().split())
	if m >= n / 2:
		A = summ(n) - (n - m)
	else:
		c = m + 1
		f = (n - m) // c
		f_u = f + 1
		n_f_u = (n - m) % c
		n_f_l = c - n_f_u
		A = summ(n) - (summ(f_u) * n_f_u + summ(f) * n_f_l)
	print(int(A))


import sys
input = sys.stdin.readline
t = int(input())
for ii in range(t):
	(n, m) = map(int, input().split())
	ans = n * (n - 1) // 2 + n
	if m >= n // 2:
		print(ans - (n - m))
	else:
		z = n - m
		if z % (m + 1) == 0:
			p = z // (m + 1)
			k = m + 1
			print(ans - k * (p + p * (p - 1) // 2))
		else:
			k1 = z % (m + 1)
			k2 = m + 1 - k1
			p1 = z // (m + 1) + 1
			p2 = p1 - 1
			print(ans - k1 * (p1 * (p1 - 1) // 2 + p1) - k2 * (p2 * (p2 - 1) // 2 + p2))


import os
import operator
import sys
from io import BytesIO, IOBase

def inar():
	return [int(k) for k in input().split()]

def main():
	for _ in range(int(input())):
		(n, m) = inar()
		total = n * (n + 1) // 2
		zeros = n - m
		groups = m + 1
		mn = zeros // groups
		print(total - mn * (mn + 1) // 2 * groups - (mn + 1) * (zeros % groups))
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


from sys import stdin, stdout
input = stdin.readline
pr = stdout.write
for _ in range(int(input())):
	(n, m) = map(int, input().split())
	k = n - m
	a = k // (m + 1)
	b = k % (m + 1)
	pr(str(n * (n + 1) // 2 - (m + 1 - b) * (a * (a + 1) // 2) - b * ((a + 1) * (a + 2) // 2)) + '\n')


import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
z = int(readline())
for _ in range(z):
	(n, m) = map(int, readline().split())
	G = m + 1
	Z = n - m
	(k, rest) = divmod(Z, G)
	print(n * (n + 1) // 2 - (G * (k * (k + 1) // 2) + (k + 1) * rest))


import sys
(T,) = map(int, input().split())
ans = []
for t in range(T):
	(N, M) = map(int, input().split())
	numSubstrings = N * (N + 1) // 2
	numZeroes = N - M
	numGroups = M + 1
	(d, r) = divmod(numZeroes, numGroups)
	assert (numGroups - r) * d + r * (d + 1) == numZeroes
	ans.append(numSubstrings - (numGroups - r) * (d * (d + 1) // 2) - r * ((d + 1) * (d + 2) // 2))
print('\n'.join(map(str, ans)))


import sys
sys.setrecursionlimit(10 ** 7)
T = int(input())
CASE = []
for _ in range(T):
	(n, m) = map(int, input().split())
	CASE.append([n, m])
res = []
for (n, m) in CASE:
	zero_cnt = n - m
	(p, q) = divmod(zero_cnt, m + 1)
	total = n * (n + 1) // 2
	if p == 0:
		total -= zero_cnt
	else:
		total -= p * (p + 1) // 2 * (m + 1 - q)
		total -= (p + 1) * (p + 2) // 2 * q
	res.append(str(total))
print('\n'.join(res))


from math import *
from heapq import heappop, heappush
MOD = 10 ** 9 + 7

class Solution:
	pass

def areYouCool(n, a, b, amount):
	for val in range(1, min(a // amount, n - 1) + 1):
		if b // amount + val >= n:
			return True
	return False

def solution():
	t = int(input())
	for _ in range(t):
		(n, m) = map(int, input().split())
		if m == 0:
			print(0)
			continue
		zeros = n - m
		total = n * (n + 1) // 2
		if zeros <= m:
			print(total - zeros)
			continue
		m += 1
		k = zeros // m
		l = zeros % m
		res = total - l * ((k + 1) * (k + 2)) // 2 - (m - l) * (k * (k + 1)) // 2
		print(res)

def main():
	t = 1
	for _ in range(t):
		solution()
import sys
import threading
sys.setrecursionlimit(10 ** 6)
threading.stack_size(1 << 27)
thread = threading.Thread(target=main)
thread.start()
thread.join()


import os
import sys
from io import BytesIO, IOBase
from math import log2, ceil, sqrt, gcd, log
from collections import *
from bisect import *
from heapq import *
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
g = lambda : input().strip()
gl = lambda : g().split()
gil = lambda : [int(var) for var in gl()]
gfl = lambda : [float(var) for var in gl()]
gcl = lambda : list(g())
gbs = lambda : [int(var) for var in g()]
rr = lambda x: reversed(range(x))
mod = int(1000000000.0) + 7
inf = float('inf')
(t,) = gil()
for _ in range(t):
	(n, m) = gil()
	z = n - m
	sz = z // (m + 1)
	md = z % (m + 1)
	if z <= m + 1:
		print(n * (n + 1) // 2 - z)
	else:
		print((n * (n + 1) - (m + 1 - md) * sz * (sz + 1) - md * (sz + 1) * (sz + 2)) // 2)


import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__

def dogshit():
	for _ in range(t):
		(n, m) = map(int, input().split())
		o = n - m
		stdo = o // (m + 1)
		stdol = o % (m + 1)
		res = n * (n + 1) // 2 - stdo * (stdo + 1) // 2 * (m + 1 - stdol) - (stdo + 1) * (stdo + 2) // 2 * stdol
		yield res
t = int(input())
ans = dogshit()
print(*ans, sep='\n')


import sys
import math
import itertools
import functools
import collections
import operator

def ii():
	return int(input())

def mi():
	return map(int, input().split())

def li():
	return list(map(int, input().split()))

def lcm(a, b):
	return abs(a * b) // math.gcd(a, b)

def revn(n):
	return str(n)[::-1]

def dd():
	return collections.defaultdict(int)

def ddl():
	return collections.defaultdict(list)

def sieve(n):
	if n < 2:
		return list()
	prime = [True for _ in range(n + 1)]
	p = 3
	while p * p <= n:
		if prime[p]:
			for i in range(p * 2, n + 1, p):
				prime[i] = False
		p += 2
	r = [2]
	for p in range(3, n + 1, 2):
		if prime[p]:
			r.append(p)
	return r

def divs(n, start=2):
	r = []
	for i in range(start, int(math.sqrt(n) + 1)):
		if n % i == 0:
			if n / i == i:
				r.append(i)
			else:
				r.extend([i, n // i])
	return r

def divn(n, primes):
	divs_number = 1
	for i in primes:
		if n == 1:
			return divs_number
		t = 1
		while n % i == 0:
			t += 1
			n //= i
		divs_number *= t

def prime(n):
	if n == 2:
		return True
	if n % 2 == 0 or n <= 1:
		return False
	sqr = int(math.sqrt(n)) + 1
	for d in range(3, sqr, 2):
		if n % d == 0:
			return False
	return True

def convn(number, base):
	newnumber = 0
	while number > 0:
		newnumber += number % base
		number //= base
	return newnumber

def cdiv(n, k):
	return n // k + (n % k != 0)
ans = []
t = ii()
for _ in range(t):
	(n, m) = mi()
	z = n - m
	g = m + 1
	k = z // g
	ans.append(n * (n + 1) // 2 - k * (k + 1) // 2 * g - (k + 1) * (z % g))
print(*ans, sep='\n')


import sys
inf = 1 << 64

def input():
	return sys.stdin.readline().rstrip()

def slv():
	(n, m) = map(int, input().split())
	(u, v) = divmod(n - m, m + 1)
	ans = n * (n + 1) // 2 - v * (u + 1) * (u + 2) // 2 - (m + 1 - v) * u * (u + 1) // 2
	print(ans)
	return
t = int(input())
for i in range(t):
	slv()


from sys import stdin, stdout
for _ in range(int(stdin.readline())):
	(m, n) = map(int, stdin.readline().split())
	zero = m - n
	t = zero // (n + 1)
	r = zero % (n + 1)
	z = t + 1
	stdout.write(str(m * (m + 1) // 2 - r * (z * (z + 1)) // 2 - (n + 1 - r) * t * (t + 1) // 2) + '\n')


import sys
readline = sys.stdin.readline
T = int(readline())
Ans = [None] * T

def calc(d):
	return d * (d + 1) // 2
for qu in range(T):
	(N, M) = map(int, readline().split())
	seg = M + 1
	leng = N - M
	Ans[qu] = calc(N) - calc(leng // seg) * (seg - leng % seg) - calc(leng // seg + 1) * (leng % seg)
print('\n'.join(map(str, Ans)))


import sys
input = sys.stdin.readline

def poss(n, m, mid):
	return m >= n // mid
out = []
for _ in range(int(input().strip())):
	(n, m) = list(map(int, input().strip().split()))
	if m >= n:
		out.append(n * (n + 1) // 2)
	elif m == 0:
		out.append(0)
	else:
		start = 1
		end = n
		mid = (start + end) // 2
		while end >= start:
			mid = (start + end) // 2
			if end == start:
				break
			if poss(n, m, mid):
				end = mid
			else:
				start = mid + 1
		ans = (n - mid + 1) * (n - mid + 2) // 2
		if mid <= (n + 1) // 2:
			ans += mid * (mid - 1) // 2 * m
		else:
			pp = (n + 1) // 2
			ans += pp * (pp + 1) // 2
			st = n - mid
			en = pp - 1
			ans += en * (en + 1) // 2
			ans -= st * (st + 1) // 2
		out.append(ans)
print('\n'.join(map(str, out)))


from heapq import heappush, heappop, heapify
from collections import deque, defaultdict, Counter
import itertools
from itertools import permutations, combinations, accumulate
import sys
import bisect
import string
import math
import time

def I():
	return int(input())

def MI():
	return map(int, input().split())

def S():
	return input()

def MS():
	return map(str, input().split())

def LI():
	return [int(i) for i in input().split()]

def LI_():
	return [int(i) - 1 for i in input().split()]

def StoI():
	return [ord(i) - 97 for i in input()]

def ItoS(nn):
	return chr(nn + 97)

def input():
	return sys.stdin.readline().rstrip()

def show(*inp, end='\n'):
	if show_flg:
		print(*inp, end=end)
YN = {False: 'No', True: 'Yes'}
MOD = 10 ** 9 + 7
inf = float('inf')
IINF = 10 ** 10
l_alp = string.ascii_lowercase
u_alp = string.ascii_uppercase
nums = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']
show_flg = False

def main():
	t = I()
	for i in range(t):
		(n, m) = MI()
		z = n - m
		g = m + 1
		total = n * (n + 1) // 2
		if z >= g:
			k = (z + (g - 1)) // g
			r = z % g
			zero_total = 0
			if r != 0:
				zero_total = k * (k + 1) // 2 * r
				zero_total += k * (k - 1) // 2 * (g - r)
			else:
				zero_total = k * (k + 1) // 2 * g
			print(total - zero_total)
		else:
			k = 1
			g = z
			zero_total = k * (k + 1) // 2 * g
			print(total - zero_total)
main()


import sys
input = sys.stdin.readline

def main():
	t = int(input())
	for _ in range(t):
		(N, M) = [int(x) for x in input().split()]
		if M == 0:
			print(0)
			continue
		total = N * (N + 1) // 2
		if N - M <= M + 1:
			print(total - (N - M))
		else:
			(q, r) = divmod(N - M, M + 1)
			ans = 0
			x = (q + 1) * (q + 1 + 1) // 2
			ans += x * r
			y = q * (q + 1) // 2
			ans += y * (M + 1 - r)
			print(total - ans)
main()


import sys
t = int(input())
for T in range(t):
	(n, m) = [int(x) for x in sys.stdin.readline().split()]
	ans = n * (n + 1) // 2
	val = (n - m) // (m + 1)
	ans -= val * (val + 1) * (m + 1) // 2
	rem = (n - m) % (m + 1)
	ans -= (val + 1) * rem
	print(ans)


import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub
sys.setrecursionlimit(100000)
input = sys.stdin.readline
sys.setrecursionlimit(100000)
input = sys.stdin.readline
INF = 2 ** 62 - 1

def read_int():
	return int(input())

def read_int_n():
	return list(map(int, input().split()))

def read_float():
	return float(input())

def read_float_n():
	return list(map(float, input().split()))

def read_str():
	return input().strip()

def read_str_n():
	return list(map(str, input().split()))

def error_print(*args):
	print(*args, file=sys.stderr)

def mt(f):
	import time

	def wrap(*args, **kwargs):
		s = time.time()
		ret = f(*args, **kwargs)
		e = time.time()
		error_print(e - s, 'sec')
		return ret
	return wrap

def slv(N, M):
	if M == 0:
		return 0
	if M == N:
		return (N + 1) * N // 2
	ans = (N + 1) * N // 2
	r = N - M
	d = r // (M + 1)
	e = r % (M + 1)
	if d != 0:
		ans -= (d + 2) * (d + 1) // 2 * e
		ans -= (d + 1) * d // 2 * (M + 1 - e)
	else:
		ans -= r
	return ans

def main():
	T = read_int()
	for _ in range(T):
		(N, M) = read_int_n()
		print(slv(N, M))
main()


from math import *
from sys import stdin
input = stdin.readline
for _ in range(int(input())):
	(n, ones) = map(int, input().split())
	zeros = n - ones
	Low = zeros / (ones + 1)
	flrLow = floor(Low)
	ans = n * (n + 1) // 2
	if flrLow == 0:
		print(ans - zeros)
		continue
	if flrLow == Low:
		ans -= zeros // flrLow * (flrLow * (flrLow + 1) // 2)
	elif flrLow != Low:
		Cnt = zeros % (ones + 1)
		ans -= Cnt * ((flrLow + 1) * (flrLow + 2) // 2)
		ans -= (ones + 1 - Cnt) * (flrLow * (flrLow + 1) // 2)
	print(ans)


import math, sys
input = sys.stdin.readline
t = int(input())

def meth(x):
	return x * (x + 1) // 2
for _ in range(t):
	(n, m) = list(map(int, input().split()))
	val = (n - m) // (m + 1)
	pp = (n - m) % (m + 1)
	res = n * (n + 1) // 2
	res -= pp * meth(val + 1)
	res -= (m + 1 - pp) * meth(val)
	print(res)


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	(N, M) = map(int, input().split())
	if M == 0:
		print(0)
	elif 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)


from sys import stdin, stdout
t = int(stdin.readline().strip())
for _ in range(t):
	(n, m) = map(int, stdin.readline().split())
	ans = None
	if m == 0:
		ans = 0
	elif m * 2 >= n:
		ans = n * (n + 1) // 2 - (n - m)
	else:
		q = (n - m) // (m + 1)
		r = (n - m) % (m + 1)
		ans = n * (n + 1) // 2 - ((m + 1) * q + 2 * r) * (q + 1) // 2
	stdout.write('{}\n'.format(ans))


from sys import stdin
from bisect import bisect_left as bl
from bisect import bisect_right as br

def input():
	return stdin.readline()[:-1]

def intput():
	return int(input())

def sinput():
	return input().split()

def intsput():
	return map(int, sinput())

class RangedList:

	def __init__(self, start, stop, val=0):
		self.shift = 0 - start
		self.start = start
		self.stop = stop
		self.list = [val] * (stop - start)

	def __setitem__(self, key, value):
		self.list[key + self.shift] = value

	def __getitem__(self, key):
		return self.list[key + self.shift]

	def __repr__(self):
		return str(self.list)

	def __iter__(self):
		return iter(self.list)

def foo(n):
	return (n ** 2 + n) // 2
t = intput()
for _ in range(t):
	(n, m) = intsput()
	ones = m
	zeros = n - ones
	size = zeros // (ones + 1)
	rem = zeros - size * (ones + 1)
	largers = rem
	smallers = ones + 1 - largers
	largesize = size + 1
	smallsize = size
	print(foo(n) - largers * foo(largesize) - smallers * foo(smallsize))


def fastio():
	import sys
	from io import StringIO
	from atexit import register
	global input
	sys.stdin = StringIO(sys.stdin.read())
	input = lambda : sys.stdin.readline().rstrip()
	sys.stdout = StringIO()
	register(lambda : sys.__stdout__.write(sys.stdout.getvalue()))
fastio()
t = int(input())
C = [list(map(int, input().split())) for i in range(t)]

def c(x, y):
	if x % 2 == 0:
		return x // 2 * y
	else:
		return y // 2 * x
for i in range(t):
	n = C[i][0]
	o = C[i][1]
	z = n - o
	al = c(n, n - 1) + n
	if z - 1 <= o:
		print(al - z)
	else:
		a = z // (o + 1)
		b = z % (o + 1)
		mi = c(a + 1, a + 2) * b + c(a, a + 1) * (o + 1 - b)
		print(al - mi)


import sys
input = sys.stdin.buffer.readline

def s(n):
	return n * (n + 1) // 2
t = int(input())
res = []
for _ in range(t):
	(n, m) = map(int, input().split())
	x = s(n)
	z = n - m
	x -= z % (m + 1) * s(z // (m + 1) + 1)
	x -= (m + 1 - z % (m + 1)) * s(z // (m + 1))
	res.append(x)
print('\n'.join(map(str, res)))