import sys
for _ in range(int(sys.stdin.readline())):
    n,m = list(map(int,sys.stdin.readline().split()))
    t = (n*(n+1))//2
    if n%2==0:
        x = n//2
        if m==0:
            sys.stdout.write(str(0)+'\n')
        elif m==x:
            sys.stdout.write(str(t-x)+'\n')
        elif m>=x:
            rem = m-x
            x = x-rem
            sys.stdout.write(str(t-x)+'\n')
        else:
            z = n-m
            s = z//(m+1)
            rem = z%(m+1)
            if rem==0:
                ans = ((m+1)*(m+2))//2
                ans = ans*s
                ans = t-ans
            else:
                # print('here',s,rem,m+1)
                ans = ((m+1)*(m+2))//2
                ans = ans*s
                temp = (rem*(rem+1))//2
                ans+=temp
                ans = t-ans
            sys.stdout.write(str(ans)+'\n')
    else:
        if m==0:
            sys.stdout.write(str(0)+'\n')
        elif m==n//2:
            sys.stdout.write(str(t-m-1)+'\n')
        elif m==(n//2)+1:
            sys.stdout.write(str(t-m+1)+'\n')
        elif m<n//2:
            z = n-m
            s = z//(m+1)
            rem = z%(m+1)
            if rem==0:
                ans = ((m+1)*(m+2))//2
                ans = ans*s
                ans = t-ans
            else:
                # print('here',s,rem,m+1)
                ans = ((m+1)*(m+2))//2
                ans = ans*s
                temp = (rem*(rem+1))//2
                ans+=temp
                ans = t-ans
            sys.stdout.write(str(ans)+'\n')
        elif m>(n//2)+1:
            # sys.stdout.write('here')
            rem = m-(n//2)-1
            x = (n//2)
            # sys.stdout.write(rem,x)
            x = x-rem
            sys.stdout.write(str(t-x)+'\n')