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

