import sys
input = lambda: sys.stdin.readline().rstrip()
for _ in range(int(input())):
    n,m=map(int,input().split())
    z=n-m
    div=z//(m+1)
    rem=z%(m+1)
    ans=n*(n+1)//2-div*(div+1)*(m+1)//2-(div+1)*rem
    print(ans)