#include<bits/stdc++.h>
using namespace std;
inline int read(){
register int s=0,w=1;
char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
while(ch>='0'&&ch<='9')s=s*10+ch-'0',ch=getchar();
return s*w;
}
int main(){
int t,n,p,i,sum=0;
t=read();
for(int a=0;a<t;a++){
n=read();
p=read();
for(int j=0;j<=1000;j++){
i=floor(n/pow(p,j));
if(i==0)break;
sum+=(i*pow(-1,j));
}
cout<<sum;
if(a!=t-1)cout<<endl;
}
return 0;
}
思路:先选择不被p整除的数,再选择只有两个因数p的数,再选择只有四个因数p的数……