#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1000010;
int nowarn;
int n1, n2, n3, m1, m2, m3, s1, s2, s3;
long long n, m, s, ans;
int sp[MAXN], spcnt, np[MAXN], npcnt;
int spw[MAXN] = {0}, npw[MAXN] = {0};
void addfac(int n, int *prm, int &cnt, int *pw) {
  for (int i = 2; i * i <= n; ++i)
    if (n % i == 0) {
      prm[cnt++] = i;
      while (n % i == 0) ++pw[i], n /= i;
    }
  if (n > 1) prm[cnt++] = n, ++pw[n];
}
void dfs1(int idx, long long num) {
  if (num > n) return;
  if (idx == spcnt) {
    ++ans;
    return;
  }
  for (int i = 0; i <= spw[sp[idx]]; ++i, num *= sp[idx]) dfs1(idx + 1, num);
}
void solve1() { dfs1(0, 1); }
void dfs2(int idx, long long num, int cnt, long long *p, int size) {
  if (idx == size) {
    ans += (m / num) * (cnt & 1 ? -1 : 1);
    return;
  }
  dfs2(idx + 1, num, cnt, p, size);
  dfs2(idx + 1, num * p[idx], cnt + 1, p, size);
}
void solve2() {
  long long tmp[20];
  int cnt = 0;
  for (int i = 0; i < npcnt; ++i)
    if (npw[np[i]] > spw[np[i]]) {
      tmp[cnt] = np[i];
      for (int t = 0; t < spw[np[i]]; ++t) tmp[cnt] *= np[i];
      ++cnt;
    }
  dfs2(0, 1, 0, tmp, cnt);
}
int main() {
  int T;
  nowarn = scanf("%d", &T);
  while (T--) {
    nowarn = scanf("%d%d%d%d%d%d%d%d%d", &n1, &n2, &n3, &m1, &m2, &m3, &s1, &s2,
                   &s3);
    s1 <<= 1, n = (long long)n1 * n2 * n3, m = (long long)m1 * m2 * m3,
              s = (long long)s1 * s2 * s3;
    ans = 0, npcnt = spcnt = 0;
    addfac(n1, np, npcnt, npw);
    addfac(n2, np, npcnt, npw);
    addfac(n3, np, npcnt, npw);
    addfac(s1, sp, spcnt, spw);
    addfac(s2, sp, spcnt, spw);
    addfac(s3, sp, spcnt, spw);
    sort(np, np + npcnt);
    sort(sp, sp + spcnt);
    npcnt = int(unique(np, np + npcnt) - np);
    spcnt = int(unique(sp, sp + spcnt) - sp);
    solve1(), solve2();
    for (int i = 0; i < npcnt; ++i) npw[np[i]] = 0;
    for (int i = 0; i < spcnt; ++i) spw[sp[i]] = 0;
    printf("%lld\n", ans);
  }
  return 0;
}
