#include <bits/stdc++.h>
using namespace std;
int const N = 1e6 + 41;
long long const INF = 2e18 + 41;
int l[N];
void sieve() {
  l[0] = l[1] = 1;
  for (int i = 2; i < N; ++i) {
    if (l[i] == 0) {
      l[i] = i;
      for (int j = i + i; j < N; j += i)
        if (l[j] == 0 || l[j] > i) l[j] = i;
    }
  }
}
vector<pair<int, int> > vn, vm, vs;
long long n, m, s;
inline int rec(int p, long long v) {
  if (p == (int)(vs.size())) return 1;
  int ret = 0;
  for (int i = 0; i < vs[p].second + 1; ++i) {
    ret += rec(p + 1, v);
    if (n / v < vs[p].first) break;
    v *= vs[p].first;
  }
  return ret;
}
int solveSecond() { return rec(0, 1LL); }
vector<long long> bd;
inline long long rec2(int p, long long v) {
  if (abs(v) > m) return 0LL;
  if (p == (int)(bd.size())) return m / v;
  return rec2(p + 1, v) + rec2(p + 1, -(v * bd[p]));
}
long long solveFirst() {
  bd.clear();
  for (int i = 0; i < (int)(vn.size()); ++i) {
    long long v = 1;
    for (int j = 0; j < vn[i].second; ++j) {
      v *= vn[i].first;
      if (s % v != 0) {
        bd.push_back(v);
        break;
      }
    }
  }
  return rec2(0, 1LL);
}
vector<pair<int, int> > fact(int v) {
  vector<pair<int, int> > ret;
  while (v > 1) {
    int d = 0;
    int k = l[v];
    while (v % k == 0) {
      v /= k;
      ++d;
    }
    ret.push_back(make_pair(k, d));
  }
  return ret;
}
vector<pair<int, int> > merge(vector<pair<int, int> > a,
                              vector<pair<int, int> > b) {
  vector<pair<int, int> > ret;
  for (int i = 0; i < (int)(a.size()); ++i) ret.push_back(a[i]);
  for (int i = 0; i < (int)(b.size()); ++i) ret.push_back(b[i]);
  sort(ret.begin(), ret.end());
  a.clear();
  a = ret;
  ret.clear();
  for (int i = 0; i < (int)(a.size()); ++i)
    if (i == 0 || a[i].first != a[i - 1].first) {
      ret.push_back(a[i]);
    } else
      ret.back().second += a[i].second;
  return ret;
}
void read(long long &val, vector<pair<int, int> > &vec, bool two = false) {
  vec.clear();
  int a, b, c;
  cin >> a >> b >> c;
  val = a * 1LL * b * 1LL * c;
  vec.clear();
  if (two) {
    val *= 2;
    vec.push_back(make_pair(2, 1));
  }
  vec = merge(vec, fact(a));
  vec = merge(vec, fact(b));
  vec = merge(vec, fact(c));
}
void solve() {
  read(n, vn);
  read(m, vm);
  read(s, vs, true);
  long long ans = solveFirst() + solveSecond();
  cout << ans << endl;
}
int main() {
  sieve();
  int test;
  cin >> test;
  while (test--) {
    solve();
  }
  return 0;
}
