import sys

# inf = open('input.txt', 'r')
# reader = (line.rstrip() for line in inf)
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)

# inf.close()
