#include <bits/stdc++.h>
using namespace std;
const int inf = 1 << 30;
const int md = 1e9 + 7;
int n, m;
int T;
const int maxn = 1e6 + 10;
bool b[maxn];
int p[maxn];
long long mpow(long long x, int k) {
  long long re = 1;
  while (k) {
    if (k & 1) re = re * x;
    x = x * x;
    k /= 2;
  }
  return re;
}
struct point {
  long long val;
  vector<int> v;
  vector<int> c;
  point() { val = 1; }
  void work(long long x) {
    val = x;
    v.clear();
    c.clear();
    int now = 0;
    while (x != 1) {
      if (x % p[now] == 0) {
        v.push_back(p[now]);
        c.push_back(0);
        while (x % p[now] == 0) {
          x /= p[now];
          c.back()++;
        }
      }
      now++;
    }
  }
};
point operator+(point a, point b) {
  point re;
  re.work(a.val * b.val);
  return re;
}
long long save[maxn];
int mp[maxn];
int cal[maxn];
int pn;
void pre() {
  for (int i = 0; (1 << i) < maxn; i++) mp[1 << i] = i;
  for (int i = 1; i < maxn; i++) cal[i] = cal[i ^ (i & -i)] + 1;
  for (int i = 2; i < maxn; i++) {
    if (b[i]) continue;
    p[pn++] = i;
    for (int j = i << 1; j < maxn; j += i) b[j] = 1;
  }
}
long long work(point n, point m, point s) {
  long long vs = s.val;
  long long vm = m.val;
  long long re = vm;
  int l = 0, r = 0;
  vector<long long> ve;
  for (; l < n.v.size(); l++) {
    while (r < s.v.size() && s.v[r] < n.v[l]) r++;
    if (r >= s.v.size() || s.v[r] != n.v[l]) {
      ve.push_back(n.v[l]);
    } else if (s.c[r] < n.c[l]) {
      ve.push_back(mpow(s.v[r], s.c[r] + 1));
    }
  }
  save[0] = 1;
  for (int st = 1; st < (1 << ve.size()); st++) {
    save[st] = save[st ^ (st & -st)] * ve[mp[st & -st]];
    if (cal[st] & 1)
      re -= vm / save[st];
    else
      re += vm / save[st];
  }
  return re;
}
long long dfs(int dep, long long now, long long& n, point& s) {
  if (dep == s.v.size()) return 1;
  long long re = 0;
  for (int i = 0; i <= s.c[dep]; i++) {
    if (now < 0 || now > n) return re;
    re += dfs(dep + 1, now, n, s);
    now *= s.v[dep];
  }
  return re;
}
long long solve(point& n, point& m, point& s) {
  long long re = 0;
  long long vn = n.val;
  re = dfs(0, 1, vn, s);
  re += work(n, m, s);
  return re;
}
int main() {
  pre();
  scanf("%d", &T);
  while (T--) {
    point n, m, s;
    point t;
    int x;
    for (int i = 0; i < 3; i++) {
      scanf("%d", &x);
      t.work(x);
      n = n + t;
    }
    for (int i = 0; i < 3; i++) {
      scanf("%d", &x);
      t.work(x);
      m = m + t;
    }
    for (int i = 0; i < 3; i++) {
      scanf("%d", &x);
      t.work(x);
      s = s + t;
    }
    t.work(2);
    s = s + t;
    long long ans = solve(n, m, s);
    printf("%I64d\n", ans);
  }
  return 0;
}
