#include <bits/stdc++.h>
using namespace std;
int pr[2100000], pl, minp[2100000], p[100], d[100], w[2100000], num;
long long f[2100000];
bool b[2100000];
long long n, m, s, ans, lim;
long long gcd(long long a, long long b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
void get_p() {
  for (int i = 2; i < 2100000; i++) {
    if (b[i] == 0) pr[++pl] = i, minp[i] = i;
    for (int j = 1; j <= pl; j++) {
      if (i * pr[j] >= 2100000) break;
      b[i * pr[j]] = 1;
      minp[i * pr[j]] = min(minp[i], pr[j]);
      if (i % pr[j] == 0) break;
    }
  }
}
void dps(int n1, int n2, int n3) {
  num = 0;
  while (n1 > 1) {
    int t = minp[n1];
    if (w[t] == 0) p[++num] = t, d[num] = 0, w[t] = num;
    d[w[t]]++;
    n1 /= t;
  }
  while (n2 > 1) {
    int t = minp[n2];
    if (w[t] == 0) p[++num] = t, d[num] = 0, w[t] = num;
    d[w[t]]++;
    n2 /= t;
  }
  while (n3 > 1) {
    int t = minp[n3];
    if (w[t] == 0) p[++num] = t, d[num] = 0, w[t] = num;
    d[w[t]]++;
    n3 /= t;
  }
  for (int i = 1; i <= num; i++) w[p[i]] = 0;
}
void dfs1(int x, long long y, int o) {
  if (x > num) {
    ans += lim / y * o;
    return;
  }
  dfs1(x + 1, y, o);
  dfs1(x + 1, y * f[x], -o);
}
void solve1(long long n) {
  lim = n;
  dfs1(1, 1, 1);
}
void dfs2(int x, long long y) {
  if (x > num) {
    if (y <= lim) ans++;
    return;
  }
  dfs2(x + 1, y);
  for (int i = 1; i <= d[x]; i++) {
    y = y * p[x];
    dfs2(x + 1, y);
  }
}
void solve2(long long n) {
  lim = n;
  dfs2(1, 1);
}
int main() {
  get_p();
  int z;
  scanf("%d", &z);
  while (z--) {
    int n1, n2, n3, m1, m2, m3, s1, s2, s3;
    scanf("%d%d%d%d%d%d%d%d%d", &n1, &n2, &n3, &m1, &m2, &m3, &s1, &s2, &s3);
    n = 1ll * n1 * n2 * n3;
    m = 1ll * m1 * m2 * m3;
    s = 2ll * s1 * s2 * s3;
    ans = 0;
    dps(n1, n2, n3);
    int tot = 0;
    for (int i = 1; i <= num; i++) {
      long long t = 1;
      for (int j = 1; j <= d[i]; j++) {
        t = t * p[i];
        if (s % t) {
          f[++tot] = t;
          break;
        }
      }
    }
    num = tot;
    solve1(m);
    dps(2 * s1, s2, s3);
    solve2(n);
    printf("%lld\n", ans);
  }
  return 0;
}
