import sys
input = sys.stdin.readline

t = int(input())
for _ in range(t):
    n, m  = list(map(int, input().split()))
    all_ptn = n * (n - 1) // 2 + n
    small = (n - m) // (m + 1)
    large_cnt = (n - m) % (m + 1)
    small_cnt = m + 1 - large_cnt
    large = small + 1
    
    cnt = (small * (small - 1) // 2 + small) * small_cnt
    cnt += (large * (large - 1) // 2 + large) * large_cnt
    print(all_ptn - cnt)