#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <vector>
#include <stack>
#include <map>
#include <set>
#include <sstream>
#include <iomanip>
#include <cmath>
#include <bitset>
#include <assert.h>
using namespace std;
typedef long long ll;
typedef set<int>::iterator ssii;
#define Cmp(a, b) memcmp(a, b, sizeof(b))
#define Cpy(a, b) memcpy(a, b, sizeof(b))
#define Set(a, v) memset(a, v, sizeof(a))
#define debug(x) cout << #x << ": " << x << endl
#define _forS(i, l, r) for(set<int>::iterator i = (l); i != (r); i++)
#define _rep(i, l, r) for(int i = (l); i <= (r); i++)
#define _for(i, l, r) for(int i = (l); i < (r); i++)
#define _forDown(i, l, r) for(int i = (l); i >= r; i--)
#define debug_(ch, i) printf(#ch"[%d]: %d\n", i, ch[i])
#define debug_m(mp, p) printf(#mp"[%d]: %d\n", p->first, p->second)
#define debugS(str) cout << "dbg: " << str << endl;
#define debugArr(arr, x, y) _for(i, 0, x) { _for(j, 0, y) printf("%c", arr[i][j]); printf("\n"); }
#define _forPlus(i, l, d, r) for(int i = (l); i + d < (r); i++)
#define lowbit(i) (i & (-i))
#define MPR(a, b) make_pair(a, b)
pair<int, int> crack(int n) {
int st = sqrt(n);
int fac = n / st;
while (n % st) {
st += 1;
fac = n / st;
}
return make_pair(st, fac);
}
inline ll qpow(ll a, int n) {
ll ans = 1;
for(; n; n >>= 1) {
if(n & 1) ans *= 1ll * a;
a *= a;
}
return ans;
}
template <class T>
inline bool chmax(T& a, T b) {
if(a < b) {
a = b;
return true;
}
return false;
}
template <class T>
inline bool chmin(T& a, T b) {
if(a > b) {
a = b;
return true;
}
return false;
}
bool _check(int x) {
//
return true;
}
int bsearch1(int l, int r) {
while (l < r) {
int mid = (l + r) >> 1;
if(_check(mid)) r = mid;
else l = mid + 1;
}
return l;
}
int bsearch2(int l, int r) {
while (l < r) {
int mid = (l + r + 1) >> 1;
if(_check(mid)) l = mid;
else r = mid - 1;
}
return l;
}
// ============================================================== //
typedef pair<int, int> PII;
const int maxn = 50000 + 10;
inline ll gcd(ll a, ll b) {
return b ? gcd(b, a%b) : a;
}
ll a0, a1, b0, b1;
bool fl[maxn];
vector<int> primes;
void getPrimes(int n) {
memset(fl, 0, sizeof(fl));
primes.clear();
for(int i = 2; i <= n; i++) {
if(fl[i]) continue;
primes.push_back(i);
for(int j = i; j <= n/i; j++) fl[i*j] = true;
}
}
vector<PII> fac;
void prework(ll d) {
for(const auto& p : primes) {
if(d % p == 0) {
int c = 0;
while (d % p == 0) c++, d /= p;
fac.push_back(make_pair(p, c));
}
}
if(d > 1) fac.push_back(make_pair(d, 1));
}
void dfs(ll k, ll x, vector<ll>& div) {
if(k == fac.size()) {
div.push_back(x);
return;
}
for(int i = 0; i <= fac[k].second; i++) {
dfs(k+1, x, div);
x *= fac[k].first;
}
}
void init() {
fac.clear();
}
int main() {
freopen("input.txt", "r", stdin);
getPrimes(maxn);
int kase;
cin >> kase;
while (kase--) {
init();
scanf("%lld%lld%lld%lld", &a0, &a1, &b0, &b1);
prework(b1);
vector<ll> div;
dfs(0, 1, div);
ll ans = 0;
for(auto x : div) {
if(gcd(x, a0) == a1 && gcd(x, b0) * b1 == x * b0) ans++;
}
printf("%lld\n", ans);
}
}