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(even, odd, spread, zero)
    print(total - odd * (odd + 1) // 2 * spread - even * (even + 1) // 2 * (zero - spread))