#!/usr/bin/env python3
import sys

def calc(a0, diff, n):
    return n * (2*a0 + (n-1)*diff) // 2

def go(n, m):
    if n//2 <= m:
        ans = n*(n+1)//2
        ans -= (n-m)
        return ans
    elif m == 0:
        return 0
    else:
        k = (n-m)//(m+1)
        r = (n-m)%(m+1)
        begin = (k+1) * calc(n-k, -(k+1), (m+1)-r)
        end = (k+2) * calc((k+2)*(r-1), -(k+2), r-1)
        return begin + end

t = int(sys.stdin.readline())
for _ in range(t):
    n, m = map(int,sys.stdin.readline().split())
    print(go(n, m))
