#include <bits/stdc++.h>
using namespace std;
const int limite = 2000005;
int esprimo[limite];
int factor[limite];
void calcula(int n, map<int, int> &f) {
  while (n != 1) {
    f[factor[n]]++;
    n /= factor[n];
  }
}
void calcula(int n1, int n2, int n3, map<int, int> &f) {
  calcula(n1, f);
  calcula(n2, f);
  calcula(n3, f);
}
long long int eleva(long long int b, long long int e) {
  long long int p = 1;
  for (int i = 0; i < e; i++) p *= b;
  return p;
}
long long int computa(map<int, int> fn, long long int m, map<int, int> fs) {
  vector<long long int> v;
  for (map<int, int>::iterator it = fn.begin(); it != fn.end(); it++) {
    int b = it->first;
    if (fs.count(b)) {
      if (fn[b] > fs[b]) v.push_back(eleva(b, fs[b] + 1));
    } else
      v.push_back(b);
  }
  int n = int(v.size());
  long long int sol = 0;
  for (int c = 1; c < 1 << n; c++) {
    long long int p = 1;
    int signo = -1;
    for (int b = 0; b < n; b++) {
      if (c & (1 << b)) {
        p *= v[b];
        signo *= -1;
      }
    }
    sol += signo * (m / p);
  }
  return m - sol;
}
void computa(int iv, long long int p, long long int n,
             vector<vector<long long int> > &v, long long int &sol) {
  if (p > n) return;
  if (iv == int(v.size())) {
    sol++;
    return;
  }
  vector<long long int> &w = v[iv];
  for (int i = 0; i < int(w.size()); i++) computa(iv + 1, p * w[i], n, v, sol);
}
long long int computa(long long int n, map<int, int> fs) {
  vector<vector<long long int> > v;
  for (map<int, int>::iterator it = fs.begin(); it != fs.end(); it++) {
    long long int b = it->first;
    long long int e = it->second;
    v.push_back(vector<long long int>());
    vector<long long int> &w = v.back();
    for (long long int p = 1; e >= 0; p *= b, e--) w.push_back(p);
  }
  long long int sol = 0;
  computa(0, 1, n, v, sol);
  return sol;
}
int main() {
  for (int i = 2; i < limite; i++) esprimo[i] = 1;
  for (int i = 2; i < limite; i++) {
    if (esprimo[i]) {
      factor[i] = i;
      for (int j = i + i; j < limite; j += i) {
        esprimo[j] = 0;
        factor[j] = i;
      }
    }
  }
  int casos;
  cin >> casos;
  for (int cas = 0; cas < casos; cas++) {
    long long int n1, n2, n3;
    long long int m1, m2, m3;
    long long int s1, s2, s3;
    cin >> n1 >> n2 >> n3;
    cin >> m1 >> m2 >> m3;
    cin >> s1 >> s2 >> s3;
    s3 *= 2;
    map<int, int> fn, fm, fs;
    calcula(n1, n2, n3, fn);
    calcula(m1, m2, m3, fm);
    calcula(s1, s2, s3, fs);
    long long int sol =
        computa(fn, m1 * m2 * m3, fs) + computa(n1 * n2 * n3, fs);
    cout << sol << endl;
  }
}
