#include<cstdio>
#include<cmath>
long long a0,a1,b0,b1;
long long gcd(long long a,long long b) {
long long r;
while(b) {
r=a%b;
a=b;
b=r;
}
return a;
}
long long solve()
{
long long a=a0/a1;
long long b=b1/b0;
long long ans=0;
long long total=sqrt(b1);
for(int i=1;i<=total;++i) {
if(i%a1==0&&b1%i==0) {
if(gcd(b,b1/i)==1&&gcd(a,i/a1)==1)
++ans;
int x=b1/i;
if(x%a1==0&&x!=i&&gcd(b,b1/x)==1&&gcd(a,x/a1)==1)
++ans;
}
}
return ans;
}
int main()
{
long long n;
scanf("%lld",&n);
for(int i=0;i<n;++i) {
scanf("%lld %lld %lld %lld",&a0,&a1,&b0,&b1);
printf("%lld\n",solve());
}
return 0;
}