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
Tags: String algorithms

Sorting

Combinatorics

Greedy algorithms

Mathematics
Skill types: Sorting

Greedy algorithms

Starter code:

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 sys
import io, os
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

def in1():
	return int(input())

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

def inx():
	return [int(_) for _ in input().split()]
q = in1()
for _ in range(q):
	(n, m) = in3()
	z = n - m
	x = z // (m + 1)
	b = z % (m + 1)
	norr = m + 1 - b
	t = n * (n + 1) // 2
	bx = b * (x + 1) * (x + 2) // 2
	ax = norr * x * (x + 1) // 2
	print(t - ax - bx)


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


from sys import stdin
input = stdin.readline
import math
for xoxo in range(1):

	def s(x):
		return (x + 1) * x // 2
	for _ in range(int(input())):
		b = []
		(n, m) = map(int, input().split())
		if m >= math.ceil(n / 2) and m != 0:
			print(s(n) - (n - m))
		elif m == 0:
			print('0')
		else:
			k = (n - m) // (m + 1)
			p = (n - m) % (m + 1)
			kk = m + 1 - p
			ans = s(n)
			ans -= s(k) * kk
			ans -= s(k + 1) * p
			print(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
import os
import io
from math import log, gcd, ceil
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop
import math

def primeFactors(n):
	l = []
	while n % 2 == 0:
		l.append(2)
		n = n / 2
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		while n % i == 0:
			l.append(int(i))
			n = n / i
	if n > 2:
		l.append(n)
	return list(set(l))

def power(x, y, p):
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def si():
	return input()
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

def prefix_sum(arr):
	r = [0] * (len(arr) + 1)
	for (i, el) in enumerate(arr):
		r[i + 1] = r[i] + el
	return r

def divideCeil(n, x):
	if n % x == 0:
		return n // x
	return n // x + 1

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

def li():
	return list(map(int, input().split()))
t = int(input())
for _ in range(t):
	(n, m) = li()
	noOfZerosCons = (n - m) // (m + 1)
	howManyOneMore = (n - m) % (m + 1)
	ans = 0
	ans = ans + noOfZerosCons * (noOfZerosCons + 1) // 2 * (m + 1 - howManyOneMore)
	ans = ans + howManyOneMore * ((noOfZerosCons + 1) * (noOfZerosCons + 2) // 2)
	print(n * (n + 1) // 2 - ans)


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


t = int(input())
ans = []
for kek in range(t):
	(n, m) = map(int, input().split())
	ans2 = 0
	if m == 0:
		ans2 = 0
	else:
		k = int((n + 1) / (m + 1))
		ans2 += m * (k + 1) * k // 2
		ans2 += (n - k) * (1 + n - k) // 2
	ans.append(ans2)
print(*ans)


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 math
import sys
for _ in range(int(input())):
	(n, m) = map(int, sys.stdin.readline().split())
	k = n - m
	p = m + 1
	c = k // p
	ans = n * (n + 1) // 2
	rem = k % p
	ans1 = rem * ((c + 1) * (c + 2)) // 2
	ans2 = 0
	ans2 = c * (c + 1) * (p - rem) // 2
	print(ans - ans1 - ans2)


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


import sys
t = int(sys.stdin.readline())
for _ in range(t):
	(n, m) = map(int, sys.stdin.readline().split())
	g = m + 1
	z = n - m
	k = z // g
	ans = 0
	a = z % g
	total = n * (n + 1) // 2
	ans += a * (k + 1) * (k + 2) // 2
	ans += k * (k + 1) // 2 * (m + 1 - a)
	print(total - ans)


import os
import sys
from math import *
from collections import *
from fractions import *
from bisect import *
from io import BytesIO, IOBase

def vsInput():
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
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 value():
	return tuple(map(int, input().split()))

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

def Int():
	return int(input())

def Str():
	return input()

def arrayS():
	return [i for i in input().split()]
for _ in range(Int()):
	(n, m) = value()
	if m == 0:
		print(m)
		continue
	total = n * (n + 1) // 2
	if n - m <= ceil(n / 2):
		print(total - n + m)
	else:
		zero = n - m
		a = zero // (m + 1)
		b = zero % (m + 1)
		total -= (a + 1) * (a + 2) * b // 2 + (m + 1 - b) * a * (a + 1) // 2
		print(total)


def main():
	import sys
	input = sys.stdin.readline
	t = int(input())
	for _ in range(t):
		(N, M) = list(map(int, input().split()))
		S = (N + 1) * N // 2
		zero = N - M
		num = zero // (M + 1)
		major = zero % (M + 1)
		minor = M + 1 - major
		S -= major * ((num + 2) * (num + 1) // 2)
		S -= minor * ((num + 1) * num // 2)
		print(S)

def __starting_point():
	main()
__starting_point()


import sys
input = lambda : sys.stdin.readline().rstrip()
for _ in range(int(input())):
	(n, m) = (int(x) for x in input().strip().split(' '))
	g = m + 1
	z = n - m
	k = z // g
	print(n * (n + 1) // 2 - g * k * (k + 1) // 2 - (k + 1) * (z % g))


import sys
t = int(sys.stdin.readline())
for i in range(t):
	(n, m) = [int(i) for i in sys.stdin.readline().split()]
	st = 1
	if n == 1:
		if m == int(1):
			print('1')
		else:
			print('0')
		st = -1
	ans = n * (n - 1) // 2 + n
	nl = n - m
	if (n - m) % (m + 1) == 0:
		g = (n - m) // (m + 1)
		ans -= (g * (g - 1) // 2 + g) * (m + 1)
	else:
		g = (n - m) // (m + 1) + 1
		ans -= (g * (g - 1) // 2 + g) * ((n - m) % (m + 1))
		g -= 1
		ans -= (g * (g - 1) // 2 + g) * (m + 1 - (n - m) % (m + 1))
	if st == 1:
		print(ans)


import math, collections, sys
input = sys.stdin.readline

def case():
	(n, m) = map(int, input().split())
	numZ = n - m
	div = numZ // (m + 1)
	rem = numZ % (m + 1)
	ans = rem * ((div + 1) * (div + 2)) // 2 + (m + 1 - rem) * div * (div + 1) // 2
	total = n * (n + 1) // 2
	print(total - ans)
for _ in range(int(input())):
	case()


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


import sys
input = sys.stdin.readline
t = int(input())
for i in range(t):
	(n, m) = map(int, input().split())
	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)


import sys
import math
import bisect
from sys import stdin, stdout
from math import gcd, floor, sqrt, log2, ceil, pi, sin, cos, acos, atan
from collections import defaultdict as dd
from bisect import bisect_left as bl, bisect_right as br
from bisect import insort
from collections import Counter
from collections import deque
from heapq import heappush, heappop, heapify
from itertools import permutations, combinations
mod = int(1000000000.0) + 7
ip = lambda : int(stdin.readline())
inp = lambda : map(int, stdin.readline().split())
ips = lambda : stdin.readline().rstrip()
out = lambda x: stdout.write(str(x) + '\n')
t = ip()
for _ in range(t):
	(n, m) = inp()
	nn = n
	n -= m
	gaps = m + 1
	times = n // gaps
	ans = 0
	remain = n % gaps
	big = remain
	small = gaps - remain
	ans = (times + 1) * (times + 2) // 2 * big
	ans += times * (times + 1) // 2 * small
	total = nn * (nn + 1) // 2
	ans = total - ans
	out(ans)


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
from sys import stdin, stdout
import bisect
import math
import heapq

def modinv(n, p):
	return pow(n, p - 2, p)

def cin():
	return map(int, sin().split())

def ain():
	return list(map(int, sin().split()))

def sin():
	return input()

def inin():
	return int(input())
import math

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

def main():
	t = inin()
	for _ in range(t):
		(n, m) = cin()
		x = n - m
		pp = n * (n + 1) // 2
		m += 1
		f = x % m
		s = x // m
		p = m - f
		g = s * (s + 1) // 2 * p
		h = (s + 1) * (s + 2) // 2 * f
		print(pp - g - h)
py2 = round(0.5)
if py2:
	from future_builtins import ascii, filter, hex, map, oct, zip
	range = xrange
import os, sys
from io import IOBase, BytesIO
BUFSIZE = 8192

class FastIO(BytesIO):
	newlines = 0

	def __init__(self, file):
		self._file = file
		self._fd = file.fileno()
		self.writable = 'x' in file.mode or 'w' in file.mode
		self.write = super(FastIO, self).write if self.writable else None

	def _fill(self):
		s = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
		self.seek((self.tell(), self.seek(0, 2), super(FastIO, self).write(s))[0])
		return s

	def read(self):
		while self._fill():
			pass
		return super(FastIO, self).read()

	def readline(self):
		while self.newlines == 0:
			s = self._fill()
			self.newlines = s.count(b'\n') + (not s)
		self.newlines -= 1
		return super(FastIO, self).readline()

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

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		if py2:
			self.write = self.buffer.write
			self.read = self.buffer.read
			self.readline = self.buffer.readline
		else:
			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 sys

class ostream:

	def __lshift__(self, a):
		sys.stdout.write(str(a))
		return self
cout = ostream()
endl = '\n'

def readnumbers(zero=0):
	conv = ord if py2 else lambda x: x
	A = []
	numb = zero
	sign = 1
	i = 0
	s = sys.stdin.buffer.read()
	try:
		while True:
			if s[i] >= b'0'[0]:
				numb = 10 * numb + conv(s[i]) - 48
			elif s[i] == b'-'[0]:
				sign = -1
			elif s[i] != b'\r'[0]:
				A.append(sign * numb)
				numb = zero
				sign = 1
			i += 1
	except:
		pass
	if s and s[-1] >= b'0'[0]:
		A.append(sign * numb)
	return A
main()


from sys import stdin
input = stdin.readline
from math import ceil

def answer():
	total = n * (n + 1) // 2
	x = n - m
	div = x // (m + 1)
	ans = total - div * (div + 1) // 2 * (m + 1) - (div + 1) * (x % (m + 1))
	return ans
for T in range(int(input())):
	(n, m) = map(int, input().split())
	print(answer())


def main():
	import sys
	input = sys.stdin.readline
	T = int(input())
	for t in range(T):
		(n, m) = map(int, input().split())
		k = n - m
		p = k // (m + 1)
		q = k % (m + 1)
		ans = n * (n + 1) // 2
		ans -= p * (p + 1) // 2 * (m + 1 - q)
		ans -= (p + 1) * (p + 2) // 2 * q
		print(ans)
main()


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
for _ in range(int(input())):
	(n, m) = map(int, sys.stdin.readline().split())
	emp = n - m
	if emp >= m + 1:
		xx = emp // (m + 1)
		rem = emp % (m + 1)
		print(n * (n + 1) // 2 - rem * (xx + 1) * (xx + 2) // 2 - (m + 1 - rem) * xx * (xx + 1) // 2)
	else:
		print(n * (n + 1) // 2 - emp)


import sys
input = sys.stdin.readline

def solve(n, m):
	z = n - m
	g = m + 1
	k = int(z / g)
	ans = n * (n + 1) // 2 - k * (k + 1) // 2 * g - (k + 1) * (z % g)
	return int(ans)
t = int(input())
out = []
for i in range(t):
	(n, m) = map(int, input().split())
	out.append(solve(n, m))
print('\n'.join(list(map(str, out))))


import sys
from math import ceil
Ri = lambda : [int(x) for x in sys.stdin.readline().split()]
ri = lambda : sys.stdin.readline().strip()

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
for _ in range(int(ri())):
	(n, k) = Ri()
	z = n - k
	o = k + 1
	each = z // o
	rem = z % o
	tot = 0
	ite = rem
	tot += rem * (each + 1) * (each + 2) // 2
	tot = tot + (o - rem) * each * (each + 1) // 2
	print(n * (n + 1) // 2 - tot)


import sys
input = sys.stdin.readline
import math
t = int(input())
for _ in range(t):
	(n, m) = [int(item) for item in input().split()]
	zeros = n - m
	groups = min(zeros, m + 1)
	large_num = zeros % (m + 1)
	small_num = max(0, groups - large_num)
	small = zeros // (m + 1)
	large = small + 1
	cnt = small * (small + 1) // 2 * small_num
	cnt += large * (large + 1) // 2 * large_num
	ans = n * (n + 1) // 2 - cnt
	print(ans)


import sys
input = sys.stdin.readline
t = int(input())
for case in range(t):
	(n, m) = map(int, input().split())
	z = n - m
	g = m + 1
	if m == 0:
		print(0)
		continue
	if m == n:
		print(n * (n + 1) // 2)
		continue
	if m >= n // 2:
		print(n * (n + 1) // 2 - (n - m))
		continue
	k = z // g
	ans = n * (n + 1) // 2 - g * (k * (k + 1) // 2) - z % g * (k + 1)
	print(ans)


import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	(n, m) = list(map(int, input().split()))
	all_ptn = n * (n - 1) // 2 + n
	small = (n - m) // (m + 1)
	large_cnt = (n - m) % (m + 1)
	small_cnt = m + 1 - large_cnt
	large = small + 1
	cnt = (small * (small - 1) // 2 + small) * small_cnt
	cnt += (large * (large - 1) // 2 + large) * large_cnt
	print(all_ptn - cnt)


import sys
input = sys.stdin.readline
t = int(input())
for you in range(t):
	l = input().split()
	n = int(l[0])
	m = int(l[1])
	tot = n * (n + 1) // 2
	z = (n - m) // (m + 1)
	q = (n - m) % (m + 1)
	sumi = 0
	sumi = q * ((z + 1) * (z + 2) // 2)
	sumi += (m + 1 - q) * (z * (z + 1) // 2)
	print(tot - sumi)


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 factorial
from collections import Counter, defaultdict
from heapq import heapify, heappop, heappush

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

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

def N():
	return int(input())

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)

def func(n):
	return n * (n + 1) // 2
for _ in range(int(input())):
	(n, m) = map(int, input().split())
	noz = n - m
	totalsub = func(n)
	nozd = noz // (m + 1)
	rem = noz % (m + 1)
	totalsub -= func(nozd + 1) * rem
	totalsub -= func(nozd) * (m + 1 - rem)
	print(int(totalsub))


import sys
import io, os
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
zzz = 3
q = int(input())
for _ in range(q):
	(n, m) = map(int, input().split())
	z = n - m
	x = z // (m + 1)
	b = z % (m + 1)
	norr = m + 1 - b
	t = n * (n + 1) // 2
	bx = b * (x + 1) * (x + 2) // 2
	ax = norr * x * (x + 1) // 2
	print(t - ax - bx)


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 tc in range(int(input())):
	(n, m) = map(int, input().split())
	ans = n * (n + 1) // 2
	z = n - m
	g = m + 1
	k = z // g
	print(ans - k * (k + 1) // 2 * g - (k + 1) * (z % g))


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


import sys
input = sys.stdin.readline
t = int(input())
for i in range(t):
	(a, b) = map(int, input().split())
	d = a - b
	q = d // (b + 1)
	r = d % (b + 1)
	count = 0
	sm = (q + 1) * (q + 2) // 2 * r
	tot = (q + 1) * r + r
	tot = a - tot
	tot = tot - (b - r)
	if q > 0:
		dv = tot // q
		sm = sm + q * (q + 1) * dv // 2
	ans = a * (a + 1) // 2
	print(ans - sm)


import sys
import copy
from math import *
input = sys.stdin.readline

def bo(i):
	return ord(i) - ord('a')
t = int(input())
while t > 0:
	t -= 1
	(n, m) = map(int, input().split())
	c = 0
	if m == 0:
		print(0)
		continue
	q = (n - m) // (m + 1)
	p = q + 1
	c += m * (p * (p + 1) // 2)
	d = n - p
	c += d * (d + 1) // 2
	print(c)


for _ in range(int(input())):
	[a, b] = list(map(int, input().split(' ')))
	k = (a - b) // (b + 1)
	extra = (a - b) % (b + 1)
	print((a * (a + 1) - (a - b + extra) * (k + 1)) // 2)


import math, sys
from collections import Counter, defaultdict, deque
from sys import stdin, stdout
input = stdin.readline
lili = lambda : list(map(int, sys.stdin.readlines()))
li = lambda : list(map(int, input().split()))
I = lambda : int(input())
S = lambda : input().strip()
mod = 1000000007
for i in range(I()):
	(n, m) = li()
	ans = n * (n + 1) // 2
	p = (n - m) // (m + 1)
	ans -= p * (p + 1) // 2 * (m + 1)
	ans -= (p + 1) * ((n - m) % (m + 1))
	print(ans)


import sys
input = sys.stdin.readline
a = int(input())
for i in range(a):
	(x, y) = map(int, input().split())
	one = y
	zero = x - y
	if one >= zero:
		print(x * (x + 1) // 2 - zero)
	elif one == 0:
		print(0)
		continue
	else:
		t = zero // (one + 1)
		r = zero % (one + 1)
		ka = r * ((t + 1) * (t + 2) // 2) + (one + 1 - r) * (t * (t + 1) // 2)
		print(x * (x + 1) // 2 - ka)


import math, random
from sys import stdin, stdout
input = stdin.readline
t = int(input())
s = ''

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

def f(n, m, l=True):
	p = n - m
	numGaps = m + 1
	numHigher = p % numGaps
	numLower = numGaps - numHigher
	higher = math.ceil(p / numGaps)
	lower = p // numGaps
	totalTaken = toTakeAway(higher) * numHigher + toTakeAway(lower) * numLower
	if l:
		stdout.write(str(toTakeAway(n) - totalTaken) + '\n')
	else:
		s = str(toTakeAway(n) - totalTaken) + '\n'

def g():
	for i in range(100000):
		f(1000000000, random.randrange(1, 1000000000), False)
for _ in range(t):
	(n, m) = map(int, input().split())
	f(n, m)


import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	(n, m) = map(int, input().split())
	zero_num = n - m
	if m == 0:
		print(0)
		continue
	k = zero_num // (m + 1)
	l = zero_num % (m + 1)
	k_num = m + 1 - l
	kp_num = l
	ans = (n + 1) * n // 2
	ans -= (k + 1) * k // 2 * k_num
	ans -= (k + 2) * (k + 1) // 2 * kp_num
	print(ans)


from sys import stdin, stdout

def total(k):
	return k * (k + 1) // 2
ans = []
t = int(stdin.readline())
for line in stdin:
	(n, m) = tuple(map(int, line.split()))
	if m == 0:
		ans.append(str(0))
	else:
		al = total(n)
		zeros = n - m
		(k, extra) = (zeros // (m + 1), zeros % (m + 1))
		zero_blocks = extra * (k + 1) + (m + 1) * total(k)
		ans.append(str(al - zero_blocks))
stdout.write('\n'.join(ans))


from collections import Counter
from collections import defaultdict as dfd
from bisect import bisect, bisect_left
from math import sqrt, gcd, ceil, factorial
from heapq import heapify, heappush, heappop
MOD = 10 ** 9 + 7
inf = float('inf')
ans_ = []

def nin():
	return int(input())

def ninf():
	return int(file.readline())

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

def stf():
	return file.readline().strip()

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

def readf():
	return list(map(int, file.readline().strip().split()))

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

def solve():
	for _ in range(nin()):
		(n, m) = read()
		z = n - m
		g = m + 1
		k = z // g
		ans = f(n) - f(k) * (g - z % g) - f(k + 1) * (z % g)
		ans_.append(ans)
solve()
for i in ans_:
	print(i)


import sys
inp = sys.stdin.readline
input = lambda : inp().strip()
flush = sys.stdout.flush

def iin():
	return int(input())

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

def main():
	T = iin()

	def fn(n):
		return n * (n + 1) // 2
	while T:
		T -= 1
		(n, m) = lin()
		ans = fn(n)
		dl = 0
		bx = m + 1
		zo = n - m
		x = zo // bx
		x1 = zo % bx
		x2 = bx - x1
		dl = fn(x) * x2 + fn(x + 1) * x1
		print(ans - dl)
main()


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

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)
		r2 = m + 1 - r
		a0 = n - (k + 1) + 1
		ans2 = r2 * (2 * a0 + (r2 - 1) * -(k + 1)) // 2
		ans2 *= k + 1
		a00 = n - (k + 1) * r2 - (k + 2) + 1
		ans3 = (r - 1) * (2 * a00 + (r - 2) * -(k + 2)) // 2
		ans3 *= k + 2
		return ans2 + ans3
for _ in range(t):
	(n, m) = map(int, sys.stdin.readline().split())
	print(go(n, m))


import sys
input = lambda : sys.stdin.readline().rstrip()
T = int(input())
for _ in range(T):
	(N, M) = map(int, input().split())
	Z = N - M
	A = Z // (M + 1)
	B = Z % (M + 1)
	print(N * (N + 1) // 2 - A * (A + 1) // 2 * (M + 1 - B) - (A + 1) * (A + 2) // 2 * B)


import sys
read = lambda : list(map(int, sys.stdin.readline().strip().split()))

def cal():
	(n, m) = read()
	z = n - m
	g = m + 1
	k = z // g
	n = n * (n + 1) // 2
	t = k * (k + 1) // 2
	ans = n
	ans -= t * g
	ans -= (k + 1) * (z % g)
	return ans
for _ in range(int(input())):
	print(cal())


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


from __future__ import division, print_function
import os
import sys, math
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 f(n):
	return n * (n + 1) // 2

def main():
	Test = int(input())
	for t in range(Test):
		(n, one) = map(int, input().split())
		zero = n - one
		ans = f(n)
		if one >= zero:
			ans -= zero
			print(ans)
			continue
		part = one + 1
		left = zero % part
		each = zero // part
		ans -= f(each + 1) * left + f(each) * (part - left)
		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()


import sys
from fractions import gcd
from math import ceil, sqrt, sin, pi, tan
from collections import defaultdict
read = sys.stdin.buffer.readline
import heapq
mi = lambda : map(int, read().split())
li = lambda : list(mi())
cin = lambda : int(read())
for _ in range(cin()):
	(n, m) = mi()
	z = n - m
	g = m + 1
	k = z // g
	ans = n * (n + 1) // 2 - g * k * (k + 1) // 2 - (k + 1) * (z % g)
	print(ans)


import sys
for _ in range(int(input())):
	(n, m) = map(int, sys.stdin.readline().split())
	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)


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