#include <bits/stdc++.h>
using namespace std;
const int MAXN = 105;
int pn[MAXN], ps[MAXN];
void fac(int *A, int x) {
  for (int d = 2; d * d <= x; d++) {
    if (x % d == 0) {
      A[++A[0]] = d;
      while (x % d == 0) x /= d;
    }
  }
  if (x != 1) A[++A[0]] = x;
}
int get(long long n, int p) {
  int tot = 0;
  while (n % p == 0) n /= p, tot++;
  return tot;
}
long long ksm(long long a, int b) {
  long long s = 1;
  for (int i = 1; i <= b; i++) s *= a;
  return s;
}
int n1, n2, n3, m1, m2, m3, s1, s2, s3;
long long n, m, s;
const int MAXS = 1e6 + 5;
int hn[MAXS], hs[MAXS], h[MAXS];
long long Ans;
void Dfs(int d, long long x) {
  if (d > ps[0]) {
    Ans++;
    return;
  }
  int k = h[d];
  for (int i = 0; i <= k && x <= n; i++, x *= ps[d]) Dfs(d + 1, x);
}
int main() {
  int T;
  cin >> T;
  while (T--) {
    pn[0] = ps[0] = 0;
    Ans = 0;
    cin >> n1 >> n2 >> n3 >> m1 >> m2 >> m3 >> s1 >> s2 >> s3;
    s1 <<= 1;
    n = 1ll * n1 * n2 * n3, m = 1ll * m1 * m2 * m3, s = 1ll * s1 * s2 * s3;
    fac(pn, n1);
    fac(pn, n2);
    fac(pn, n3);
    fac(ps, s1);
    fac(ps, s2);
    fac(ps, s3);
    sort(pn + 1, pn + pn[0] + 1);
    sort(ps + 1, ps + ps[0] + 1);
    pn[0] = unique(pn + 1, pn + pn[0] + 1) - pn - 1;
    ps[0] = unique(ps + 1, ps + ps[0] + 1) - ps - 1;
    for (int i = 1; i <= pn[0]; i++)
      hn[i] = get(n, pn[i]), hs[i] = get(s, pn[i]);
    for (int i = 1; i <= ps[0]; i++) h[i] = get(s, ps[i]);
    for (int S = 0; S < (1 << pn[0]); S++) {
      long long tmp = 1;
      int f = 1;
      for (int i = 1; i <= pn[0]; i++)
        if (S >> (i - 1) & 1) {
          int kn = hn[i], ks = hs[i];
          if (kn <= ks)
            f = 0;
          else {
            tmp *= ksm(pn[i], ks + 1);
            f *= -1;
          }
        }
      Ans += m / tmp * f;
    }
    Dfs(1, 1);
    cout << Ans << '\n';
  }
  return 0;
}
