Problem: Mister B has a house in the middle of a giant plain field, which attracted aliens life. For convenience, aliens specified the Cartesian coordinate system on the field in such a way that Mister B's house has coordinates (0, 0). After that they sent three beacons to the field, but something went wrong. One beacon was completely destroyed, while the other two landed in positions with coordinates (m, 0) and (0, n), respectively, but shut down.

Mister B was interested in this devices, so he decided to take them home. He came to the first beacon, placed at (m, 0), lifted it up and carried the beacon home choosing the shortest path. After that he came to the other beacon, placed at (0, n), and also carried it home choosing the shortest path. When first beacon was lifted up, the navigation system of the beacons was activated.

Partially destroyed navigation system started to work in following way.

At time moments when both survived beacons are at points with integer coordinates the system tries to find a location for the third beacon. It succeeds if and only if there is a point with integer coordinates such that the area of the triangle formed by the two survived beacons and this point is equal to s. In this case the system sends a packet of information with beacon positions to aliens, otherwise it doesn't.

Compute how many packets of information system sent while Mister B was moving the beacons.

Input

The first line contains one integer t (1 ≤ t ≤ 1000) — the number of test cases. The next 3·t lines describe t test cases. 

Every test case is described in three lines as follows. Note that each parameter is given as a product of three factors.

The first line of a test case contains three space-separated integers: n1, n2, n3 (1 ≤ ni ≤ 106) such that n = n1·n2·n3.

The second line contains three space-separated integers: m1, m2, m3 (1 ≤ mi ≤ 106) such that m = m1·m2·m3.

The third line contains three space-separated integers: s1, s2, s3 (1 ≤ si ≤ 106) such that s = s1·s2·s3.

Note that for hacks only tests with t = 1 allowed.

Output

Print t integers one per line — the answers for each test.

Example

Input

3
2 1 1
2 1 1
1 1 3
1 5 1
2 2 1
1 1 2
10 6 18
2 103 2
13 1 13


Output

4
7
171

Note

First test case contains the following beacon positions: (2, 0) and (0, 2), s = 3. The following packets could be sent: ((2, 0), (0, 2), ( - 1, 0)), ((1, 0), (0, 2), (4, 0)), ((0, 0), (0, 2), (3, 1)), ((0, 0), (0, 1), ( - 6, 0)), where (b1, b2, p) has next description: b1 — first beacon position, b2 — second beacon position, p — some generated point.

