t = int(input())
raw = []
for _ in range(t):
    raw.append(list(map(int,input().split())))
for i in range(t):
    n,m = raw[i]
    p = (n-m) // (m+1)
    q = (n-m)  % (m+1)
    ans = n*(n+1)//2
    ans -= (p+1)*(p+2)*q//2
    ans -= p*(p+1)*(m+1-q)//2
    print(ans)