import sys
input = sys.stdin.buffer.readline
 
t = int(input())
for _ in range(t):
    n, m = map(int, input().split())
    ans = n*(n+1)//2
    r = (n-m)%(m+1)
    if r != 0:
        z = (n-m-r)//(m+1)
        ans = ans - (z*(z+1)//2)*m - (z+r)*(z+r+1)//2
        z = (n-m)//(m+1)
        ans = ans - (z*(z+1)//2)*(m+1)
    else:
        z = (n-m)//(m+1)
        ans = ans - (z*(z+1)//2)*(m+1)
    print(ans)