#include <bits/stdc++.h>
int gcd(int a, int b) {return !b ? a : gcd(b, a % b);}
int lcm(int a, int b) {return a / gcd(a, b) * b;}
int main(int argc, char const *argv[]) {
int T, a0, a1, b0, b1; std::cin >> T;
while (T--) {
scanf("%d%d%d%d", &a0, &a1, &b0, &b1);
int kn = sqrt(1.0 * b1), ans = 0;
for (int i = 1; i <= kn; i++)
if (b1 % i == 0) {
if (gcd(i, a0) == a1 and lcm(i, b0) == b1) ans++;
if (i != b1 / i and gcd(b1 / i, a0) == a1 and lcm(b1 / i, b0) == b1) ans++;
}
printf("%d\n", ans);
}
return 0;
}