import sys

if __name__ == "__main__":
    T, = map(int, input().split())
    ans = []
    for t in range(T):
        # Need fast IO
        # Using input() TLE'd.
        # Using sys.stdin passed.
        # But in local testing there's no difference?
        N, M = map(int, input().split())

        numSubstrings = N * (N + 1) // 2
        numZeroes = N - M
        numGroups = M + 1  # Use the ones as separators
        d, r = divmod(numZeroes, numGroups)
        # Minimize number of substrings with just zeroes in it
        #   (numGroups - r) groups of size d
        #   r groups of size d + 1
        assert (numGroups - r) * d + r * (d + 1) == numZeroes
        ans.append(
            numSubstrings
            - (numGroups - r) * (d * (d + 1) // 2)
            - r * ((d + 1) * (d + 2) // 2)
        )
    print("\n".join(map(str, ans)))
