#include <bits/stdc++.h>
using namespace std;
bool dead[3456782];
vector<long long> primes;
void genprimes() {
  primes.push_back(2);
  while (primes.back() < 1000000) {
    long long p = primes.back();
    for (long long i = p * p; i < 1000000; i += p) {
      dead[i] = 1;
    }
    long long i;
    for (i = p + 1; dead[i]; i++)
      ;
    primes.push_back(i);
  }
}
void factor(long long n, map<long long, long long>& mp) {
  for (long long prime : primes) {
    while (n % prime == 0) {
      n /= prime;
      mp[prime]++;
    }
  }
}
int main() {
  genprimes();
  int t;
  cin >> t;
  while (t--) {
    long long n, n1, n2, n3;
    long long m, m1, m2, m3;
    long long s, s1, s2, s3;
    map<long long, long long> fn, fs;
    cin >> n1 >> n2 >> n3 >> m1 >> m2 >> m3 >> s1 >> s2 >> s3;
    n = n1 * n2 * n3, m = m1 * m2 * m3, s = 2 * s1 * s2 * s3;
    factor(n, fn);
    factor(s, fs);
    vector<long long> bad;
    for (auto it : fn) {
      if (it.second > fs[it.first]) {
        long long val = 1;
        for (int i = 0; i < fs[it.first] + 1; i++) val *= it.first;
        bad.push_back(val);
      }
    }
    int sz = bad.size();
    long long bad_cnt = 0;
    for (int mask = 1; mask < (1 << sz); mask++) {
      int cnt = __builtin_popcount(mask);
      long long val = 1;
      for (int i = 0; i < sz; i++) {
        if ((mask >> i) & 1) {
          val *= bad[i];
        }
      }
      if (cnt & 1)
        bad_cnt += m / val;
      else
        bad_cnt -= m / val;
    }
    vector<long long> f;
    f.push_back(1);
    for (auto it : fs) {
      long long e = it.second, z = 1;
      long long szz = f.size();
      while (e--) {
        z *= it.first;
        for (int i = 0; i < szz; i++) {
          if (f[i] * z <= n) f.push_back(f[i] * z);
        }
      }
    }
    cout << m - bad_cnt + f.size() << endl;
  }
  return 0;
}
