import sys

i = list(sys.stdin.readlines())
t = int(i[0])
for __ in range(t):
    n,m = map(int, i[__ + 1].split())
    if m == 0:
        print(0)
        continue
    buckets = m + 1
    fl = (n - m) // buckets
    big_buckets = (n + 1) % buckets
    comp = n * (n + 1) // 2

    comp -= big_buckets * (fl + 2) * (fl + 1) // 2
    comp -= (buckets - big_buckets) * fl * (fl + 1) //2
    print(comp)
