#include <bits/stdc++.h>
using namespace std;
int sd[3300342], maxD = 3300000;
vector<pair<int, int> > sDivs, nDivs;
long long ans1, ans2, ss, nn, mm;
vector<long long> bad;
void preCalc() {
  for (int i = 0; i < maxD; i++) {
    sd[i] = -1;
  }
  sd[0] = 1;
  sd[1] = 1;
  for (int i = 2; i < maxD; i++) {
    if (sd[i] != -1) {
      continue;
    }
    for (int j = i; j < maxD; j += i) {
      if (sd[j] == -1) {
        sd[j] = i;
      }
    }
  }
}
vector<pair<int, int> > getDivs(int n[3]) {
  vector<int> d;
  vector<pair<int, int> > ret;
  for (int i = 0; i < 3; i++) {
    int c = n[i];
    for (; c != 1;) {
      d.push_back(sd[c]);
      c /= sd[c];
    }
  }
  sort(d.begin(), d.end());
  for (int i = 0; i < d.size();) {
    int j = i;
    for (; j < d.size() && d[i] == d[j]; j++) {
    }
    ret.push_back(make_pair(d[i], j - i));
    i = j;
  }
  return ret;
}
void cntDivs(int ind, long long val) {
  if (ind == sDivs.size()) {
    ans2 += (val <= nn);
    return;
  }
  for (int i = 0; i <= sDivs[ind].second; i++) {
    cntDivs(ind + 1, val);
    val *= sDivs[ind].first;
  }
}
void calcInEx(int ind, long long val, int k) {
  if (ind == bad.size()) {
    ans1 += k * mm / val;
    return;
  }
  calcInEx(ind + 1, val, k);
  calcInEx(ind + 1, val * bad[ind], -k);
}
long long solve(int n[3], int m[3], int s[3]) {
  ans1 = 0;
  ans2 = 0;
  bad.clear();
  nn = 1LL * n[0] * n[1] * n[2];
  mm = 1LL * m[0] * m[1] * m[2];
  ss = 1LL * s[0] * s[1] * s[2];
  sDivs = getDivs(s);
  cntDivs(0, 1);
  nDivs = getDivs(n);
  for (int i = 0; i < nDivs.size(); i++) {
    long long c = 1;
    for (int j = 0; j <= nDivs[i].second; j++) {
      if (ss % c) {
        bad.push_back(c);
        break;
      }
      c *= nDivs[i].first;
    }
  }
  calcInEx(0, 1, 1);
  return ans1 + ans2;
}
int main() {
  preCalc();
  int t;
  cin >> t;
  for (int i = 0; i < t; i++) {
    int n[3], m[3], s[3];
    cin >> n[0] >> n[1] >> n[2];
    cin >> m[0] >> m[1] >> m[2];
    cin >> s[0] >> s[1] >> s[2];
    s[0] *= 2;
    cout << solve(n, m, s) << "\n";
  }
  return 0;
}
