import sys

input = sys.stdin.readline


def main():
    t = int(input())
    for _ in range(t):
        N, M = [int(x) for x in input().split()]

        if M == 0:
            print(0)
            continue

        total = (N * (N + 1)) // 2
        if (N - M) <= M + 1:
            print(total - (N - M))
        else:
            y = (N - M) // (M + 1)

            total2 = (y * (y + 1)) // 2
            total2 *= M
            z = N - M - y * M
            total3 = (z * (z + 1)) // 2
            ans1 = total - total2 - total3

            y = -(-(N - M) // (M + 1))
            total2 = (y * (y + 1)) // 2
            total2 *= M
            z = N - M - y * M
            total3 = (z * (z + 1)) // 2
            ans2 = total - total2 - total3

            print(max(ans1, ans2))


if __name__ == '__main__':
    main()
