T = int(input())
arr = []
for i in range(T):
    n, m = map(int, input().split())
    k = n - m
    m += 1
    t = k // m
    kol = k - t * m
    ans = n * (n + 1) // 2 - (kol * ((t + 1) ** 2) + (m - kol) * (t ** 2) + k) // 2
    arr.append(ans)
print('\n'.join(map(str, arr)))
