#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const int N = 2e6 + 20;
const double eps = 1e-8;
int pri[N], mip[N], pos[N], c;
bool vis[N];
long long n, m, s;
long long fac[N], mcnt, pcnt[1000], p[1000];
void prime() {
  c = 0;
  memset((vis), 0, sizeof(vis));
  for (int i = 2; i <= N; i++) {
    if (!vis[i]) pri[c++] = i, mip[i] = i;
    for (int j = 0; j < c && i * pri[j] < N; j++) {
      vis[i * pri[j]] = 1;
      mip[i * pri[j]] = min(mip[i], pri[j]);
      if (i % pri[j] == 0) break;
    }
  }
}
void desp(long long a[]) {
  int t;
  mcnt = 0;
  for (int i = 0; i < 3; i++) {
    while (a[i] > 1) {
      t = mip[a[i]];
      if (!pos[t]) {
        p[++mcnt] = t;
        pos[t] = mcnt;
        pcnt[mcnt] = 0;
      }
      pcnt[pos[t]]++;
      a[i] /= t;
    }
  }
  for (int i = 1; i <= mcnt; i++) pos[p[i]] = 0;
}
long long dfs1(long long x, long long y, int flag) {
  long long ans = 0;
  if (x > mcnt) {
    return ans += m / y * flag;
  }
  ans += dfs1(x + 1, y, flag);
  ans += dfs1(x + 1, y * fac[x], -flag);
  return ans;
}
long long dfs2(long long x, long long y) {
  long long ans = 0;
  if (x > mcnt) {
    if (y <= n) return 1;
    return 0;
  }
  ans += dfs2(x + 1, y);
  for (int i = 1; i <= pcnt[x]; i++) {
    y *= p[x];
    ans += dfs2(x + 1, y);
  }
  return ans;
}
int main() {
  int T;
  long long a[3], b[3], c[3];
  prime();
  cin >> T;
  while (T--) {
    for (int i = 0; i < 3; i++) scanf("%lld", a + i);
    for (int i = 0; i < 3; i++) scanf("%lld", b + i);
    for (int i = 0; i < 3; i++) scanf("%lld", c + i);
    n = a[0] * a[1] * a[2];
    m = b[0] * b[1] * b[2];
    s = 2LL * c[0] * c[1] * c[2];
    desp(a);
    int cnt = 0;
    for (long long i = 1; i <= mcnt; i++) {
      long long t = 1;
      for (int j = 1; j <= pcnt[i]; j++) {
        t = t * p[i];
        if (s % t) {
          fac[++cnt] = t;
          break;
        }
      }
    }
    mcnt = cnt;
    long long ans = 0;
    ans += dfs1(1, 1, 1);
    c[0] *= 2LL;
    desp(c);
    ans += dfs2(1, 1);
    printf("%lld\n", ans);
  }
  return 0;
}
