#include <bits/stdc++.h>
using namespace std;
long long n, m, ans, s;
int T, cn[1000000 + 10], cs[1000000 + 10], pn[50], ps[50], mn[1000000 + 10], g,
    p[1000000 + 10], a[1000000 + 10];
long long rd() {
  long long tmp = 0, f = 1;
  char ch = getchar();
  while (!isdigit(ch) && (ch != '-') && (ch != -1)) ch = getchar();
  if (ch == '-') f = -1, ch = getchar();
  while (isdigit(ch)) tmp = (tmp << 1) + (tmp << 3) + ch - '0', ch = getchar();
  return tmp * f;
}
void mk_tb() {
  for (int i = 2; i < 1000000 + 10; ++i) {
    if (!mn[i]) p[mn[i] = ++g] = i;
    for (int j = 1; j <= mn[i] && i * p[j] < 1000000 + 10; ++j)
      mn[i * p[j]] = j;
  }
}
void decom(long long n, int *a, int *la) {
  for (int i = 1; (long long)p[i] * p[i] <= n; ++i)
    if (!(n % p[i])) {
      la[++*la] = i;
      while (!(n % p[i])) ++a[i], n /= p[i];
    }
  if (n != 1) la[++*la] = mn[n], ++a[mn[n]];
}
void del(long long n, int *a) {
  for (int i = 1; p[i] * p[i] <= n; ++i)
    if (!(n % p[i]))
      while (!(n % p[i])) --a[i], n /= p[i];
  if (n != 1) --a[mn[n]];
}
void dfs(int k, long long tmp) {
  if (tmp > n) return;
  if (k > *ps) {
    ++ans;
    return;
  }
  long long t = 1;
  for (int i = 0; i <= cs[ps[k]]; ++i) {
    dfs(k + 1, tmp * t);
    t *= p[ps[k]];
  }
}
int main() {
  scanf("%d", &T);
  mk_tb();
  while (T--) {
    n = rd() * rd() * rd();
    m = rd() * rd() * rd();
    s = rd() * rd() * rd();
    s <<= 1;
    *pn = *ps = *a = 0;
    ans = 0;
    decom(n, cn, pn);
    decom(s, cs, ps);
    dfs(1, 1);
    for (int i = 1; i <= *pn; ++i)
      if (cn[pn[i]] > cs[pn[i]]) a[++*a] = pn[i];
    for (int i = 0; i < (1 << *a); ++i) {
      long long t = 1;
      int cnt = 0;
      for (int j = 1; j <= *a; ++j)
        if ((i >> j - 1) & 1) {
          for (int k = 1; t <= m && k <= cs[a[j]] + 1; ++k) t *= p[a[j]];
          ++cnt;
        }
      (cnt & 1) ? ans -= m / t : ans += m / t;
    }
    del(n, cn);
    del(s, cs);
    printf("%I64d\n", ans);
  }
  return 0;
}
