import sys
for _ in range(int(input())):
    n,m=map(int,sys.stdin.readline().split())
    emp=n-m
    if(emp>=(m+1)):
        xx=emp//(m+1)
        rem=(emp%(m+1))
        print(n*(n+1)//2-rem*(xx+1)*(xx+2)//2-(m+1-rem)*xx*(xx+1)//2)
    else:
        print(n*(n+1)//2-emp)