modulo question

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.
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≤T≤106
1≤N≤P≤106

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).
can anyone help me in this question please?
Thanks
And? What is your question? What code have you written? What problems are you having with that code?

If this is a CodeChef challenge, you should know that the CodeChef adjudicators are aware that people are using this forum to cheat, and that they have disqualified people for doing so in the past.
Topic archived. No new replies allowed.