#include <bits/stdc++.h>
using namespace std;
template <typename T, typename U>
inline void smin(T &a, U b) {
  if (a > b) a = b;
}
template <typename T, typename U>
inline void smax(T &a, U b) {
  if (a < b) a = b;
}
inline void getint(int &first) {
  char c;
  while (c = getchar(), c > '9' || c < '0')
    ;
  for (first = 0; c >= '0' && c <= '9'; c = getchar())
    first = (first << 1) + (first << 3) + c - '0';
}
inline void print(int first) {
  if (first < 10) {
    putchar('0' + first);
    return;
  }
  print(first / 10);
  putchar(first % 10 + '0');
}
int power(int a, int b, int m, int ans = 1) {
  for (; b; b >>= 1, a = 1LL * a * a % m)
    if (b & 1) ans = 1LL * ans * a % m;
  return ans;
}
vector<pair<long long, long long> > fac[5];
long long q[11111111];
int cmp(pair<long long, long long> &a, pair<long long, long long> b) {
  if (a.first != b.first) return 0;
  a.second += b.second;
  return 1;
}
vector<pair<long long, long long> > calc1(
    vector<pair<long long, long long> > &a,
    vector<pair<long long, long long> > &b) {
  int i = 0, n = a.size(), j = 0, m = b.size();
  vector<pair<long long, long long> > tmp;
  while (i < n && j < m) {
    if (a[i].first == b[j].first) {
      if (a[i].second < b[j].second)
        tmp.push_back(pair<long long, long long>(b[j].first, a[i].second + 1));
      i++, j++;
    } else if (a[i].first > b[j].first)
      tmp.push_back(pair<long long, long long>(b[j].first, 1)), j++;
    else
      i++;
  }
  while (j < m) tmp.push_back(pair<long long, long long>(b[j].first, 1)), j++;
  return tmp;
}
vector<pair<long long, long long> > c;
long long calc(long long second, long long first) {
  long long ans = 0;
  for (int i = 0; i < c.size(); i++) {
    if (first % c[i].first == 0) {
      if (c[i].second & 1LL)
        ans -= second / c[i].first;
      else
        ans += second / c[i].first;
    }
  }
  return ans;
}
int main() {
  int T, qb;
  for (scanf("%d", &T); T--;) {
    long long first = 1, second = 1, z = 2;
    for (int i = 0; i < 3; i++) {
      fac[i].clear();
      if (i == 2) fac[i].push_back(pair<long long, long long>(2, 1));
      for (int j = 0; j < 3; j++) {
        int u;
        scanf("%d", &u);
        if (i == 0) first *= u;
        if (i == 1) second *= u;
        if (i == 2) z *= u;
        for (int v = 2; v * v <= u; v++)
          if (u % v == 0) {
            int cnt = 0;
            while (u % v == 0) u /= v, cnt++;
            fac[i].push_back(pair<long long, long long>(v, cnt));
          }
        if (u > 1) fac[i].push_back(pair<long long, long long>(u, 1));
      }
      if (!fac[i].size()) continue;
      sort(fac[i].begin(), fac[i].end());
      fac[i].erase(unique(fac[i].begin(), fac[i].end(), cmp), fac[i].end());
    }
    qb = 0;
    q[qb++] = 1;
    fac[3] = calc1(fac[2], fac[0]);
    long long mx = (long long)(sqrtl(z) + 1);
    for (int i = 0; i < fac[2].size(); i++) {
      long long u = fac[2][i].first, v = fac[2][i].second;
      for (int z = qb, j = 0; j < z; j++) {
        long long mid = 1;
        for (int k = 0; k < v; k++) {
          mid *= u;
          if (mid > mx) break;
          if (q[j] * mid > mx) break;
          q[qb++] = q[j] * mid;
        }
      }
    }
    long long ans = 0;
    for (int i = 0; i < qb; i++) {
      if (z / q[i] < q[i]) continue;
      if (q[i] <= first) ans++;
      if (q[i] * q[i] == z) continue;
      if (z / q[i] <= first) ans++;
    }
    qb = 0, q[qb++] = 1;
    q[qb++] = 0;
    for (int i = 0; i < fac[3].size(); i++) {
      long long u = fac[3][i].first, v = fac[3][i].second;
      for (int z = qb, j = 0; j < z; j += 2) {
        long long mid = 1;
        for (int k = 0; k < v; k++) mid *= u;
        if (q[j] > second / mid) continue;
        q[qb++] = q[j] * mid;
        q[qb++] = q[j + 1] + 1;
      }
    }
    for (int i = 0; i < qb; i += 2) {
      if (q[i + 1] & 1LL)
        ans -= second / q[i];
      else
        ans += second / q[i];
    }
    printf("%I64d\n", ans);
  }
  return 0;
}
