#include <bits/stdc++.h>
using namespace std;
int n[3], m[3], s[3];
long long N, M;
map<int, int> mp1, mp2;
vector<pair<int, int> > vp;
vector<long long> v;
long long ans;
void dfs(int p, long long v) {
  if (p == vp.size()) {
    ans++;
    return;
  }
  for (int i = 0; i <= vp[p].second && v <= N; i++, v *= vp[p].first)
    dfs(p + 1, v);
}
int main() {
  int T;
  cin >> T;
  while (T--) {
    scanf("%d%d%d%d%d%d%d%d%d", n, n + 1, n + 2, m, m + 1, m + 2, s, s + 1,
          s + 2);
    N = (long long)n[0] * n[1] * n[2];
    M = (long long)m[0] * m[1] * m[2];
    mp1.clear();
    mp2.clear();
    mp2[2] = 1;
    for (int i = 2; i <= 1001; i++) {
      for (int j = 0; j < 3; j++)
        while (s[j] % i == 0) s[j] /= i, mp2[i]++;
      for (int j = 0; j < 3; j++)
        while (n[j] % i == 0) n[j] /= i, mp1[i]++;
    }
    for (int j = 0; j < 3; j++)
      if (s[j] > 1) mp2[s[j]]++;
    for (int j = 0; j < 3; j++)
      if (n[j] > 1) mp1[n[j]]++;
    v.clear();
    ans = 0;
    vp.clear();
    for (map<int, int>::iterator it = mp2.begin(); it != mp2.end(); it++)
      vp.push_back(make_pair(it->first, it->second));
    for (map<int, int>::iterator it = mp1.begin(); it != mp1.end(); it++)
      if (it->second > mp2[it->first]) {
        long long tmp = 1;
        for (int i = 1; i <= mp2[it->first] + 1; i++) tmp *= it->first;
        v.push_back(tmp);
      }
    dfs(0, 1);
    for (int i = 0; i < (1 << v.size()); i++) {
      long long now = M;
      bool flag = true;
      for (int j = 0; j < (int)v.size(); j++)
        if ((i >> j) & 1) now /= v[j], flag = !flag;
      if (flag)
        ans += now;
      else
        ans -= now;
    }
    printf("%I64d\n", ans);
  }
  return 0;
}
