#include using namespace std; const long long MOD = 1000000007; const long long HALF = 500000004; const long long ONE_SIXTH = 166666668; long long sum1(long long N){ N %= MOD; return N * (N + 1) % MOD * HALF % MOD; } long long sum2(long long N){ N %= MOD; return N * (N + 1) % MOD * (N * 2 + 1) % MOD * ONE_SIXTH % MOD; } int main(){ int T; cin >> T; for (int i = 0; i < T; i++){ long long a, N; cin >> a >> N; long long mx = sqrt(a * N); long long ans = N % MOD * mx % MOD; for (int j = 0; j < a; j++){ long long q = (mx - j) / a; ans += MOD - sum2(q) * a % MOD; ans += MOD - sum1(q) * j * 2 % MOD; if (j != 0){ q++; } long long s; if (j == 0 && a > 1){ s = MOD - 1; } else { s = ((long long) j * j - 1) / a % MOD; } ans += MOD - s * q % MOD; } ans %= MOD; cout << ans << endl; } }