def main():
    import sys
    input = sys.stdin.readline

    T = int(input())
    for t in range(T):
        n, m = map(int, input().split())
        k = n - m
        p = k // (m+1)
        q = k % (m+1)
        ans = n * (n+1) // 2
        ans -= p * (p+1) // 2 * (m + 1 - q)
        ans -= (p+1) * (p+2) // 2 * q
        print(ans)

main()