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:
            q, r = divmod((N - M), (M + 1))

            ans = 0
            x = ((q + 1) * (q + 1 + 1)) // 2
            ans += x * r
            y = (q * (q + 1)) // 2
            ans += y * (M + 1 - r)

            print(total - ans)




if __name__ == '__main__':
    main()
