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)