# from debug import debug
import sys
from math import log2, ceil
input = sys.stdin.readline

t = int(input())
while t:
	t-=1
	n, m = map(int, input().split())
	if m == 0:
		print(0)
	elif m >= n//2:
		print(n*(n+1)//2 - (n-m))
	else:
		v = n-2*m+1
		x, y = v//2, ceil(v/2)
		print(x, y)
		print(n*(n+1)//2 - (x*(x+1)//2 + y*(y+1)//2) - (m-1))
