#include <bits/stdc++.h>
using namespace std;
const int N = 1000010;
int t, sp[N];
map<int, int> n, m, s;
vector<pair<int, int> > _n, _m, _s;
long long n_val, m_val, s_val;
void sieve(void) {
  for (int i = 2; i * i < N; ++i) {
    if (sp[i]) continue;
    sp[i] = i;
    for (int j = i * i; j < N; j += i) {
      if (sp[j]) continue;
      sp[j] = i;
    }
  }
  for (int i = 2; i < N; ++i) {
    if (sp[i]) continue;
    sp[i] = i;
  }
}
int size;
long long Less;
void backtrack(int at, long long val) {
  if (at == size) {
    if (val <= n_val) ++Less;
    return;
  }
  for (int i = 0; i <= _s[at].second; ++i) {
    backtrack(at + 1, val);
    val *= 1LL * _s[at].first;
  }
  return;
}
int main() {
  sieve();
  scanf("%d", &t);
  while (t--) {
    n.clear(), m.clear(), s.clear();
    _n.clear(), _m.clear(), _s.clear();
    for (int i = 0; i < 3; ++i) {
      int x;
      scanf("%d", &x);
      while (x > 1) {
        int p = sp[x];
        while (x % p == 0) {
          ++n[p], x /= p;
        }
      }
    }
    for (int i = 0; i < 3; ++i) {
      int x;
      scanf("%d", &x);
      while (x > 1) {
        int p = sp[x];
        while (x % p == 0) {
          ++m[p], x /= p;
        }
      }
    }
    for (int i = 0; i < 3; ++i) {
      int x;
      scanf("%d", &x);
      while (x > 1) {
        int p = sp[x];
        while (x % p == 0) {
          ++s[p], x /= p;
        }
      }
    }
    ++s[2];
    n_val = m_val = s_val = 1LL;
    for (auto it : n) {
      _n.emplace_back(it.first, it.second);
      for (int i = 0; i < it.second; ++i) {
        n_val *= 1LL * it.first;
      }
    }
    for (auto it : m) {
      _m.emplace_back(it.first, it.second);
      for (int i = 0; i < it.second; ++i) {
        m_val *= 1LL * it.first;
      }
    }
    for (auto it : s) {
      _s.emplace_back(it.first, it.second);
      for (int i = 0; i < it.second; ++i) {
        s_val *= 1LL * it.first;
      }
    }
    int sz = _n.size();
    long long tot = m_val;
    for (int mask = 1; mask < 1 << sz; ++mask) {
      int nope = 0, on = 0;
      long long num = 1LL;
      for (int i = 0; i < sz; ++i) {
        if (!(mask & 1 << i)) continue;
        ++on;
        int p = _n[i].first, e = _n[i].second;
        if (s[p] + 1 > e) {
          nope = 1;
          break;
        }
        for (int j = 0; j <= s[p]; ++j) {
          num *= 1LL * p;
        }
      }
      if (nope) continue;
      long long cur = m_val / num;
      if (on & 1) cur = -cur;
      tot += cur;
    }
    Less = 0;
    size = _s.size();
    backtrack(0, 1LL);
    tot += Less;
    printf("%lld\n", tot);
  }
  return 0;
}
