#include <bits/stdc++.h>
using namespace std;
long long smallestPrimeDivisor[1000001], n[4], m[4], s[4], c[30];
void Initialise() {
  for (int i = 2; i < 1000001; i++) {
    for (int j = i; j < 1000001; j += i) {
      if (smallestPrimeDivisor[j] == 0) smallestPrimeDivisor[j] = i;
    }
  }
}
long long GetPower(long long n, long long p) {
  long long ans = 0;
  while (n % p == 0) {
    n /= p;
    ans++;
  }
  return ans;
}
int GetTotalValidPrimes(long long N, long long S) {
  int totalValidPrimes = 0;
  set<long long> d;
  d.insert(2);
  for (int i = 1; i <= 3; i++) {
    long long x = n[i];
    while (x > 1) {
      d.insert(smallestPrimeDivisor[x]);
      x /= smallestPrimeDivisor[x];
    }
  }
  for (int i = 1; i <= 3; i++) {
    long long x = s[i];
    while (x > 1) {
      d.insert(smallestPrimeDivisor[x]);
      x /= smallestPrimeDivisor[x];
    }
  }
  S *= 2;
  for (auto x : d) {
    if (GetPower(N, x) <= GetPower(S, x)) continue;
    long long y = GetPower(S, x) + 1, z = 1;
    while (y--) z *= x;
    c[totalValidPrimes++] = z;
  }
  return totalValidPrimes;
}
int main() {
  Initialise();
  int t;
  scanf("%d", &t);
  while (t--) {
    long long N = 1, M = 1, S = 1, ans = 0;
    map<long long, long long> d;
    vector<long long> v;
    for (int i = 1; i <= 3; i++) {
      scanf("%lld", &n[i]);
      N *= n[i];
    }
    for (int i = 1; i <= 3; i++) {
      scanf("%lld", &m[i]);
      M *= m[i];
    }
    for (int i = 1; i <= 3; i++) {
      scanf("%lld", &s[i]);
      S *= s[i];
    }
    int totalValidPrimes = GetTotalValidPrimes(N, S);
    ans = M;
    for (int mask = 1; mask < (1 << totalValidPrimes); mask++) {
      int validPrimes = __builtin_popcount(mask);
      long long ansx = M;
      for (int i = 0; i < totalValidPrimes; i++) {
        if ((mask & (1 << i)) > 0) ansx /= c[i];
      }
      if (validPrimes % 2 == 1)
        ans -= ansx;
      else
        ans += ansx;
    }
    d[2]++;
    for (int i = 1; i <= 3; i++) {
      long long x = s[i];
      while (x > 1) {
        d[smallestPrimeDivisor[x]]++;
        x /= smallestPrimeDivisor[x];
      }
    }
    v.push_back(1);
    for (auto p : d) {
      long long sz = v.size(), x = 1, y, z;
      tie(y, z) = p;
      while (z--) {
        x *= y;
        for (int i = 0; i < sz; i++) v.push_back(x * v[i]);
      }
    }
    for (auto x : v) ans += (x <= N);
    printf("%lld\n", ans);
  }
  return 0;
}
