#include using namespace std; using ll=long long; using pii=pair; #define all(a) a.begin(),a.end() #define pb push_back #define sz(a) ((int)a.size()) const int maxn=1000005,mod=1000000007; int sub(int x, int y){x-=y; if(x<0) x+=mod; return x;} int mul(int x, int y){return ((ll)x)*y%mod;} int Pow(int x, int y=mod-2){int res=1; for(; y; x=mul(x,x),y>>=1) if(y&1) res=mul(res,x); return res;} signed main(){ ios_base::sync_with_stdio(0),cin.tie(0); int t; cin >> t; while(t--){ int a; ll n; cin >> a >> n; ll m=sqrt(a*n); while((m+1)*(m+1)<=a*n) m++; while(m*m>a*n) m--; ll tot=mul(mul(m,m+1),mul(2*m+1,Pow(6))); tot=sub(tot,m); for(int i=0; i