t = int(input())

for _ in range(t):
    n, m = map(int, input().split())
    
    if m==0:
        print(0)
        continue
    
    a, b = divmod(n-m, m)
    ans = n*(n+1)//2-a*(a+1)//2*m-b*(b+1)//2
    print(ans)