import sys
input = lambda: sys.stdin.readline().rstrip()

T = int(input())
for _ in range(T):
    N, M = map(int, input().split())
    Z = N - M
    A = Z // (M+1)
    B = Z % (M+1)
    print(N * (N+1) // 2 - A * (A+1) // 2 * (M+1-B) - (A+1) * (A+2) // 2 * B)
