import sys
input = sys.stdin.readline

def main():
    t = int(input())

    for _ in range(t):
        n,m = map(int,input().split())
        if m == 0:
            print(0)
        elif m == n:
            print(n*(n-1)//2 + n)
        else:
            p = n-m
            r,q = p%(m+1),p//(m+1)
            res = n*(n-1)//2 + n
            if r != 0:
                res -= ((q+1) + (q+1)*q//2)*r
            if q != 0:
                res -= (q+q*(q-1)//2)*(m+1-r)
            print(res)
if __name__ == "__main__":
    main()
