#include<stdio.h>
#include<math.h>
#define mod 1000000007
typedef long long ll;
ll euler(ll x)
{
ll res=x,i;
for(i=2;i<=sqrt(x);i++){
if(x%i==0){
res=res/i*(i-1);
while(x%i==0)x/=i;
}
}
if(x>1)res=res/x*(x-1);
return res;
}
void f(ll n,ll m)
{
ll res=0,i;
for(i=1;i<=sqrt(n);i++){
if(n%i==0){
if(n/i!=i&&n/i>=m){
if(i==1||i==2){
res=(res+n/i)%mod;
}
else{
res=(res+euler(i)/2*i*(n/i))%mod;
}
}
if(i>=m){
if(n/i==1||n/i==2){
res=(res+i)%mod;
}
else{
res=(res+euler(n/i)/2*(n/i)*i)%mod;
}
}
}
}
printf("%lld\n",res);
}
int main()
{
ll n,m;
int x;
scanf("%d",&x);
while(x--){
scanf("%lld%lld",&n,&m);
f(n,m);
}
return 0;
}
//求欧拉函数(即n以内所有与n互质的数的个数) // 设n的质因数分别为p1,p2,.....,pn
//f(x)=n*(1-p1)*(1-p2)*...*(1-pn)
//求与n互质的数之和S(x)=f(x)/2*x