#include <bits/stdc++.h>
using namespace std;
long long ans, num[3];
vector<long long> v1;
vector<pair<int, int> > v2;
long long pow(long long a, long long b) {
  if (b == 0) return 1;
  if (b % 2) return a * pow(a, b - 1);
  long long c = pow(a, b / 2);
  return c * c;
}
void count1(int i, long long tmp, int u) {
  if (i == v1.size()) {
    ans += (u % 2 ? -1 : 1) * (num[1] / tmp);
    return;
  }
  count1(i + 1, tmp, u);
  count1(i + 1, tmp * v1[i], u + 1);
}
void count2(int i, long long tmp) {
  if (tmp > num[0]) return;
  if (i == v2.size()) {
    ans += (tmp <= num[0]);
    return;
  }
  long long k = 1;
  for (int j = 0; j <= v2[i].second; j++) {
    count2(i + 1, tmp * k);
    k *= v2[i].first;
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int prime[1000001];
  for (int i = 0; i < 1000001; i++) prime[i] = i;
  for (int i = 2; i * i < 1000001; i++) {
    if (prime[i] == i) {
      for (int j = i * i; j < 1000001; j += i) prime[j] = min(prime[j], i);
    }
  }
  int t;
  scanf("%d", &t);
  while (t--) {
    ans = 0;
    map<int, int> nums[3];
    for (int i = 0; i < 3; i++) {
      num[i] = 1;
      for (int j = 0; j < 3; j++) {
        int a;
        scanf("%d", &a);
        num[i] *= a;
        while (a != 1) {
          if (nums[i].find(prime[a]) == nums[i].end())
            nums[i][prime[a]] = 1;
          else
            nums[i][prime[a]]++;
          a /= prime[a];
        }
      }
    }
    if (nums[2].find(2) == nums[2].end())
      nums[2][2] = 1;
    else
      nums[2][2]++;
    v1.clear();
    for (map<int, int>::iterator it = nums[0].begin(); it != nums[0].end();
         it++) {
      if (nums[2].find(it->first) == nums[2].end() ||
          nums[0][it->first] > nums[2][it->first])
        v1.push_back(pow(it->first, nums[2][it->first] + 1));
    }
    count1(0, 1, 0);
    v2.clear();
    for (map<int, int>::iterator it = nums[2].begin(); it != nums[2].end();
         it++)
      v2.push_back(*it);
    count2(0, 1);
    printf("%lld\n", ans);
  }
  return 0;
}
