#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
int nn[3], mm[3], ss[3];
map<int, int> factorize(int xx[3]) {
  map<int, int> ret;
  for (int i = (0); i < (3); ++i) {
    int x = xx[i];
    for (int j = 2; j * j <= x; ++j)
      while (x % j == 0) ++ret[j], x /= j;
    if (x != 1) ++ret[x];
  }
  return ret;
}
map<int, int> gcd(map<int, int> a, map<int, int> b) {
  map<int, int> ret;
  for (auto it = a.begin(); it != a.end(); ++it) {
    int c = min(it->second, b[it->first]);
    if (c != 0) ret[it->first] = c;
  }
  return ret;
}
vector<long long> divisors(map<int, int> f) {
  if (((int)(f).size()) == 0) return vector<long long>(1, 1);
  auto it = f.begin();
  int times = it->second, val = it->first;
  f.erase(it);
  vector<long long> sub = divisors(f);
  vector<long long> ret;
  for (int i = (0); i < (((int)(sub).size())); ++i) {
    long long cur = sub[i];
    ret.push_back(cur);
    for (int i = (0); i < (times); ++i) {
      cur *= val;
      ret.push_back(cur);
    }
  }
  return ret;
}
void rec(vector<long long> &cnt, vector<long long> &orig, int idx,
         vector<int> &cur, vector<int> &sz, vector<int> &szprod, int at,
         long long val) {
  if (at >= ((int)(sz).size())) {
    cnt[idx] += val;
  } else {
    rec(cnt, orig, idx, cur, sz, szprod, at + 1, val);
    if (cur[at] + 1 < sz[at])
      rec(cnt, orig, idx + szprod[at], cur, sz, szprod, at + 1, -val);
  }
}
void run() {
  for (int i = (0); i < (3); ++i) scanf("%d", &nn[i]);
  for (int i = (0); i < (3); ++i) scanf("%d", &mm[i]);
  for (int i = (0); i < (3); ++i) scanf("%d", &ss[i]);
  long long n = (long long)nn[0] * nn[1] * nn[2];
  long long m = (long long)mm[0] * mm[1] * mm[2];
  long long s = (long long)ss[0] * ss[1] * ss[2];
  map<int, int> fs = factorize(ss);
  fs[2]++;
  vector<long long> ds = divisors(fs);
  map<int, int> fn = factorize(nn);
  long long ret = 0;
  {
    vector<long long> out;
    for (auto it = fn.begin(); it != fn.end(); ++it) {
      long long now = 1;
      for (int j = (0); j < (it->second); ++j) {
        now *= it->first;
        if (2 * s % now != 0) {
          out.push_back(now);
          break;
        }
      }
    }
    for (int i = (0); i < (1 << ((int)(out).size())); ++i) {
      int cnt = 0;
      long long prod = 1;
      for (int j = (0); j < (((int)(out).size())); ++j)
        if (i & (1 << j)) {
          ++cnt;
          prod *= out[j];
        }
      if (cnt % 2 == 0)
        ret += m / prod;
      else
        ret -= m / prod;
    }
  }
  {
    for (int i = (0); i < (((int)(ds).size())); ++i)
      if (ds[i] <= n) ++ret;
  }
  printf("%lld\n", ret);
}
int main() {
  int n;
  scanf("%d", &n);
  for (int i = (1); i <= (n); ++i) run();
  return 0;
}
