def f(n): a=int(n**0.5) for i in range(-3,3): if (a+i)**2<=n<(a+i+1)**2: return a+i T=int(input()) mod=10**9+7 for _ in range(T): a,N=map(int,input().split()) ans=0 for i in range(1,a): if i>N: break ans+=f(a*i) ans%=mod if a-1>=N: print(ans) continue d=dict() for i in range(a,4*a): s=f(i*a) if s not in d: d[s]=0 d[s]+=1 x=f(N//a) #x>=1 s1=0 s2=0 s3=3*a kosu=a for i in d: s1+=i*d[i]%mod s2+=i s1%=mod s2%=mod if x>=2: ans+=s1*(x-1)%mod ans+=(2*a*kosu%mod*((x-2)*(x-1)*(2*x-3))//6)%mod ans+=(((x-2)*(x-1))//2)*a%mod*s3%mod ans+=(((x-2)*(x-1))//2)*2*s2%mod ans%=mod #(x**2)*a~N kk=N-((x**2)*a)+1 for i in d: if kk-(d[i]+2*(x-1))<=0: ans+=kk*(i+a*(x-1))%mod ans%=mod break else: kk-=(d[i]+2*(x-1)) kk%=mod ans+=(i+a*(x-1))*(d[i]+2*(x-1))%mod ans%=mod print(ans)