#include<bits/stdc++.h>
using namespace std;
int n,a0,a1,b0,b1;
int gcdx(int a,int b)
{
return !b?a:gcdx(b,a%b);
}
int gcdy(int a,int b)//lcm
{
return a/gcdx(a,b)b;
}
int main()
{
cin>>n;
while(n--)
{
cin>>a0>>a1>>b0>>b1;
int m=sqrt(1.0b1),ans=0;
for(int i=1;i<=m;i++)
{
if(b1%i==0)
{
if(gcdx(i,a0)==a1 and gcdy(i,b0)==b1)
{
ans++;
}
if(i!=b1/i and gcdx(b1/i,a0)==a1 and gcdy(b1/i,b0)==b1)
{
ans++;
}
}
}
cout<<ans<<endl;
}
return 0;
}