t=int(input())
while(t>0):
  t-=1
  n,p=map(int,input().split())
  temp=int(n/2)+1
  if(temp!=0):
    x=n%temp
    a=p-x
    df=(a*(a-1))+a
    sub=p-n
    sf=sub*(sub+a)
    print(sf+df)
  else:
    print(p*p*p)
