#include <bits/stdc++.h>
using namespace std;
const int MX = 1010;
vector<int> prime;
void sieve() {
  vector<bool> mark(MX, 0);
  int limit = sqrt(MX + 1);
  prime.push_back(2);
  for (int i = 3; i < MX; i += 2) {
    if (!mark[i]) {
      prime.push_back(i);
      if (i <= limit)
        for (int j = i * i; j < MX; j += 2 * i) mark[j] = 1;
    }
  }
}
long long power(long long base, int pow) {
  long long ans = 1LL;
  while (pow > 0) {
    if (pow & 1) ans *= base;
    pow >>= 1;
    base *= base;
  }
  return ans;
}
void input(long long &N, vector<pair<int, int> > &v) {
  N = 1LL;
  long long a;
  int cnt;
  map<int, int> mp;
  for (int i = 0; i < 3; i++) {
    cin >> a;
    N *= a;
    for (int i = 0; i < prime.size() && prime[i] * prime[i] <= a; i++) {
      if (a % prime[i] == 0) {
        cnt = 0;
        while (a % prime[i] == 0) {
          a /= prime[i];
          cnt++;
        }
        mp[prime[i]] += cnt;
      }
    }
    if (a > 1) mp[a]++;
  }
  for (map<int, int>::iterator it = mp.begin(); it != mp.end(); it++)
    v.push_back(pair<int, int>(it->first, it->second));
}
int count_div(const long long &N, const long long made, const int idx,
              const vector<pair<int, int> > &v) {
  if (idx >= v.size()) return 1;
  int ret = 0;
  long long pr = 1LL, pow = v[idx].second;
  for (int i = 0; i <= pow && pr <= N / made; i++) {
    ret += count_div(N, made * pr, idx + 1, v);
    pr *= v[idx].first;
  }
  return ret;
}
long long gcd_div(const vector<pair<int, int> > &Sm,
                  const vector<pair<int, int> > &Nm, const long long &M) {
  vector<long long> pdiv;
  int i = 0, j = 0;
  while (i < Sm.size()) {
    while (j < Nm.size() && Nm[j].first < Sm[i].first)
      pdiv.push_back(Nm[j++].first);
    if (j < Nm.size() && Nm[j].first == Sm[i].first) {
      if (Nm[j].second > Sm[i].second)
        pdiv.push_back(power(Nm[j].first, Sm[i].second + 1));
      j++;
    }
    i++;
  }
  while (j < Nm.size()) pdiv.push_back(Nm[j++].first);
  long long ans = M, vag;
  int limit = (1 << pdiv.size());
  for (int mask = 1, cnt; mask < limit; mask++) {
    cnt = 0;
    vag = 1LL;
    for (int pos = 0; pos < pdiv.size(); pos++) {
      if (mask & (1 << pos)) {
        cnt++;
        vag *= pdiv[pos];
      }
    }
    if (cnt & 1)
      ans -= M / vag;
    else
      ans += M / vag;
  }
  return ans;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  sieve();
  int T;
  cin >> T;
  while (T--) {
    long long N, M, S, a;
    vector<pair<int, int> > Nm, Mm, Sm;
    input(N, Nm);
    input(M, Mm);
    input(S, Sm);
    S <<= 1;
    if (Sm[0].first == 2)
      Sm[0].second++;
    else
      Sm.insert(Sm.begin(), pair<int, int>(2, 1));
    cout << count_div(N, 1LL, 0, Sm) + gcd_div(Sm, Nm, M) << '\n';
  }
  return 0;
}
