#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
int a0,a1,b0,b1,ans;
int gcd(int a,int b)
{ return b==0?a:gcd(b,a%b); }
int work()
{
if (a0%a1!=0||b1%b0!=0)
return 0;
int a=a0/a1,b=b1/b0;
int i,x; ans=0;
int top=floor(sqrt(b1));
for (i=1;i<=top;i++)
if (b1%i==0)
{
if (i%a1==0)
if (gcd(b,b1/i)==1)
if (gcd(a,i/a1)==1)
ans++;
x=b1/i;
if (x%a1==0)
if (x!=i)
if (gcd(b,b1/x)==1)
if (gcd(a,x/a1)==1)
ans++;
}
return ans;
}
int main()
{
int t,i;
scanf("%d",&t);
for (i=1;i<=t;i++)
{
scanf("%d%d%d%d",&a0,&a1,&b0,&b1);
printf("%d\n",work());
}
return 0;
}