#!/usr/bin/env python3
import sys
t = int(sys.stdin.readline())
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)
        r2 = (m+1)-r
        a0 = n-(k+1)+1
        ans2 = r2*(2*a0 + (r2-1)*(-(k+1)))//2
        ans2 *= (k+1)
        a00 = n-(k+1)*r2-(k+2)+1
        ans3 = (r-1)*(2*a00+(r-2)*(-(k+2)))//2
        ans3 *= (k+2)
        return ans2+ans3
for _ in range(t):
    n, m = map(int,sys.stdin.readline().split())
    print(go(n, m))
