Chef is playing a game with two of his friends. In this game, each player chooses an integer between $1$ and $P$ inclusive. Let's denote the integers chosen by Chef, friend 1 and friend 2 by $i$, $j$ and $k$ respectively; then,  Chef's score is
(((Nmodi)modj)modk)modN.(((Nmodi)modj)modk)modN.(((N\,\mathrm{mod}\,i)\,\mathrm{mod}\,j)\,\mathrm{mod}\,k)\,\mathrm{mod}\,N\,.
Chef wants to obtain the maximum possible score. Let's denote this maximum score by $M$. Find the number of ways to choose the triple $(i,j,k)$ so that Chef's score is equal to $M$.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first and only line of each test case contains two space-separated integers $N$ and $P$.
-----Output-----
For each test case, print a single line containing one integer — the number of ways to obtain the maximum score.
-----Constraints-----
- $1 \le T \le 10^6$
- $1 \le N \le P \le 10^6$
-----Subtasks-----
Subtask #1 (10 points):
- $1 \le T \le 100$
- $1 \le N \le P \le 100$
Subtask #2 (90 points): original constraints
-----Example Input-----
2
4 4
3 4
-----Example Output-----
9
13
-----Explanation-----
Example case 1: Chef's maximum possible score is $M = 1$. All possible values of $(i, j, k)$ such that the score is $1$ are
$(3, 2, 2)$,
$(3, 2, 3)$,
$(3, 2, 4)$,
$(3, 3, 2)$,
$(3, 3, 3)$,
$(3, 3, 4)$,
$(3, 4, 2)$,
$(3, 4, 3)$,
$(3, 4, 4)$.
# -*- coding: utf-8 -*-
"""
@author: anishukla
"""
T = int(input())
for i in range(T):
 N, P = map(int,input().split())
 req = (N//2)+1
 
 if(N==1 or N==2):
  print(P**3)
  
 else:
  req=N%req
  b=(P-req)**2
  c=(P-N)*(P-req)
  a=(P-N)*(P-N)
  print(a+b+c)
# -*- coding: utf-8 -*-
"""
@author: anishukla
"""
T = int(input())
for i in range(T):
 N, P = map(int,input().split())
 req = (N//2)+1
 
 if(N==1 or N==2):
  print(P**3)
  
 else:
  req=N%req
  b=(P-req)**2
  c=(P-N)*(P-req)
  a=(P-N)*(P-N)
  print(a+b+c)