Second test case contains the following beacon initial positions: (4, 0) and (0, 5), s = 2. The following packets could be sent: ((4, 0), (0, 5), (0, 4)), ((3, 0), (0, 5), (2, 3)), ((2, 0), (0, 5), (2, 2)), ((1, 0), (0, 5), (1, 4)), ((0, 0), (0, 4), (0, - 1)), ((0, 0), (0, 2), (2, 0)), ((0, 0), (0, 1), (4, 0)).

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
using namespace std;
template <class T>
inline T sqr(T x) {
  return x * x;
}
template <class T>
inline T parse(const string &s) {
  T x;
  stringstream ss(s);
  ss >> x;
  return x;
}
const double EPS = 1e-12;
const int INF = 1000 * 1000 * 1000;
const long long LINF = INF * 1ll * INF;
const double DINF = 1e200;
const double PI = 3.1415926535897932384626433832795l;
int gcd(int a, int b) { return a ? gcd(b % a, a) : b; }
long long gcd(long long a, long long b) { return a ? gcd(b % a, a) : b; }
long long gcdex(long long a, long long b, long long &x, long long &y) {
  if (!a) {
    x = 0, y = 1;
    return b;
  }
  long long k = b / a;
  long long g = gcdex(b - k * a, a, y, x);
  x -= k * y;
  return g;
}
long long inv(long long a, long long m) {
  assert(m > 1);
  long long x, y, g;
  g = gcdex(a, m, x, y);
  return (x % (m / g) + m / g) % m / g;
}
long long crt(long long a1, long long m1, long long a2, long long m2) {
  long long a = (a2 - a1 % m2 + m2) % m2;
  long long x, y, g;
  g = gcdex(m1, m2, x, y);
  if (a % g) return -1;
  long long m = m1 / g * m2;
  x *= a / g;
  x = (x % m2 + m2) % m2;
  long long r = (a1 + x * m1) % m;
  assert(r % m1 == a1 && r % m2 == a2);
  return r;
}
long long powmod(long long a, long long p, long long m) {
  long long r = 1;
  while (p) {
    if (p & 1) r = r * a % m;
    p >>= 1;
    a = a * a % m;
  }
  return r;
}
bool isprime(long long a) {
  if (a <= 1) return false;
  for (long long i = 2; i * i <= a; ++i) {
    if (a % i == 0) return false;
  }
  return true;
}
long long sqrtup(long long a) {
  if (!a) return 0;
  long long x = max(0ll, (long long)sqrt((double)a));
  while (x * x >= a) --x;
  while ((x + 1) * (x + 1) < a) ++x;
  return x + 1;
}
long long isqrt(long long a) {
  assert(a >= 0);
  long long x = max(0ll, (long long)sqrt((double)a));
  while (x * x > a) --x;
  while ((x + 1) * (x + 1) <= a) ++x;
  return x;
}
long long sgn(long long x) { return x < 0 ? -1 : x > 0 ? 1 : 0; }
template <class T>
ostream &operator<<(ostream &s, const vector<T> &v);
template <class A, class B>
ostream &operator<<(ostream &s, const pair<A, B> &p);
template <class K, class V>
ostream &operator<<(ostream &s, const map<K, V> &m);
template <class T, size_t N>
ostream &operator<<(ostream &s, const array<T, N> &a);
template <class... T>
ostream &operator<<(ostream &s, const tuple<T...> &t);
template <class T>
ostream &operator<<(ostream &s, const vector<T> &v) {
  s << '[';
  for (int i = 0; i < (((int)(v).size())); ++i) {
    if (i) s << ',';
    s << v[i];
  }
  s << ']';
  return s;
}
template <class A, class B>
ostream &operator<<(ostream &s, const pair<A, B> &p) {
  s << "(" << p.first << "," << p.second << ")";
  return s;
}
template <class K, class V>
ostream &operator<<(ostream &s, const map<K, V> &m) {
  s << "{";
  bool f = false;
  for (const auto &it : m) {
    if (f) s << ",";
    f = true;
    s << it.first << ": " << it.second;
  }
  s << "}";
  return s;
}
template <class T>
ostream &operator<<(ostream &s, const set<T> &m) {
  s << "{";
  bool f = false;
  for (const auto &it : m) {
    if (f) s << ",";
    f = true;
    s << it;
  }
  s << "}";
  return s;
}
template <class T>
ostream &operator<<(ostream &s, const multiset<T> &m) {
  s << "{";
  bool f = false;
  for (const auto &it : m) {
    if (f) s << ",";
    f = true;
    s << it;
  }
  s << "}";
  return s;
}
template <class T, class V, class C>
ostream &operator<<(ostream &s, const priority_queue<T, V, C> &q) {
  auto a = q;
  s << "{";
  bool f = false;
  while (!a.empty()) {
    if (f) s << ",";
    f = true;
    s << a.top();
    a.pop();
  }
  return s << "}";
}
template <class T, size_t N>
ostream &operator<<(ostream &s, const array<T, N> &a) {
  s << '[';
  for (int i = 0; i < (((int)(a).size())); ++i) {
    if (i) s << ',';
    s << a[i];
  }
  s << ']';
  return s;
}
template <size_t n, class... T>
struct put1 {
  static ostream &put(ostream &s, const tuple<T...> &t) {
    s << get<sizeof...(T) - n>(t);
    if (n > 1) s << ',';
    return put1<n - 1, T...>::put(s, t);
  }
};
template <class... T>
struct put1<0, T...> {
  static ostream &put(ostream &s, const tuple<T...> &t) { return s; }
};
template <class... T>
ostream &operator<<(ostream &s, const tuple<T...> &t) {
  s << "(";
  put1<sizeof...(T), T...>::put(s, t);
  s << ")";
  return s;
}
ostream &put3(ostream &s, const char *, bool) { return s; }
template <class U, class... T>
ostream &put3(ostream &s, const char *f, bool fs, U &&u, T &&...t) {
  while (*f == ' ') ++f;
  if (!fs) s << ", ";
  auto nf = f;
  int d = 0;
  while (*nf && (*nf != ',' || d)) {
    if (*nf == '(')
      ++d;
    else if (*nf == ')')
      --d;
    ++nf;
  }
  auto nf2 = nf;
  while (nf2 > f && *(nf2 - 1) == ' ') --nf;
  fs = *f == '"';
  if (!fs) {
    s.write(f, nf2 - f);
    s << "=";
  };
  s << u;
  if (fs) s << ' ';
  if (*nf) ++nf;
  return put3(s, nf, fs, forward<T>(t)...);
}
void fact(long long a, map<long long, long long> &m) {
  for (long long p = 2; p * p <= a; ++p) {
    while (a % p == 0) {
      a /= p;
      ++m[p];
    }
  }
  if (a > 1) ++m[a];
}
map<long long, long long> read() {
  map<long long, long long> m;
  for (int i = 0; i < (3); ++i) {
    long long a;
    cin >> a;
    fact(a, m);
  }
  return m;
}
long long toval(map<long long, long long> m) {
  long long r = 1;
  for (auto it : m) {
    for (int i = 0; i < (it.second); ++i) r *= it.first;
  }
  return r;
}
long long doit1(int a, long long n, const vector<long long> &v) {
  if (!n) return 0;
  long long r = n;
  for (; a < ((int)(v).size()); ++a) r -= doit1(a + 1, n / v[a], v);
  return r;
}
long long doit2(
    int a, long long n,
    const vector<pair<pair<long long, long long>, pair<long long, long long>>>
        &v) {
  if (!n) return 0;
  if (a == ((int)(v).size())) {
    return 1;
  }
  if (v[a].first.first <= n) {
    return v[a].first.second;
  }
  long long r = 1;
  for (; a < ((int)(v).size()); ++a) {
    long long t = n;
    for (int k = 0; k < (v[a].second.second); ++k) {
      t /= v[a].second.first;
      r += doit2(a + 1, t, v);
    }
  }
  return r;
}
long long brute1(long long n, long long m, long long s) {
  long long r = 0;
  for (long long i = 1; i <= m; ++i) {
    r += s % gcd(i, n) == 0;
  }
  return r;
}
long long brute2(long long n, long long s) {
  long long r = 0;
  for (long long i = 1; i <= n; ++i) r += s % i == 0;
  return r;
}
long long brute(long long n, long long m, long long s) {
  return brute1(n, m, s) + brute2(n, s);
}
long long solve(map<long long, long long> n, map<long long, long long> m,
                map<long long, long long> s) {
  vector<long long> v;
  for (auto it : n) {
    long long c = s.count(it.first) ? s.at(it.first) : 0ll;
    if (it.second <= c) continue;
    long long t = 1;
    for (int i = 0; i < (c + 1); ++i) t *= it.first;
    v.push_back(t);
  }
  long long res = doit1(0, toval(m), v);
  vector<pair<pair<long long, long long>, pair<long long, long long>>> u;
  for (auto it : s) {
    long long t = 1;
    for (int i = 0; i < (it.second); ++i) t *= it.first;
    u.push_back(make_pair(make_pair(t, it.second + 1), it));
  }
  sort((u).begin(), (u).end());
  reverse((u).begin(), (u).end());
  for (int i = ((int)(u).size()) - 2; i >= 0; --i) {
    u[i].first.first *= u[i + 1].first.first;
    u[i].first.second *= u[i + 1].first.second;
  }
  res += doit2(0, toval(n), u);
  return res;
}
void test() {
  for (long long lim = 1; lim <= 1000; ++lim) {
    for (int qqq = 0; qqq < (1000); ++qqq) {
      long long n = rand() % lim + 1;
      long long m = rand() % lim + 1;
      long long s = rand() % lim + 1;
      map<long long, long long> nf, mf, sf;
      fact(n, nf);
      fact(m, mf);
      fact(s, sf);
      long long r = solve(nf, mf, sf);
      long long b = brute(n, m, s);
      if (r != b) {
        do {
        } while (false);
        return;
      }
    }
  }
  do {
  } while (false);
}
int main(int argc, char **argv) {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.precision(20);
  int tc;
  cin >> tc;
  for (int qqq = 0; qqq < (tc); ++qqq) {
    auto n = read();
    auto m = read();
    auto s = read();
    ++s[2];
    long long r = solve(n, m, s);
    cout << r << '\n';
  }
  return 0;
}