import sys
#from bisect import bisect_left
#from collections import defaultdict

#print("executing")

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

    ze = n-m
    allp = (n*(n+1))//2
    mean = ze//(m+1)
    more = ze%(m+1)
    allp -= (m+1-more)*(mean*(mean+1)//2) + more*((mean+1)*(mean+2)//2)
    print(allp)
