"""
    Author : thekushalghosh
    Team   : CodeDiggers
"""
import sys,math
input = sys.stdin.readline
############ ---- USER DEFINED INPUT FUNCTIONS ---- ############
def inp():
    return(int(input()))
def inlt():
    return(list(map(int,input().split())))
def insr():
    s = input()
    return(s[:len(s) - 1])
def invr():
    return(map(int,input().split()))
################################################################
############ ---- THE ACTUAL CODE STARTS BELOW ---- ############
t = 1
t = inp()
for tt in range(t):
    n,m = invr()
    q = n - m
    qw = 0
    if m > q and m % 2 == 0 and q % 2 == 0:
        qw = 1
    s = "10" * min(m,q)
    if q > m:
        q = q - m
        s = "0" * ((q + 1) // 2) + s + "0" * (q // 2)
    else:
        m = m - q
        s = "1" * (m // 2) + s + "1" * ((m + 1) // 2)
    c = 0
    for i in range(n):
        if (s[i] == "1"): 
            qw += ((c + 1) * (n - i))
            c = 0
        else: 
            c+=1
    print(qw)