#include<stdio.h>
#include<math.h>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int N=200005;
int T,S,pr[N],pc;
ll n,num[N],m,K;
ull g[N];
bool fl[N];
inline int ID(ll x){return x<=S?x:m-n/x+1;}
ull f(ll n,int i){
if(n<1||pr[i]>n)return 0;
ull ret=g[ID(n)]-(i-1)*(K+1);
while((ll)pr[i]*pr[i]<=n){
int p=pr[i];
ull e=K+1,t=n/p;
while(t>=p)ret+=f(t,i+1)*e+e+K,t/=p,e+=K;
i++;
}
return ret;
}
ull solve(ll n){
int i,p,x;ull y;
S=sqrt(n);
while((ll)S*S<=n)S++;
while((ll)S*S>n)S--;
while(m)num[m--]=0;
for(i=1;i<=S;i++)num[++m]=i;
for(i=S;i>=1;i--)if(n/i>S)num[++m]=n/i;
for(i=1;i<=m;i++)g[i]=num[i]-1;
x=1;y=0;
for(p=2;p<=S;p++)if(g[p]!=g[p-1]){
while(num[x]<(ll)p*p)x++;
for(i=m;i>=x;i--)g[i]-=g[ID(num[i]/p)]-y;
y++;
}
for(i=1;i<=m;i++)g[i]*=K+1;
return f(n,1)+1;
}
int main(){
int i,j;
for(i=2;i<N;i++)if(!fl[i]){
pr[++pc]=i;
for(j=i+i;j<N;j+=i)fl[j]=1;
}
for(scanf("%d",&T);T--;){
scanf("%lld%lld",&n,&K);
printf("%llu\n",solve(n));
}
return 0;
}
I2luY2x1ZGU8c3RkaW8uaD4KI2luY2x1ZGU8bWF0aC5oPgoKdXNpbmcgbmFtZXNwYWNlIHN0ZDsKdHlwZWRlZiB1bnNpZ25lZCBsb25nIGxvbmcgdWxsOwp0eXBlZGVmIGxvbmcgbG9uZyBsbDsKCmNvbnN0IGludCBOPTIwMDAwNTsKCmludCBULFMscHJbTl0scGM7CmxsIG4sbnVtW05dLG0sSzsKdWxsIGdbTl07CmJvb2wgZmxbTl07CmlubGluZSBpbnQgSUQobGwgeCl7cmV0dXJuIHg8PVM/eDptLW4veCsxO30KCnVsbCBmKGxsIG4saW50IGk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