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)
