import sys
import math
from collections import defaultdict
from collections import deque
from itertools import combinations
from itertools import permutations
input = lambda : sys.stdin.readline().rstrip()
read = lambda : list(map(int, input().split()))
go = lambda : 1/0
def write(*args, sep="\n"):
  for i in args:
    sys.stdout.write("{}{}".format(i, sep))
INF = float('inf')
MOD = int(1e9 + 7)
YES = "YES"
NO = "NO"

g = lambda x, n : -x*x + (n+1)*x

def f(cnt, n):
  first = - (( cnt * (cnt + 1) * (2*cnt + 1) ) // 6)
  second = ((n + 1) * cnt * (cnt + 1)) // 2 
  third = 0
  return first + second + third 

def h(x):
  return (x*(x+1))//2

for _ in range(int(input())):
  try:
    n, m = read()

    if m == 0:
      print(0)
      go() 
    
    if n % 2 == 1:
      ans = g((n + 1) // 2, n)
      m -= 1 

      x = n//2
      y = m//2
      ans += 2*(h(x) - h(x-y))
      if m % 2 == 1:
        ans += x - y 
      
      print(ans)
    
    else:
      ans = g(n//2 + 1, n)
      m -= 1

      x = n//2

      if m >= 1:
        ans += x
        m -= 1
      
      y = m//2 
      ans += 2*(h(x - 1) - h(x - 1 - y))
      
      if m % 2 == 1:
        ans += x - y - 1
      
      
      print(ans)
      

        

    
      


    
    
      
      
    
    

  except ZeroDivisionError:
    continue

  except Exception as e:
    print(e)
    continue