//lcm(a,b)*gcd(a,b)==a*b;
//枚举;
#include<cstdio>
int n,m,a1,a0,b1,b0,ans=0;
int gcd(int a,int b){
return b?gcd(b,a%b):a;
}
int main(){
scanf("%d",&n);
for(int q=1;q<=n;q++){
ans=0;
scanf("%d%d%d%d",&a0,&a1,&b0,&b1);
for(int i=1;i*i<=b1;i++){
if((b1/i)*i==b1){
for(int k=0;k<2;k++){
m=k==0?i:b1/i;
if(k&&i==b1/i) break; //i和b1/i都要枚举是不是解,但i!=b1/i;
if((m%a1==0)&&(gcd(m,a0)==a1)&&(b1*gcd(b0,m)==m*b0)) ans++;
}
}
}
printf("%d\n",ans);
}
return 0;
}