#!/usr/bin/env python3
import sys
input = sys.stdin.readline
import math

t = int(input())
for _ in range(t):
    n, m = [int(item) for item in input().split()]
    zeros = n - m
    large_num = zeros % (m + 1)
    small_num = max(0, (m + 1) - large_num)
    small = zeros // (m + 1) + 1
    large = small + 1 + 1
    cnt = small * (small - 1) // 2 * small_num
    cnt += large * (large - 1) // 2 * large_num
    ans = n * (n + 1) // 2 - cnt
    print(ans)