#include<cstdio>
#include<cmath>
inline long long read()
{
long long f=1;
long long k=0;
char c=getchar();
while(c>'9'||c<'0') {
if(c=='-')
f=-1;
c=getchar();
}
while(c>='0'&&c<='9') {
k=k*10+c-'0';
c=getchar();
}
return k*f;
}
inline void print(long long a)
{
if(a==0) {
putchar('0');
putchar('\n');
return ;
}
char c[100];
int q=0;
while(a) {
c[++q]=a%10+'0';
a/=10;
}
for(int i=q;i>=1;--i)
putchar(c[i]);
putchar('\n');
}
long long a0,a1,b0,b1;
inline long long gcd(long long a,long long b) {
register long long r;
while(b) {
r=a%b;
a=b;
b=r;
}
return a;
}
inline long long solve()
{
long long a=a0/a1;
long long b=b1/b0;
long long ans=0;
register long long total=sqrt(b1);
for(register int i=1;i<=total;++i) {
if(b1%i==0) {
if(i%a1==0&&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()
{
// freopen("1.out","w",stdout);
long long n=read();
for(register int i=0;i<n;++i) {
a0=read();
a1=read();
b0=read();
b1=read();
print(solve());
}
return 0;
}