#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int> > vc, va, vb;
int a[3], b[3], c[3];
long long n, m, s, p[50], pp[1 << 20];
void fac(int k, vector<pair<int, int> > &v) {
  int m = sqrt(k);
  for (int i = 2; i <= m; i++)
    if (k % i == 0) {
      int cnt = 0;
      while (k % i == 0) k /= i, cnt++;
      v.push_back(pair<int, int>(i, cnt));
    }
  if (k > 1) v.push_back(pair<int, int>(k, 1));
  return;
}
void func(vector<pair<int, int> > &v) {
  sort(v.begin(), v.end());
  vector<pair<int, int> > w;
  for (auto first : v) {
    if (!w.empty() && w.back().first == first.first)
      w.back().second += first.second;
    else
      w.push_back(first);
  }
  v = w;
  return;
}
long long ans;
void calc(int first, long long up) {
  if (first == vc.size()) {
    ans++;
    return;
  }
  if (up < vc[first].first) {
    ans++;
    return;
  }
  int al = vc[first].second;
  for (int i = 0; i <= al; i++) {
    calc(first + 1, up);
    up /= vc[first].first;
    if (up == 0) break;
  }
  return;
}
int main() {
  int T;
  for (scanf("%d", &T); T--;) {
    scanf("%d %d %d", &a[0], &a[1], &a[2]);
    scanf("%d %d %d", &b[0], &b[1], &b[2]);
    scanf("%d %d %d", &c[0], &c[1], &c[2]);
    long long m = 1ll * b[0] * b[1] * b[2];
    va.clear();
    vb.clear(), vc.clear();
    ans = 0;
    for (int i = 0; i < 3; i++) fac(a[i], va);
    for (int i = 0; i < 3; i++) fac(c[i], vc);
    func(va), func(vc);
    if (vc[0].first == 2)
      vc[0].second++;
    else
      vc.insert(vc.begin(), pair<int, int>(2, 1));
    n = 1ll * a[0] * a[1] * a[2];
    m = 1ll * b[0] * b[1] * b[2];
    calc(0, n);
    int sz = va.size();
    for (int i = 0; i < sz; i++) {
      int k =
          lower_bound(vc.begin(), vc.end(), pair<int, int>(va[i].first, 0)) -
          vc.begin();
      int deg = 0;
      if (k < vc.size() && vc[k].first == va[i].first) deg = vc[k].second;
      if (va[i].second > deg)
        vb.push_back(pair<int, int>(va[i].first, deg + 1));
    }
    sz = vb.size();
    for (int i = 0; i < sz; i++) {
      int q = vb[i].first, r = vb[i].second;
      p[i] = 1;
      while (r--) p[i] *= q;
    }
    int up = 1 << sz;
    pp[0] = 1;
    for (int i = 1; i < up; i++) {
      int t = __builtin_ctz(i);
      pp[i] = pp[i - (1 << t)] * p[t];
    }
    for (int i = 0; i < up; i++) {
      int k = __builtin_popcount(i);
      if (k & 1)
        ans -= m / pp[i];
      else
        ans += m / pp[i];
    }
    printf("%I64d\n", ans);
  }
  return 0;
}
