#include <bits/stdc++.h>
using namespace std;
int num[3][3];
vector<int> prime;
int check[1000001];
vector<pair<int, int> > p[3];
long long int ai[1 << 17];
long long int number[3];
int cnt;
void calcprime() {
  for (int i = 2; i < 1000000; i++) {
    if (check[i] > 0) continue;
    prime.push_back(i);
    for (int j = i; j < 1000000; j += i) check[j]++;
  }
}
void factorize(long long int n, int nms, int start) {
  int i;
  if (n == 1) return;
  for (i = start; i < prime.size(); i++) {
    if (n % prime[i] == 0) {
      if (!p[nms].empty() && p[nms][p[nms].size() - 1].first == prime[i])
        p[nms][p[nms].size() - 1].second++;
      else
        p[nms].push_back(make_pair(prime[i], 1));
      break;
    }
  }
  factorize(n / prime[i], nms, i);
}
long long int pobae(long long int m) {
  long long int ret = m;
  int n = cnt;
  int k = 1;
  long long int tmp;
  for (int i = 1; i < (1 << n); i++) {
    tmp = m;
    k = 1;
    int s = i;
    while (s) {
      tmp /= ai[(s & -s) - 1];
      k *= -1;
      s -= s & -s;
    }
    ret += k * tmp;
  }
  return ret;
}
long long int divisors(long long int now, int k) {
  long long int ret = 0;
  if (k == p[2].size()) return 1;
  int pow = p[2][k].second;
  for (int i = 0; i <= pow; i++) {
    ret += divisors(now, k + 1);
    now *= p[2][k].first;
    if (now > number[0]) return ret;
  }
  return ret;
}
int main() {
  int t;
  scanf("%d", &t);
  calcprime();
  while (t--) {
    cnt = 0;
    for (int i = 0; i < 3; i++)
      scanf("%d %d %d", &num[i][0], &num[i][1], &num[i][2]);
    p[2].push_back(make_pair(2, 1));
    for (int i = 0; i < 3; i++) {
      number[i] = 1;
      for (int j = 0; j < 3; j++) number[i] *= num[i][j];
      factorize(number[i], i, 0);
    }
    for (int i = 0; i < p[0].size(); i++) {
      int si = 0;
      for (int j = 0; j < p[2].size(); j++) {
        if (p[0][i].first == p[2][j].first) si = p[2][j].second;
      }
      if (p[0][i].second > si) {
        long long int tmp = 1;
        si++;
        while (si--) tmp *= p[0][i].first;
        ai[(1 << cnt) - 1] = tmp;
        cnt++;
      }
    }
    printf("%lld\n", pobae(number[1]) + divisors(1, 0));
    for (int i = 0; i < 3; i++) p[i].clear();
  }
}
