#include #include using namespace atcoder; using namespace std; using ll = long long; ll A, N,T; using mint = modint1000000007; mint B[1000006]; ll sqrtz(ll N) { ll L = 0, R = 2e9; while (abs(R - L) > 1) { ll mid = (R + L) / 2; if (mid * mid <= N)L = mid; else R = mid; } return L; } mint f(ll m) { ll r = ll(sqrt(m * A)); mint R = r; mint res = r * mint(m); mint D = R * (R - 1) * (2 * R + 5) / 6 - mint(r / A) * B[A] - B[r % A]; return res - D / A; } int main() { cin >> T; while(T){ cin >> A >> N; T--; for (ll i = 1; i <= A; i++)B[i] = B[i - 1] + ((i * i - 1) % A); cout << f(N).val() << endl; } }