// #cheatkhitacontre #khionhatoicheat #include #define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0) #define int long long /*fuck int*/ using namespace std; int mod = 1000000007; int Sqrt(int x) { int ret = 0; for (int i = (1ll<<31); i >= 1; i>>=1) if ((ret+i)*(ret+i)<=x) ret += i; return ret; } int mul(int a, int b) { return (a*b)%mod; } int binpow(int a, int b) { int ret = 1; while (b) { if (b&1) ret = mul(ret,a); a = mul(a,a); b >>= 1; } return ret; } int Sum(int a, int b) { return (a+b+mod)%mod; } int sub(int a, int b) { return (a-b+mod)%mod; } int Div(int a, int b) { return mul(a,binpow(b,mod-2)); } void solve() { int n, a, m, ss = 0, s = 0; cin >> a >> n; m = Sqrt(a*n); vector dp(a+1,0); for (int i = 1; i <= a; i++) dp[i] = Sum(dp[i],(i*i-1)%a+1); for (int i = 1; i <= a; i++) s = Sum(s,dp[i]); for (int i = 1; i <= m%a; i++) ss = Sum(ss,dp[i]); cout << sub(mul(m,n),Div(sub(Div(mul(mul(m,m+1),2*m+1),6ll),Sum(mul(s,m/a),ss)),a)) << "\n"; } signed main() { IOS; int TEST=1; cin >> TEST; while (TEST--) solve(); }