import sys

t =  int(input())
for i in range(t):
    line = list(map(int,sys.stdin.readline().split()))
    n = line[0]
    m = line[1]

    rem = (n-m) % (m+1)
    q = (n-m) // (m+1)
    ans = (n * (n-1)) // 2 + m - ( rem*((q+1)*q) // 2 ) - ((m+1-rem)*((q*(q-1))//2))
    print(ans)
